I'm trying to make some application but it's too difficult to describe it here so I simplify my problem into simpler ones.
I just want to make a script that takes two arguments: time and command. It parses arguments with getopts, waits some time and executes the given command.
I've tried to use various combinations of double-quotes, single-quotes and no quotes at all, use bash arrays (I think so) and look for similar questions but it didn't help.
script.sh
time=1
command=""
while getopts "t:x:" opt; do
case $opt in
t) time=$OPTARG;;
x) command=( "$OPTARG" );; # store the command in a variable
esac
done
sleep $time
"${command[@]}" # execute the stored command
test.sh
# script to test arguments passing
echo "1$1 2$2 3$3 4$4"
The results of executing the script should be the same as the execution of command passed in -x argument.
$./script.sh -x "./test.sh a 'b c'"
1a 2'b 3c' 4 # actual results
$./test.sh a 'b c'
1a 2b c 3 4 # expected results
change "${command[@]}"
to eval "${command[0]}"