HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
cancel
Showing results for 
Search instead for 
Did you mean: 

Help in script

 
SOLVED
Go to solution
Waqar Razi
Regular Advisor

Help in script

I need help in writing a script. Here is what I want to do.

The script runs and passed two command line arguments to the script like:

./script apple DR_dir

Now in the script I want to check if $1 and $2 are passed as the command line arguments. If they are passed, I want to assign the value of $1 to CLIENT and $2 to DIR variable in the script otherwise default CLIENT to apple and DIR to DR_dir.

if ($1 is set)
then
SERVER=$1
else
SERVER=apple
endif

if ($2 is set)
then
DIR=$2
else
DR_dir=DR_dir
endif

Can some one please help me in these if statements?
6 REPLIES
Fredrik.eriksson
Valued Contributor
Solution

Re: Help in script

CLIENT=$1
[ -n $CLIENT ] || CLIENT="apple"

DIR=$2
[ -n $DIR ] || DIR="DR_dir"

This just tests if the condition is true, if it's false then execute whatever is behind ||.

If $DIR contains anything this statement will return true and || is ignored.

-n checks if the string length is non-zero. If it's non-zero, the statement returns true, otherwise false.

Here's a url to a bunch of condition syntaxes.
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html#sect_07_01_01

Best regards
Fredrik Eriksson
Michael Mike Reaser
Valued Contributor

Re: Help in script


set +u
if [ "$1" = "" ];
then
SERVER=apple
else
SERVER="$1"
fi
if [ "$2" = "" ];
then
DIR=DR_dir
else
DIR="$2"
fi

There's no place like 127.0.0.1

HP-Server-Literate since 1979
James R. Ferguson
Acclaimed Contributor

Re: Help in script

Hi Wagar:

One way:

#!/usr/bin/sh
if [ -z "$1" ]; then
SERVER="apple"
else
SERVER=$1
fi
if [ -z "$2" ]; then
DIR="DR_dr"
else
DIR=$2
fi
echo "CLIENT=${CLIENT}"
echo "DIR=${DIR}"

Regards!

...JRF...
Heironimus
Honored Contributor

Re: Help in script

I don't have an HP handy to test, but I think this "default value" syntax is in the current POSIX sh spec and has been in Korn for a while.

CLIENT=${1:-apple}
DIR=${2:-DR_dir}
Doug O'Leary
Honored Contributor

Re: Help in script

Hey;

The default syntax as presented above is definitely the way to go:

$ cat testors
#!/usr/bin/sh

Client=${1:-apple}
Dir=${2:-DR_dir}

printf "%-8s %s\n" ${Client} ${Dir}
$ ./testors
apple DR_dir
$ ./testors pear
pear DR_dir
$ ./testors pear cobbler
pear cobbler

HTH;

Doug O'Leary

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html

Re: Help in script

>otherwise default CLIENT to apple and DIR to DR_dir.

This could be as simple as counting parms:
if [ $# -ge 1 ]; then
SERVER=$1
else
SERVER=apple
fi

>Doug: The default syntax as presented above is definitely the way to go:

Right, Heironimus' solution will work even if -u is set.
And when using that ":", null parms will take the default:
$ ./testors "" ""
apple DR_dir