Reputation: 226
I'm writing a code in UNIX to translate a txt document to html. The first parameter I pass in is the number of lines in the file to be used in the title. The second is the file to translate.
So far I have:
numLinesInTitle=$1
lineCount=`wc -l $2`
firstInput=`head -$numLinesInTitle $2`
This all works so far. Here's where I get the problem:
numBodyLines=`expr $lineCount - $1`
It says expr syntax error
Upvotes: 0
Views: 94
Reputation: 46876
The problem you're having is likely that your $lineCount
variable has more in it than you think.
Try running wc -l /etc/passwd
and see what you get. Now take that result (the whole result) and feed it to expr
, and see what you get. (Spoiler: "syntax error".)
The problem is that the wc
command shows you the name of the file along with the number of lines in that file. To avoid this behaviour, you can ask it to tell you the number of lines FROM STANDARD INPUT instead. For example:
lineCount=`wc -l < $2`
That should solve this particular issue. Here are a couple of hints that may solve future issues.
lineCount=$(wc -l < $2)
would be better.lineCount=$(wc -l < "$2")
would be better.Upvotes: 2