Frage

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.

War es hilfreich?

Lösung

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" 

Andere Tipps

#!/bin/sh

if [ $1 = "-a" ]; then
    var=$2
fi
echo $var

You shoud be careful of the space between if and [

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top