LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-11-2010, 08:13 AM   #1
dgs012
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Rep: Reputation: 0
Passing multiple arguments in shell script


The script receives multiple files as parameters and it is supposed to count the number of lines in each of them and write that number in another file.

This is my script:
Code:
while [ -n "$1" ]
     do
       lines=`cat $1 | wc -l`
       echo "The number of lines in file $1 is $lines." >> lines.txt
       shift
     done
Is there any other way to do the same thing, without using shift?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-11-2010, 08:40 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Yes:
Code:
for posparm in "$@"
do
   echo "The number of lines in file $posparm is $( wc -l $posparm )"
done
Note: $( <commands> ) has advantages over ` <commands> ` as explained on Greg's WIKI.
 
Old 12-11-2010, 09:03 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
And if you look under Pitfalls from the same author you find you can change the following:
Code:
for item in "$@"

#becomes simply

for item
 
2 members found this post helpful.
Old 12-11-2010, 09:32 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by grail View Post
And if you look under Pitfalls from the same author you find you can change the following:
Code:
for item in "$@"

#becomes simply

for item
Cool! More specifically described on this Greg's WIKI page and in this section of the GNU bash reference.
 
Old 12-11-2010, 09:36 AM   #5
dgs012
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you very much for the help!
 
Old 12-11-2010, 09:41 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Another one:
Code:
wc -l "$@" | awk '!/total/{printf "The number of lines in file %s is %d.\n", $2, $1}'
 
Old 12-11-2010, 09:57 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
And more awk
Code:
awk 'FNR != NR{print NR-1;NR=1}END{print NR}' "$@"
 
Old 12-11-2010, 01:06 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Great, grail! Adding something more...
Code:
awk 'FNR == NR{file=FILENAME}FNR != NR{printf "The number of lines in file %s is %d.\n",file,NR-1; NR=1}END{printf "The number of lines in file %s is %d.\n",FILENAME,NR}' "$@"
but at this point is far more readable the loop solution!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Passing arguments to shell script like perl script get-opts somupl86 Linux - Newbie 2 12-03-2010 12:14 AM
[SOLVED] case statement passing multiple arguments supulton Programming 2 08-07-2010 09:00 PM
shell scripts wrappers and passing arguments nosbod Programming 2 09-21-2006 09:27 AM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 06:13 AM
Apache passing arguments to a shell script disabled? mikeyt_333 Linux - Security 3 08-03-2004 07:09 PM


All times are GMT -5. The time now is 09:03 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration