LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2011, 02:07 PM   #1
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Rep: Reputation: 0
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.?
 
Old 05-30-2011, 03:05 PM   #2
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
Unless this is just for you to learn scripting, you can get the number of lines with
Code:
#cat swansong |wc -l
 
Old 05-30-2011, 03:20 PM   #3
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
#cat swansong |wc -l
with this I'll be limited to file 'swansong'.

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.
 
Old 05-30-2011, 03:31 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You can easily put that in a script
Code:
#! /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"
'
 
Old 05-30-2011, 03:50 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
1 members found this post helpful.
Old 05-30-2011, 03:50 PM   #6
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TobiSGD View Post

# 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 .
 
Old 05-30-2011, 04:14 PM   #7
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post


exec < /dev/stdin ### No need to redirect standard input at the end
### of a script. Anyway /dev/stdin is the correct
### one, not tty.
[/CODE]
what is /dev/stdin? What does it do?


Why

Quote:
terminal= 'tty'
exec < $terminal
is incorrect ?
 
Old 05-30-2011, 04:15 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
I made the same mistake as you in your above script, the line should be
Code:
nol=$(cat $1 | wc -l)
 
Old 05-30-2011, 04:21 PM   #9
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TobiSGD View Post
I made the same mistake as you in your above script, the line should be
Code:
nol=$(cat $1 | wc -l)

Or it could be:
Code:
nol = ` cat $1 | wc - l `
Anyways
Quote:
nol=$(cat $1 | wc -l)
worked for me .
 
Old 05-30-2011, 09:39 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by hd_pulse
nol = ` cat $1 | wc - l `
Actually, no it cannot. You need to re-read colucix's advice
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in read a text file and display in DOS command... steve2688 Programming 7 06-17-2009 03:04 AM
number of lines to the new file coppuca Linux - Newbie 7 01-06-2009 10:17 AM
can't see all 20,160 lines in a file when 'cat filename' dave247 Debian 4 10-25-2008 05:13 PM
Command prompt display Jongi Linux - General 2 05-06-2006 06:57 AM
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 12:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration