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.
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.
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 …
cat content.txt | grep "foo" && work.sh