On Sun, 8 Apr 2001, Andrew Smith wrote: as> Hi as> as> I'm writing a small bash script whereby I want part of it to check to see if as> an argument given to then script is contained in a file. If not, it needs to as> be added. For example: as> as> /tmp/names is a file containing: as> as> Tom as> Dick as> Harry as> as> If my script (eg, /tmp/foo) is called as shown below: as> as> /tmp/foo Gary as> as> The script should add Gary to the file. as> as> as> If my script is called as shown below: as> as> /tmp/foo Harry as> as> The script should return a message saying Harry is already in the list. as> as> as> as> This is what I have so far ($2 is the argument) as> as> if ! [[cat /tmp/names | grep $2 = "$2"]]; then { as> echo>>"/tmp/names" "$2" as> echo "Name isn't already in the file. Name added!" as> } else { as> echo "name is already in the file. Name is NOT added again." as> } fi as> Your trying to use an argument that doesn't exist, the names your using in the above examples reside at the 1st location, not the 2nd as your using within your routine. Try the following #!/bin/sh # if [ ! `cat ./names | grep $1` ]; then echo $1 >> ./names echo "Name added!" else echo "Name already exists in file." fi skull@daydream:~/bin > cat ./names Bill Charlie Percy Fred George Ron skull@daydream:~/bin > ./foo Ron Name already exists in file. skull@daydream:~/bin > ./foo Ginny Name added! skull@daydream:~/bin > cat ./names Bill Charlie Percy Fred George Ron Ginny Also, you'll want to add a check before the bulk of the script to see that a name exists at argument location 1, otherwise it will fail and add a blank line to the file. To get around this you could use the following test -z $1 && exit 1 which will just quietly exit the routine and do nothing, or the following if [ -z $1 ]; then echo "Nothing supplied, exiting" exit 1 fi which will at least tell you what happened. as> It basically prints the file piped through grep and makes a judgement based as> on the output. My problem is the script always thinks that a given name as> isn't in the list and adds it, whether the name is in the file or not. as> as> Could anyone help? as> as> Many thanks, as> as> as> as> Andrew Smith as> as> as> -- S.Toms - tomas@primenet.com - www.primenet.com/~tomas SuSE Linux v7.0+ - Kernel 2.2.18 Sauron is alive in Argentina!