PDA

View Full Version : bash script help



udha
July 5th, 2006, 10:08 AM
Here's some psudocode of what I'm after:
to run the script:
./addthis.sh something

the script will do something like:
get input $1
if $1 exists in file: sorry, that is taken
else add $1 to file

do if a string exists somewhere in a file, I need to catch that out, any tips, I'm sure this is easy to do but it's got me stumped:?

kabus
July 5th, 2006, 10:20 AM
A rough idea :


if grep -q "$1"' file; then
echo "sorry, that is taken"
else
echo "$1" >> file
fi

udha
July 5th, 2006, 10:51 AM
A rough idea :


if grep -q "$1" file; then
echo "sorry, that is taken"
else
echo "$1" >> file
fi

Thanks that works, one further tweak is that some lines might have more text, so if there was:

yoyo

and you try to insert 'yo' it will fail, any ideas?

udha
July 5th, 2006, 11:05 AM
I've gotten around it by putting values in double-quotes:

grep -q "\"$1\"" file; ....

echo "\"$1\"" >> file
...