I'm trying to write this script for an assignment - it's only the second one I've written so bear with me.
As a part of this script, I need to be able to check if the first argument given matches the first word of file. If it does, exit with an error message, if it doesn't, append the arguments to the file. I understand how to write the if statement, but not how to use grep within a script. I understand that grep will look something like this
grep ^$1 schemas.txt
I feel like this should be much easier than I am making it. Any help appreciated.
Update: I'm getting an error too many arguments on the if statement. I got rid of the space between grep-q and then got an error binary operator expected. I've been poking around at it, but I don't see what it sees.
if [ grep -q ^$1 schemas.txt ]
then
echo "Schema already exists. Please try again"
exit 1
else
echo "$@" >> schemas.txt
fi

[…]and it'll work. Though you probably want to quote your pattern:if grep -q "^$1" schemas.txt; then …– derobert Sep 18 '12 at 19:25