I have a Bash script that needs to call a command that will require quotes for one of it's arguments. When I run the command
myTimeCommand --time="2018 04 23 1100" -o /tmp/myOutput ~/myInput
from the command line, it works fine. In my script, I want to pass "2018 04 23 1100" as a parameter and send it directly to the command.
myScript --time="2018 04 23 1100"
But, when I try this, I get an error message that
""2018" is not a valid time.
I'm using getopt
to parse the parameters. Here's my script
OPTIONS=t:
LONGOPTIONS=time:
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "$0" -- "$@")
echo ${PARSED}
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
# now enjoy the options in order and nicely split until we see --
while true; do
echo "option: ${1}"
case "$1" in
-t|--time)
timeBase="$2"
timeBase="--time=\"${timeBase}\""
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
echo ${timeBase}
myTimeCommand ${timeBase} -o /tmp/myOutput ~/myInput
EDIT: Removed the runCommand()
function. While this produced some good comments, detracted from the issue at hand - copying quoted parameters to run with a command.
Second example:
Text file (myTest.txt
) with
I have a Bash script that needs to call a command that will require quotes for one of it's arguments. When I run the command
myTimeCommand --time="2018 04 23 1100" -o /tmp/myOutput ~/myInput
from the command line, it works fine. In my script, I want to pass "2018 04 23 1100" as a parameter and send it directly to the command.
When I run grep "command line" myTest.txt
I get a hit for the last line (as expected). When I modify the script to
OPTIONS=s:
LONGOPTIONS=str:
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTIONS --name "$0" -- "$@")
echo ${PARSED}
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
# now enjoy the options in order and nicely split until we see --
while true; do
echo "option: ${1}"
case "$1" in
-s|--str)
sStr="\"$2\""
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
echo "grep ${sStr} myTest.txt"
grep ${sStr} myTest.txt
The echo shows the command as expected, but the actual grep fails
-bash-4.2~>./extText --str="command line"
grep "command line" myTest.txt
grep: line": No such file or directory
How do I call grep with the parameter I passed in ("command line" in this case)?