How can I pass a parameter to a cli-script?
Today I had to change a system-property's value from "test" to "test.tmp".
I successfully used the following cli command:
system-property=my.mdb.destination/:write-attribute(name=value,value=test.tmp)
Tomorrow I will have to revert this value from "test.tmp" to "test".
How can I pass the value to my cli script and how do I use this parameter in the script?
If it is not possible to pass parameters can I use environment variables of the operating system instead?
How would you do this?
Thank you,
Mathias
Responses
I recommend investing in a book on Bash Scripting and the following is a good place to start
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/
Here is a quick example though - you would likely want to use ${1} in your script
#!/bin/bash
echo "\$0 : $0"
echo "\$1 : $1"
echo "\$2 : $2"
echo "\$@ : $@"
exit 0
[jradtke@cypher ~]$ ./test.sh foo bar
$0 : ./test.sh
$1 : foo
$2 : bar
$@ : foo bar
You would probably want/need to add some additional logic to the script that makes the script fail if it was executed without the value. That way you won't run the script with a blank value.
if [ $# -lt 1 ]; then exit 9; fi
system-property=my.mdb.destination/:write-attribute(name=value,value=${1})
Also - if you need to replace the value in your script (NOTE: it would have to be the ONLY instance of the value in the file, else this will replace ALL instances)
sed -i -e 's/test.tmp/test/g' file_name.txt
James was pretty close in his answer, a Bash scripting book would have the answer ... kinda ...sorta ...
Here is the method I use to create dynamic CLI. The 'deploy' CLI command can't really be passed on the command line because it contains spaces. This method provides a means to pass arguments into an 'interactive' CLI session. Note: 'launchCLI.sh' is a script we use to launch the CLI tool:
< skipping code to validate parameters and setup environment >
$SCRIPTDIR/launchCLI.sh <<EOT
deploy $WARFILE $*
quit
EOT
Result=$?
if [ $Result -ne 0 ] ; then
echo Failed deploying $WARFILE $GROUPNAME >>$LOGNAME 2>&1
echo Aborting
exit $Result
fi
What this does is use stdin redirection (that's the '<<EOT .... EOT' part). You can found out more in bash scripting books.
Note ... if CLI authentication is turned on, the username and password must be entered as the first two lines after <<EOT
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
