Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 03:35 PM..
Sponsored Links
    #2  
Old Unix and Linux 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 06:50 AM.. Reason: Removed URL
Sponsored Links
    #3  
Old Unix and Linux 01-25-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 25 April 2015, 7:29 PM EDT
Location: France
Posts: 319
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 Unix and Linux 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 Unix and Linux 01-25-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 25 April 2015, 7:29 PM EDT
Location: France
Posts: 319
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 Unix and Linux 01-25-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 April 2015, 11:01 AM EDT
Location: Saskatchewan
Posts: 20,522
Thanks: 880
Thanked 3,622 Times in 3,386 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 12:04 PM
Check if passed arguments is users testman84 Shell Programming and Scripting 3 10-24-2010 04:02 PM
read arguments from shell DNAx86 Programming 1 05-11-2008 11:56 AM
How to check the number of command line arguments? prkwan Shell Programming and Scripting 3 11-17-2002 12:57 PM
Arguments to a shell program csvenkata UNIX for Advanced & Expert Users 3 10-23-2001 09:36 AM



All times are GMT -4. The time now is 11:33 AM.