please pardon since I am a UNIX beginner.
I wanna write a shell script that can ask user type in the filename and output the number of lines in that file, here is my code:
echo "Pls enter your filename:"
read filename
result=wc -l $filename
echo "Your file has $result lines"
However, I couldn’t get it working since it complains about the identifier “filename”. Could experts help?Thanks !!
That works fine, at least in
bash. Well, thereadworks fine. However, the assignment toresultshould probably be:but, since that command outputs both the line count and the filename, you might want to change it a little to just get the line count, something like:
or:
The command you have:
will set
resultto the literalwc, then try to execute the-lcommand.For example, the following five-line script:
will, when run and given its name as input, produce the following:
If you’re not using
bash, you need to specify which shell you are using. Different shells have different ways of doing things.