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.

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:

/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.

You can use man sed ("sed") for that , see the following example:

sed -i '$s/$/New test/' file

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.

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.

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.

Im not quite sure i understand how to use that line. Im better at disassembling something to figure out how it works, not getting the pieces and putting it together.

Either way i appreciate the help, i,ll see what i can do. :slight_smile: