Bash uses an external program called test to perform boolean tests, but that program is used mostly via its alias [
.
if ( $1 = "-a" )
should become
if [ $1 = "-a" ]
if you use [
or
if test $1 = "-a"
سؤال
I just started writing shell scripts in Unix so, I am a total newbie
I want to read the arguments given when the user run the script ex:
sh script -a abc
I want to read for argument -a
user gave abc
.
My code so far:
if ( $1 = "-a" )
then var=$2
fi
echo $var
I get an error.
المحلول
Bash uses an external program called test to perform boolean tests, but that program is used mostly via its alias [
.
if ( $1 = "-a" )
should become
if [ $1 = "-a" ]
if you use [
or
if test $1 = "-a"
نصائح أخرى
#!/bin/sh
if [ $1 = "-a" ]; then
var=$2
fi
echo $var
You shoud be careful of the space between if and [