Reputation: 17
Currently trying to write a looping case script that matches different cases to execute scripts. However, I am unsure as off how to pass arguments into the cases after the cases have been matched! This is what I have but does not work. I am new to bash so thanks for any help in advance!
Edit:
I have changed to ${@:2}
but when I enter delete text.txt
or open x
, I get Error
returned. I think the program is unable to read the rest of the input.
I run the looping script in the command line -
./loop.sh
I then type
open John
The script open.sh
runs to make a new directory with the name of the following argument. It works manually - I type ./open.sh John
and a directory named John
is created. Here I wish to type open John
which enters the loop, matches the open
case and then puts John
after the command to run the script and make the directory.
If I type ./loop.sh
Then open
it does match it. But cannot run the script because the script requires an argument
#!/bin/bash
while true; do
read request
case $request in
open)
./open.sh "${@:2}"
;;
delete)
./delete.sh "${@:2}"
;;
*)
echo "Error"
exit 1
esac
done
Upvotes: 0
Views: 279
Reputation: 9855
You have to split the command line entered as request
into separate arguments. One way to do this is the set
shell builtin, see e.g. the end of https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
example script:
#!/bin/bash
while true; do
read request
# split into positional parameters $1, $1, ...
set -- $request
# the first word is now $1
case "$1" in
open)
./open.sh "${@:2}"
;;
delete)
./delete.sh "${@:2}"
;;
*)
echo "Error" >&2
exit 1
esac
done
Upvotes: 1