How to check arguments in shell??? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to check arguments in shell???

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-25-2013
Manueldo Manueldo is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 25 January 2013, 4:24 PM EST
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
How to check arguments in shell???

for example I have
make [file] target
file is optional. So can I check whether there is [file] or no? I tried


Code:
if test $# -eq 1 
then
path=$1
else path=$2
fi

But it doesnt work properlu ;(

Moderator's Comments:
Please use code tags next time for your code and data.

Last edited by radoulov; 01-25-2013 at 04:35 PM..
Sponsored Links
    #2  
Old 01-25-2013
Axiomfinity Axiomfinity is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 25 January 2013, 2:51 PM EST
Location: Sacramento, ca.
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Linux Bash check args


Code:
Expectedargs = 1If [ $# -ne $expectedargs ] 
 Then
         Echo "usage: 'basename "
          Exit 1
Fi


That should work for you

Last edited by Scott; 01-26-2013 at 07:50 AM.. Reason: Removed URL
Sponsored Links
    #3  
Old 01-25-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 16 December 2014, 3:00 PM EST
Location: France
Posts: 318
Thanks: 19
Thanked 81 Times in 76 Posts
This should fulfill your requirements

Code:
#!/bin/sh

if [ "$#" -eq 1 ]; then
	path="$1"
else
	file="$1"
	path="$2"
fi

exit 0

    #4  
Old 01-25-2013
Manueldo Manueldo is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 25 January 2013, 4:24 PM EST
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
By echo wanted to check...Does not work still...

Code:
#!/bin/sh
if [ "$#" -eq 1 ] then
target=$1
echo $target

else makefile='$1'
target=$2
echo $makefile
$target

fi
exit 0

Sponsored Links
    #5  
Old 01-25-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 16 December 2014, 3:00 PM EST
Location: France
Posts: 318
Thanks: 19
Thanked 81 Times in 76 Posts

Code:
#!/bin/sh
if [ "$#" -eq 1 ] then
    target="$1"
    echo "$target"
else
    makefile="$1"
    target="$2"
    echo "$makefile\n$target"
fi
exit 0

Sponsored Links
    #6  
Old 01-25-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 December 2014, 2:25 AM EST
Location: Saskatchewan
Posts: 20,043
Thanks: 856
Thanked 3,463 Times in 3,233 Posts
That is missing an important semicolon or line break.

Also, 'echo' does not work that way.


Code:
if [ "$#" -eq 1 ]; then
    target="$1"
    echo "$target"
else
    makefile="$1"
    target="$2"
    printf "%s\n" "$makefile" "$target"
fi
exit 0

Sponsored Links
    #7  
Old 01-25-2013
Manueldo Manueldo is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 25 January 2013, 4:24 PM EST
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
He is asking Syntax error: "else" unexpected (expecting "then")
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
Shell Scripting with Arguments dw15 Homework & Coursework Questions 4 08-15-2012 01:04 PM
Check if passed arguments is users testman84 Shell Programming and Scripting 3 10-24-2010 05:02 PM
read arguments from shell DNAx86 Programming 1 05-11-2008 12:56 PM
How to check the number of command line arguments? prkwan Shell Programming and Scripting 3 11-17-2002 01:57 PM
Arguments to a shell program csvenkata UNIX for Advanced & Expert Users 3 10-23-2001 10:36 AM



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