Basic shell script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic shell script help
# 1  
Old 06-15-2011
Question Basic shell script help

Im trying to make a script that simply adds a word to the last available line in a txt file without overwriting any previous lines.

Ive googled this and there are great examples but no one explain what each function does, and i dont entirely understand how it works.

Basically Im looking for someone to show me the correct format, and tell me how it works. Im going to use this as a basis to write more complex shell scripts in the future.
# 2  
Old 06-16-2011
It would be helpful to know what you found, and what you intend to use. Very difficult to explain how something works if you don't post it.

Assuming you are writing Kshell or bash based scripts, this snipit of code will append the word 'foo' to the last line in the file. It assumes the name of the file is passed in on the command line:

Code:
/usr/bin/sed  '$!b
s/$/ foo/' $1 >tmp.$$
if (( $? == 0 ))          # if the sed completed successfully, move tmp file over original file
then
   mv tmp.$$ $1
else
  rm tmp.$$
  echo "error generating new file"
fi

The sed programme reads the file ($1). For each line that is not the last line ($!b) it branches to the end of the programme which by default prints the line. When the last line is encountered, the end of the line is substituted with the word "foo" preceded by a space. The line is then printed.

Depending on your version of sed, you will need to have a hard newline after the b. If you don't it will interpret the next token as the target of the branch. There might be a work around for this, but I am not aware of it.

The output of the sed command is written to a temporary file. If the sed is successful ($? contains the exit code; 0 is good) then the temp file is moved onto (replaces) the original file. If the sed errors, then the temp file is removed and an error is written.

I'm sure there are other ways of doing this.
# 3  
Old 06-16-2011
Quote:
Originally Posted by kylecn
Im trying to make a script that simply adds a word to the last available line in a txt file without overwriting any previous lines.
You can use sed for that , see the following example:
Code:
sed -i '$s/$/New test/' file

# 4  
Old 06-16-2011
Seems easy enough.

Would it be possible instead of replacing it with a pre defined name, have it choose a random name from a txt file or just specified in the script that it puts onto the last line.
# 5  
Old 06-16-2011
Quote:
Originally Posted by kylecn
Seems easy enough.

Would it be possible instead of replacing it with a pre defined name, have it choose a random name from a txt file or just specified in the script that it puts onto the last line.
Your request is not clear, please elaborate.
# 6  
Old 06-16-2011
For example instead of adding the word foo to the last line of the txt fiel which is what i initiially wanted i want to have a list of words that it can choose from.

foo
hello
cat
cow
pineapple

The script chooses a random word from the above and adds it to the last line of the text file instead of just having it add a one flat word every time. I dont know where the words would be stored, i suppose it would read them from a txt file o the random list would be in the script itself.

Last edited by kylecn; 06-16-2011 at 10:30 PM..
# 7  
Old 06-16-2011
Code:
var=newtext
sed -i '$s/$/'$var'/' file

This line will add the value of var variable to the end of last line in file

For the rest of your assignment, you should work yourself using your favorite shell howto read a file to array and pick a "random" array element for substitution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Beginner bash - basic shell script 'while' help...

Hi everyone, first time visitor to these forums here. Keeping a long story short I've been attempting to learn how to code in bash. I have VERY little previous experience with coding languages besides simply copying and pasting batch scripts for Windows. So, with that in mind I've followed a... (4 Replies)
Discussion started by: Meta
4 Replies

2. Shell Programming and Scripting

Basic Shell script - Not working

Hello, This is basic (i think). I am trying to run a shell script which would go into each folder (folder names defined in the list) and after entering would run some commands, once done, come out of the folder and continue until the list ends. Pretty basic and there are bunch of example online. ... (9 Replies)
Discussion started by: Zam_1234
9 Replies

3. Shell Programming and Scripting

Basic Combination Shell Script

I need to have a script read a file that has a list of words in a single column like below:Black Blue Brown Orange Red Yellow Green White Purple Silver Grey Tan Then print to another file just all of the two-word possible combinations. Example: Black,Blue Anyone want to take a... (4 Replies)
Discussion started by: vespasian
4 Replies

4. Shell Programming and Scripting

Need a little help with my first shell script. Basic image resize script...

Hey everyone, just now joined because I didn't want to go onto Ubuntu forums and start asking about how to write shell scripts. Seems like this is a pretty active forum for exactly what I need. I'm trying to modify a shell script I found online, the end goal is to have it find all files in the... (9 Replies)
Discussion started by: mozzles
9 Replies

5. UNIX for Dummies Questions & Answers

Basic sed usage in shell script

Hi, A basic sed question. I have a set of files. In each file there is a number that I want replaced. For example, if I run sed I should get the following: % cat test2.txt #goofy//171.00 goofy 171.00 % sed -i 's/171/xxx/g' test2.txt % cat test2.txt #goofy//xxx.00 goofy xxx.00 ... (2 Replies)
Discussion started by: pc2001
2 Replies

6. Shell Programming and Scripting

Help! Basic shell script advice

##### (2 Replies)
Discussion started by: AidoPotato
2 Replies

7. Shell Programming and Scripting

Basic Shell Script Help

Lets say I wanted to create a script that would show what people are doing on my machine using the w command and refresh in about 6 seconds. What would be the easiest way to do this? I pretty much want the script to loop until I stop it. I'm using the BASH shell by the way. Help is appreciated.... (1 Reply)
Discussion started by: c4391
1 Replies

8. Shell Programming and Scripting

shell script basic doubt

hi, I am new script learner, so my basic doubt is , how to store value of any command in a variable example $ ls | wc -l i want to stote the output of this in a variable c. so that i can use c in if else loop. and when do we use " ` " symbol in script.. can anyone also tell for... (5 Replies)
Discussion started by: hi2_t
5 Replies

9. Shell Programming and Scripting

Basic Shell script syntax help

Hi All, I am new to shell scripting. I have a variable which holds a numeric value.I have to check whether this variable holds a value between(0- 8),(8-17)(17-24).How do i write this syntax using if in shell scripting. Thanks Vignesh (2 Replies)
Discussion started by: vignesh53
2 Replies

10. Shell Programming and Scripting

need a quick basic shell script help

im trying to run the below if command ifconfig -a |grep 10.100.120.21 gives me below output inet addr:10.100.120.21 Bcast:10.100.120.255 Mask:255.255.255.0 i just want a basic shell which says if above exists then continue how would i do this? (6 Replies)
Discussion started by: eb222
6 Replies
Login or Register to Ask a Question