문제

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 [

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top