How to check the number of command line arguments? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to check the number of command line arguments?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-14-2002
prkwan prkwan is offline
Registered User
 
Join Date: Nov 2002
Last Activity: 19 November 2002, 7:41 AM EST
Location: London UK
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
How to check the number of command line arguments?

How do you check the number of command line arguments and give an error message if wrong?

also how to check if 2 files exist and are regular file and give an error message if wrong? (I know is something to do with test)

if test $filename1 = $filename2 ;
then ... (dont know what to do)
else echo Wrong file
fi


How do you use cmp to check if the files have the same content and output a sutiable message?

cmp file1 file2 ?


Thank you so much!!

Unix SuperNewbie

Last edited by prkwan; 11-14-2002 at 01:43 PM..
Sponsored Links
    #2  
Old 11-14-2002
Vishnu Vishnu is offline
Registered User
 
Join Date: Aug 2002
Last Activity: 21 June 2007, 6:24 PM EDT
Location: Marlboro, MA
Posts: 114
Thanks: 0
Thanked 1 Time in 1 Post
all these seem homework questions...

you could have saved a lot of time by doing man on these commands and trying out yourself...

also refer a good book on UNIX shell programming - a very old and good one "UNIX programming by Kerninghan and Pike"

Cheers!
Vishnu.
Sponsored Links
    #3  
Old 11-16-2002
prkwan prkwan is offline
Registered User
 
Join Date: Nov 2002
Last Activity: 19 November 2002, 7:41 AM EST
Location: London UK
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
i got the shell script up and running, but is there anyway to improve it?


if test $# -lt 2 ; then
echo need 2 files ; exit0 fi

elif test -f $1 -a -f $2 ;
then cmp $1 $2 ;
else echo need exist and regular files ; exit0 fi
if test -f $1 -eq -f $2 ;
then wc $1 $2 ; fi
if test -f $1 != $2 ;
then echo File not the same ;
else echo Same file :
fi


how to display file size?
    #4  
Old 11-17-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 20 November 2014, 5:33 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 438 Times in 261 Posts
I'm not as convinced as Vishnu that this thread is a violation of our rule against homework. I would not be teaching that "if test" syntax in the 21st century.

prkwan, at some point, someone got the idea to give the "test" command a second name. The second name is "[". That's right it is just a left bracket. And if the test command sees that it's name is "[", it knows that it should expect that its last parameter should be "]" which can be ignored. So that means that your line:
if test $# -lt 2 ; then
can be rewritten as:
if [ $# -lt 2 ] ; then
Because [ is a command name and because ] is a parameter, both of them must be surrounded by whitespace. Still the new syntax is much easier to read. This change was made in the 70's so I wonder if you may be reading an older book? In any event, switching to the newer syntax is the first change that I would suggest.

Next, you really need to use indentation to make your code more readable. There is no one absolutely best way to indent, you need to find your own style. But judge it by asking yourself how readable is the code. And be consistent... sometimes you have "then" on the same line as the "if" and in other cases it's on the next line.

You haven't told us which shell or which version of unix you are using. But you probably should have a leading line that says "#! /bin/sh" or something. Read this post for the reason.

I'm a little surprised that you are following a "fi" with an "elif", no shell I have tried will accept that.

You consistently have "exit0" where you need "exit 0". And I try to end my scripts with an explicit exit. This is for readability more than anything.
Code:
#! /bin/ksh
if [ $# -lt 2 ] ; then
      echo need 2 files  
      exit 0
elif [ -f $1 -a -f $2 ] ; then 
      cmp $1 $2 ;
else 
      echo need exist and regular files  
      exit 0 
fi
if [ -f $1 -eq -f $2 ] ; then 
      wc $1 $2  
fi
if [ -f $1 != $2 ] ; then 
      echo File not the same 
else 
      echo Same file
fi
exit 0

Now that I can read the code, I see more trouble. The test around your "wc" doesn't make sense. I'm not sure what you had in mind for that. Nor does your final test. But there it like you meant to not have that "-f".


Quote:
how to display file size?
Well "wc" really seems to have that base covered here. The other thing is "ls -l".
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
command line arguments vins_89 Shell Programming and Scripting 2 03-13-2011 04:14 AM
command line arguments ikeQ UNIX for Dummies Questions & Answers 1 05-20-2010 04:32 AM
Command line arguments. Varghese UNIX for Dummies Questions & Answers 1 02-17-2009 06:33 AM
arguments in command line rrs UNIX for Dummies Questions & Answers 6 07-28-2006 07:44 AM
Command Line Arguments roba909 UNIX for Dummies Questions & Answers 7 01-19-2006 01:46 PM



All times are GMT -4. The time now is 03:48 PM.