[SOLVED] read filename from the command prompt and display the number of lines in file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
read filename from the command prompt and display the number of lines in file
Quote:
if [ $# -eq 1 ]
then
terminal= 'tty'
exec <$1
flag=1
fi
nol=0
while read line
do
nol= 'expr $nol + 1'
done
echo "number of lines= $nol "
if [ "$flag" = 1 ]
then
exec < $terminal
fi
I tried to write the code which reads the filename from the command prompt & displays the total number of lines in the file.
The file which I want to count to count the lines is 'swansong'
So while executing I put it like this:
Quote:
./sample swansong
where sample is the name of the script file.
But it generates error
Quote:
/dev/pts/1
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
./sample: line 15: expr $nol + 1: command not found
number of lines= 0
Please help.
Where to edit so that it works fine for me.?
I wrote the above code which accepts any filename (text file) at command prompt then prints the number of lines it has in it . Not just any specific file.
#! /bin/bash
# Test for correct number of commandline parameters and if the specified file exists here
.
.
.
# Now here the command
nol='cat $1 | wc -l'
echo "Number of lines in $1: $nol"
Just to review your script, so that you can learn from errors:
Code:
#!/bin/bash
if [ $# -eq 1 ]
then
terminal='tty' ### No spaces before and after the equal sign
exec <$1
flag=1
fi
nol=0
while read line
do
nol=$(expr $nol + 1) ### Command substitution uses backticks or $(command) construct
done
echo "number of lines= $nol "
if [ "$flag" -eq 1 ] ### -eq is for integer comparison
### = or == is for string comparison
then
exec < /dev/stdin ### No need to redirect standard input at the end
### of a script. Anyway /dev/stdin is the correct
### one, not tty.
fi
# Test for correct number of commandline parameters and if the specified file exists here
.
.
.
# Now here the command
nol='cat $1 | wc -l'
echo "Number of lines in $1: $nol"[/CODE]'
I used the above code in the program but it gives the output as it is:
Quote:
Number of lines in swansong: cat $1 | wc -l
My purpose is not solved . It doesnt gives the desired output.
The file 'swansong' of which I want to calculate the line numbers exists in the same HOME directory where the this script resides.
Also what is the use of exec command then
Quote:
exec <$1
please also give a hint to solve it with 'exec' command .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.