9 Replies - 3112 Views - Last Post: 07 March 2012 - 01:23 PM

#1 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Create and Edit a Linux Script File - openSUSE

Posted 04 March 2012 - 02:56 PM

I am obviously a new comer to linux, and this seemingly simple task is driving me crazy. First, I have searched for a tutorial or guidance on doing this but have come empty. I am learning this for school but also because I want expand my knowledge of linux. Part of my issue is there seems to be numerous editor choices, so each web search has a slightly different twist on the topic. Right now my main issue is not the creation of the file, but the actual edit. I have created the file, Assignment7.script (my instructors naming request). And I have confirmed it exists in my bin folder. My most recent attempt was to use the touch command to create the file. Then I tried vi, vim, and emacs to open/edit the file. Each time I get the following. "(1) Another program may be editing the same file. and (2) An edit session for this file crashed." Then a number of choices, [O]pen Read-Only, (E)dit anyway, ®ecover, (D)elete it, (Q)uit, (A)bort."

1. What tool should I use to create and edit a text file (in this case a script file)?
2. Do I need even need to use vi, vim or emacs to edit the file?
3. How do I prevent the above message in the future? One post suggested that it was something I did. What didn't I do correctly.

Thank you,
d

Is This A Good Question/Topic? 0
  • +

Replies To: Create and Edit a Linux Script File - openSUSE

#2 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Create and Edit a Linux Script File - openSUSE

Posted 04 March 2012 - 04:00 PM

* Moved to the Linux, Unix, OSx forum *

Please try not to post your questions in the Tutorials section.
Was This Post Helpful? 1
  • +
  • -

#3 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Create and Edit a Linux Script File - openSUSE

Posted 04 March 2012 - 04:12 PM

Quote

Then I tried vi, vim, and emacs to open/edit the file. Each time I get the following. "(1) Another program may be editing the same file. and (2) An edit session for this file crashed." Then a number of choices, [O]pen Read-Only, (E)dit anyway, ®ecover, (D)elete it, (Q)uit, (A)bort."

Did you try try any of the options? Did any of them work?

I dimly remember getting this error at some point, because I had been editing the file in vim when the power was suddenly cut of. Once I got it back up running, I got something like that and my first choice (likely ®ecover) fixed everything. Even saved most of the stuff I wrote before the power was cut.

Quote

What tool should I use to create and edit a text file (in this case a script file)?

That's up to you, but I would recommend the nano editor if you are new to this. Vi, Vim and Emacs are all powerful editors, but there is a bit of a learning curve with all of them. Nano is simpler to use.

But I usually use vim to create and edit files through the CLI. It doesn't require you to actually create the file. You can just do: $ vim /path/to/new/file.ext, then you can write something (or not), and finally use the :wq command to save the changes. If the file did not exist prior to you opening it in vim, it will be created when you do this.
Was This Post Helpful? 1
  • +
  • -

#4 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Create and Edit a Linux Script File - openSUSE

Posted 04 March 2012 - 04:30 PM

Thanks for fixing that. It has been a long frustrating day.
Was This Post Helpful? 0
  • +
  • -

#5 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Create and Edit a Linux Script File - openSUSE

Posted 04 March 2012 - 04:53 PM

I did try the various options Edit, should be renamed '(X)destroy existing file and make another. The one post I could find on this suggested that somehow I did not end my last process correctly. After reading up on the cmd Touch, it appears to be a one shot deal, with arguments that simply generates a file. So I was even more stumped when the msg above appeared once again. I realize that making mistakes is a good way of learning, but seeing the same msg over and over, without any apparent reason got the better of me.

I will try nano, and I just actually found a Bash Script tutorial under the tutorials. A good short explanation.

I will go and play around. Also openSUSE has a text editor, should I just use that for creating scripts, or is that considered bad practice?
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Create and Edit a Linux Script File - openSUSE

Posted 04 March 2012 - 05:07 PM

