- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Help in script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-23-2009 07:17 AM
тАО02-23-2009 07:17 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-23-2009 07:33 AM
тАО02-23-2009 07:33 AM
Solution[ -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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-23-2009 07:34 AM
тАО02-23-2009 07:34 AM
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
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-23-2009 07:36 AM
тАО02-23-2009 07:36 AM
Re: Help in script
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-23-2009 10:24 AM
тАО02-23-2009 10:24 AM
Re: Help in script
CLIENT=${1:-apple}
DIR=${2:-DR_dir}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-23-2009 11:47 AM
тАО02-23-2009 11:47 AM
Re: Help in script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-23-2009 01:05 PM
тАО02-23-2009 01:05 PM
Re: Help in script
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