3

I am writing some script to make a directory, but I want firstly sure that the name that the user insert is correct. for this issue I wrote something like below (FILE.txt is the file which the user name saved in)

if grep -q  '>' FILE.txt
  then
  echo "You should avoid inserting ">" in your code!"
  fi

But it seems that the bash thought that I want to insert to output and so it give this error: -sh: syntax error near unexpected token `newline' Do anybody have some idea? tnx for help

Ali Payandeh
  • 63
  • 1
  • 10
  • I've always liked referring folks to [this answer](http://stackoverflow.com/a/9712555/1072112) when there's a problem with quoting. :-) – ghoti Oct 21 '15 at 22:37

2 Answers2

5

The problem isn't with your grep line, it's with your echo line. Your shell sees it as:

echo "You should avoid inserting "  >  " in your code!"

You can fix this simply by removing the quotes in the middle of the sentence, or using single quotes like:

echo "You should avoid inserting '>' in your code!"
seumasmac
  • 2,174
  • 16
  • 7
1

You have to escape the quote in your echo statement:

if grep -q '>' file.txt; then
    echo "You should avoid inserting \">\" in your code!"
else
    echo "code is good"
fi
John M.
  • 602
  • 7
  • 6