Is there a file called .filename.ext.swp, where filname.ext is the name of the file you are trying to edit? - I was testing this over here, and if I kill the vim process while editing a file I get the error message you posted, and a file like that is created. Removing the file should clear out the error message. (Although any "recovery" options will be lost.)

Note that you need to use the -a switch for the ls command to see such files: ls -la, for example.

I've never used openSUSE myself, so I don't know which text editor it has. I wouldn't consider it a bad practice to use it though. There is no "right" way to create script files. - Whatever works best for you is the right way :)
Was This Post Helpful? 1
  • +
  • -

#7 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Create and Edit a Linux Script File - openSUSE

Posted 04 March 2012 - 05:50 PM

I had to install nano, but at the level I am it was worth it. A nice prompt to save. I still want to learn vim and or emacs but for now I need quick and dirty, my assign was due Friday. So it has to be complete tonight. Another post may happen regarding cmds, we shall see.

Yes, and after a number of times playing around there are several.swp files. e.g. assign7.script.swp.

I still want to understand what I did wrong. I see your point, I obviously just closed out of VIM prematurely not knowing any better. Now looking back on it, when I used Touch to create assign7.script, it would have been the second/third time creating the file with the same name. So assign7.script.swp would have existed from my previous attempt to edit/create the file using VIM or emacs.

I really appreciate the help.

d
Was This Post Helpful? 0
  • +
  • -

#8 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Create and Edit a Linux Script File - openSUSE

Posted 05 March 2012 - 09:17 PM

Here is the final product. The script has to be run with two arguments: folder_name and file_name.

#!/bin/bash
 
# Author: W0235269 diek
# Date: 03 Mar 2012

# 2. The script will create a subfolder in the current directory 
	mkdir $1

# 3. The script will report to the screen that the folder was created, with folder name.
	echo $1 "was created"

# 4. The script will create a file in the current directory with your first name, which is to be 
#    specified in the 2nd cmd arg.
	touch $2

# 5. The script will report to the screen that the file was created and with filename.
	echo "File $2 created"

# 6. The script will change the permissions on the file to be (RWE) for the 
#    user (owner), R and W for the owning grp, and just R for others.
	chmod 764 $2

# 7. The script will report that the permissions were changed.
	echo "Permissions changed"

# 8. The script will run a cmd to display all processes running on the system, get only the 
#    PID and the process name columns from the output, and redirect the output to the file created in Step 4.
	ps -e | awk '{print$1" "$4}' > $2

# 9. The script will run a cmd to display the disk usage of the root (/) file system, 
#    replace EVERY INSTANCE of the capital letter G with Gb, and APPEND the redirected output to the 
#    end of the file created in step 4.
	df -h / | sed 's/G/Gb/g' >> $2

# 10. The script will report to the screen that both the list of process and the disk usage of 
#     the root file system have been written to the file.
	echo "Disk Usage & Processess sent to $2"

# 11. The script will output the no of lines, words, and characters in the file created in Step 4.
	wc $2

# 12. The script will MOVE the file created in Step 4 into the folder created in Step 2.
	mv $2 $1

# 13. The script will report that it has moved the file and that the script has success completed.
	echo "Script Complete"

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6595
  • View blog
  • Posts: 30,785
  • Joined: 10-May 07

Re: Create and Edit a Linux Script File - openSUSE

Posted 06 March 2012 - 10:26 PM

For what it's worth, I would do the following :

#folder_name and file_name
if [ ! $1 ]; then
  echo "You must provide a folder name"
  exit
else folder=`echo $1`
fi
if [ ! $2 ]; then
  echo "You must provide a file name"
  exit
else
  filename=`echo $2`
fi



Then use the variables through the rest of the code. Nothing wrong with the way you have it coded, but ${filename} makes more sense than $2. I know what I'm looking at.

Plus error checking should always get you bonus points. Always.
Was This Post Helpful? 2
  • +
  • -

#10 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Create and Edit a Linux Script File - openSUSE

Posted 07 March 2012 - 01:23 PM

Supported! Nice touch, as a programming student I should have thought about that.

Thanks to you, and Atli for his patience and help.

d
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1