- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Parameter checking in scripts
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
тАО05-02-2006 12:00 AM
тАО05-02-2006 12:00 AM
Re: Parameter checking in scripts
your error is using
. myscript
This is executed directly in the running shell.
Check this:
. myscript a b c
echo $1
a
You have to call the script 'in normal way':
myscript a b c
without leading '. '.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2006 12:10 AM
тАО05-02-2006 12:10 AM
Re: Parameter checking in scripts
Both Hein and Peter N. are correct. A dot character followed by a space followed by the script name means *read* or 'source' the file.
This is used to bring environmental variables into your current environment (shell). For instance, do this:
# cat /tmp/env
#!/usr/bin/sh
export VAR1=jrf
export VAR2=hein
export VAR3=peter
Now, compare:
# /tmp/env
echo "${VAR1}, ${VAR2} ${VAR3}"
sh: VAR1: Parameter not set.
...to:
# . /tmp/env
echo "${VAR1}, ${VAR2} ${VAR3}"
jrf, hein peter
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2006 01:19 AM
тАО05-02-2006 01:19 AM
Re: Parameter checking in scripts
#!/usr/bin/sh
set -u
[ $# -eq 1 ] && VAR1=$1 || VAR1=4
echo "VAR1 is $VAR1"
In this case, the test is explicitly for one parameter so anything else (ie, no parameters, two parameters, etc) will leave VAR1=4, otherwise, VAR1 is assigned the first parameter on the command line. As with all interactive scripts, you should assume that monkeys are typing on the keyboard and the value for VAR1 should be tested for validity (ie, not a number, number too large or small, etc)
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2006 01:35 AM
тАО05-02-2006 01:35 AM
Re: Parameter checking in scripts
Using /usr/bin/sh :
$ more /tmp/env
#!/usr/bin/sh
echo "${VAR1}, ${VAR2} ${VAR3}"
$ export VAR1=jrf
$ export VAR2=hein
$ export VAR3=peter
$ /tmp/env
jrf, hein peter
$ . /tmp/env
jrf, hein peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2006 01:52 AM
тАО05-02-2006 01:52 AM
SolutionJRF's solutions was (sorry) a bit misleading, because the variables VARi should NOT exported in the calling shell.
Let us try my way:
cat myscript
#!/usr/bin/sh
v1=${1:-jrf}
v2=${2:-hein}
v3=${3:-nik}
echo v1=$v1 v2=$v2 v3=$v3
Now try:
myscript
v1=jrf v2=hein v3=nik
myscript set your params
v1=set v2=your v3=params
. myscript set your params
v1=set v2=your v3=params
. myscript
v1=set v2=your v3=params
myscript
v1=jrf v2=hein v3=nik
set try this
. myscript
v1=try v2=this v3=nik
But you always get your desired result via
myscript Jong
v1=Jong v2=hein v3=nik
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2006 02:02 AM
тАО05-02-2006 02:02 AM
Re: Parameter checking in scripts
THe 'echo' is run after either running the script or sourcing it. It wasn't part of the script I meant you to run. Eliminating the superflous (yes, misleading) 'export', what I showed was:
#!/usr/bin/sh
VAR1=jrf
VAR2=hein
VAR3=peter
exit
Now, compare:
# /tmp/env #...run the script
# echo "${VAR1}, ${VAR2} ${VAR3}"
sh: VAR1: Parameter not set.
# set #...to see the environmental variables
...to:
# . /tmp/env #...source the script
# echo "${VAR1}, ${VAR2} ${VAR3}"
jrf, hein peter
# set #...now shows VAR1, VAR2 and VAR3 too!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2006 06:46 PM
тАО05-02-2006 06:46 PM
Re: Parameter checking in scripts
And for the parameterquestion this is elegant: v1=${1:-jrf}
Thanks all!
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2006 06:50 PM
тАО05-02-2006 06:50 PM
Re: Parameter checking in scripts
- « Previous
-
- 1
- 2
- Next »