[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Script to add line to file if it doesn't exist



On 2010-04-08 03:01, Mart Frauenlob wrote:
On 07.04.2010 23:12, Ron Johnson wrote:
On 2010-04-07 15:45, Mart Frauenlob wrote:
On 07.04.2010 22:10, Kent West wrote:

[...]

I want a script that will read the file and look for the name "fred",
and if it's found, leave the file alone, but if it's not found, to add
the name "fred" to the bottom of the file.


#!/bin/sh
grep -w "fred" file || printf "%s\n" "fred" >>file

Mart's method is the shell way.  The 3GL Way is:

grep -w "$NAME" "$FILE"
TMP=$?
if [ "$TMP" = "1" ];
then
    echo -e "$NAME\n" >> "$FILE"
fi



May I ask what the 3GL way is?
All that is 'shell' scripting, right?


Shell operators like &&, ||, |, > *implicitly* do "if" comparisons, open files for read/write, etc.

By "the 3GL Way", I mean a more explicit enunciation of each step.

--
"History does not long entrust the care of freedom to the weak
or the timid."  Dwight Eisenhower


Reply to: