I am working creating a bash shell framework where I need to pass variable number of arguments from one script to another.
Script1.sh
#!/bin/bash
vOut=`sudo -u execuser script2.sh $1 $2 $3`
Script2.sh
ActualScriptName="$2"
Host="$1"
Args="$3"
vOut=`$ActualScriptName -H${HOST} "$Args"
ActualScript3.sh
#!/bin/bash
while getopts ":H:s:e:" OPTION
do
case $OPTION in
T)
HOST=${OPTARG}
;;
s)
START_TIME=${OPTARG}
;;
e)
END_TIME=${OPTARG}
;;
?)
usage
exit
;;
esac
done
echo HOST=$HOST
echo START_TIME=$START_TIME
echo END_TIME=$END_TIME
Now, when I am calling script1.sh:
script1.sh 10.1.1.1 ActualScript1.sh "-s='2015-09-20 02:00' -e='2015-09-20 02:30'"
I am getting output as:
HOST=10.1.1.1
START_TIME='2015-09-20 02:00' -e'2015-09-20 02:30'
END_TIME=
How can I pass this variable number of arguments from script1 for ActualScript1.sh?
You should use "$#" for passing around all the arguments from one script to another and use shift to move positional arguments.
You can have these scripts like this:
script1.sh:
#!/bin/bash
./script2.sh "$#"
script2.sh:
Host="$1"
ActualScriptName="$2"
shift 2
"$ActualScriptName" -H"$Host" "$#"
script3.sh:
#!/bin/bash
while getopts ":H:s:e:" OPTION
do
case $OPTION in
H)
HOST=${OPTARG}
;;
s)
START_TIME=${OPTARG}
;;
e)
END_TIME=${OPTARG}
;;
?)
echo "usage"
#exit
;;
esac
done
echo HOST="$HOST"
echo START_TIME="$START_TIME"
echo END_TIME="$END_TIME"
Related
i'm trying to use getopts to make a command: actu -c [credits to add] to add a value to a variable that is on a .txt file but I don't understand what i'm doing wrong:
#!/bin/bash
set -e
set -u
set -o pipefail
while getopts 'c:' OPTION; do
case "$OPTION" in
c)
c="$OPTARG"
value=$(<cred.txt)
value=$((value+c))
echo "There were added: "$c" credits, total: "$value""
echo $value>cred.txt
;;
?)
echo "actu [-c]" >&2
exit 1
;;
esac
done
shift "$((OPTIND -1))"
Any help would be apreciated!
I would like to put my getopt call into a function so I can make my script a bit more tidy. I've read a few guides Using getopts inside a Bash function but they seem to be for getopts not getopt and cannot get my head round it.
I have the following getopt call at the start of my script
#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------
getopt_results=$( getopt -s bash -o e:h --long ENVIRONMENT:,HELP:: -- "$#" )
if test $? != 0
then
echo "Failed to parse command line unrecognized option" >&2
Usage
exit 1
fi
eval set -- "$getopt_results"
while true
do
case "$1" in
-e | --ENVIRONMENT)
ENVIRONMENT="$2"
if [ ! -f "../properties/static/build_static.${ENVIRONMENT}.properties" -o ! -f "../properties/dynamic/build_dynamic.${ENVIRONMENT}.properties" ]; then
echo "ERROR: Unable to open properties file for ${ENVIRONMENT}"
echo "Please check they exist or supply a Correct Environment name"
Usage
exit 1
else
declare -A props
readpropsfile "../properties/dynamic/dynamic.${ENVIRONMENT}.properties"
readpropsfile "../properties/static/static.${ENVIRONMENT}.properties"
fi
shift 2
;;
-h | --HELP)
Usage
exit 1
;;
--)
shift
break
;;
*)
echo "$0: unparseable option $1"
Usage
exit 1
;;
esac
done
when I put the whole lot in function , say called parse_command_line ()
and call it with parse_command_line "$#"
my script dies because it cannot work out the parameters it was called with. I have tried making OPTIND local as per some of the guides. Any advice? Thanks.
getopt shouldn't be used, but the bash-aware GNU version works fine inside a function, as demonstrated below:
#!/usr/bin/env bash
main() {
local getopt_results
getopt_results=$(getopt -s bash -o e:h --long ENVIRONMENT:,HELP:: "$#")
eval "set -- $getopt_results" # this is less misleading than the original form
echo "Positional arguments remaining:"
if (( $# )); then
printf ' - %q\n' "$#"
else
echo " (none)"
fi
}
main "$#"
...when saved as getopt-test and run as:
./getopt-test -e foo=bar "first argument" "second argument"
...properly emits:
Positional arguments remaining:
- -e
- foo=bar
- --
- hello
- cruel
- world
I found an answer here for a question on counting number of parameters passed argument to a BASH script. I'm interested on the line : ${1?"Usage: $0 ARGUMENT"} where it throws warning if no parameter is given.
Now I would like to invoke a usage function Usage using : ${1?"Usage: $0 ARGUMENT"} but I do not know how to do it. I tried : ${1?Usage} and BASH throws an error on this line. Can some suggest how to invoke a function using this.
The sample script is as below,
#!/bin/bash
function Usage() {
echo "Usage: $0 [-q] [-d]"
echo ""
echo "where:"
echo " -q: Query info"
echo " -d: delete info"
echo ""
}
# Exit if no argument is passed in
: ${1?Usage}
while getopts "qd" opt; do
case $opt in
q)
echo "Query info"
;;
d)
echo "Delete info"
;;
*)
Usage;
exit 1
;;
esac
done
What about this?
function Usage() {
echo "Usage: $0 [-q] [-d]"
echo ""
echo "where:"
echo " -q: Query info"
echo " -d: delete info"
echo ""
}
# Exit if no argument is passed in
: ${1?"$(Usage)"}
Anyhow, I think this is more readable:
if [ $# -lt 1 ] ; then
Usage
fi
Another idea would be to handle it like this:
mode=""
while getopts "qdh" opt; do
case $opt in
q)
mode="Query info"
;;
d)
mode="Delete info"
;;
h)
Usage
exit 0
;;
*)
Usage >&2
exit 1
;;
esac
done
if [ -z "${mode}" ] ; then
Usage >&2
exit 1
fi
you just need pass argument to the bash script.
assume that the bash script's name is hello.sh;
for example:
#!/bin/bash
# script's name is hello.sh
: ${1?"Usage: $0 Argument"}
echo hello,$1
then chmod a+x hello.sh
then we call the script use this :
bash hello.sh yourname
then echo the "hello,yourname"
if you just called use bash hello.sh and no argument for this script
you will get your error message like this Usage: hello.sh Argument
I have a script which should be run as either one of these two:
script.sh -t TYPE
script.sh -t TYPE -f FILE
If it is run without a -t flag I want it to error and exit.
If it is run with a -t flag I want to grab the value and store it in a
variable called "$TYPE" and print "JUST $TYPE"
If it is run with a -f flag I want it grab the value and store it in a variable called
"$FILE" and print "$TYPE and $FILE"
From information and tutorials on both here and the internet generally this is the closest I can get. Can anyone help me put in the second conditional into this existing code?
while getopts ":t:" opt; do
case $opt in
a)
echo "JUST $OPTARG" >&2
;;
\?)
echo "Error - Invalid type argument" >&2
exit 1
;;
:)
echo "Error - No type argument" >&2
exit 1
;;
esac
done
I think you get confused how you should handle command line arguments.
The common way is that the processing of all arguments precedes the actual job of the program/script.
Further more (related to getopts) if an option is appended by a colon, that indicates that the option is expected to have an argument.
Your case statement looks overpopulated too. You don't need to test for a colon and a question mark. The whole testing can be put after the while loop
I would do it like this
#!/bin/bash
unset TYPE
unset FILE
#uncomment if you want getopts to be silent
#OPTERR=0
while getopts "t:f:" opt; do
case $opt in
t)
TYPE=$OPTARG
echo "JUST $OPTARG"
;;
f)
FILE=$OPTARG
;;
esac
done
if ! test "$TYPE" ; then
echo "-t is obligatory"
exit 1
fi
if test "$TYPE" && test "$FILE" ; then
echo "$TYPE and $FILE"
fi
Have a look at this:
TYPE=""
FILE=""
while getopts "t:f:" opt; do
case $opt in
t) TYPE="$OPTARG"
;;
f) FILE="$OPTARG"
;;
esac
done
if [ -z "$TYPE" ]; then
echo "No -t. Bye."
exit 1 # error
else
if [ -n "$FILE" ]; then
echo "$TYPE and $FILE"
else
echo JUST $TYPE
fi
fi
#!/bin/bash
#if present -a flag then print this echo
#echo "A";
#if -b is present then print b
#echo "B"
#and if -c 10 present how can I read this value '10' ?
above is how I want to look my script
and I want to be able to start it like this
myscript.sh -a -b -c 10
or
myscript.sh
or
myscript.sh -a -c 10
or
myscript.sh -c 10
and so on
Type 'man getopt' at your shell, and follow the instructions.
Use getopts like this:
arg=-1
while getopts "c:ab" optionName; do
case "$optionName" in
a) echo "-a is present";;
b) echo "-b is present";;
c) arg="$OPTARG"; echo "-c is present [$arg]";;
esac
done
You may have a look at getopts .
The following example is taken from http://wiki.bash-hackers.org/howto/getopts_tutorial
#!/bin/bash
while getopts ":a" opt; do
case $opt in
a)
echo "-a was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
#!/bin/bash
for var in "$#"
do
echo "$var"
http://osr600doc.sco.com/en/SHL_automate/_Passing_to_shell_script.html