|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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 ;(
Last edited by radoulov; 01-25-2013 at 03:35 PM.. |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Code:
Expectedargs = 1If [ $# -ne $expectedargs ]
Then
Echo "usage: 'basename "
Exit 1
FiThat should work for you Last edited by Scott; 01-26-2013 at 06:50 AM.. Reason: Removed URL |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
This should fulfill your requirements Code:
#!/bin/sh if [ "$#" -eq 1 ]; then path="$1" else file="$1" path="$2" fi exit 0 |
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
He is asking Syntax error: "else" unexpected (expecting "then")
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 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 |
|
|