0

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
Bodo
  • 9,287
  • 1
  • 13
  • 29
Michelle
  • 7
  • 3
  • It looks like this is what you're looking for: [process all arguments except the first one in a bash script](https://stackoverflow.com/questions/9057387/process-all-arguments-except-the-first-one-in-a-bash-script) – j4ckofalltrades Nov 22 '20 at 13:52
  • I would suggest not to load fine into loop , rather do some other logic. – vrkansagara Nov 22 '20 at 14:16
  • @j4ckofalltrades does not appear to work but thank you for the suggestion. Appreciated! – Michelle Nov 22 '20 at 14:27
  • The problem description "does not work" is not sufficient. What exactly does not work? Please [edit] your question and show the command line you use to run your script, the input you enter and the actual and expected output or result. If necessary, explain what exactly is wrong. If you get any error message, copy&paste the error message to the question. If a suggested solution does not work, add to your question how exactly you used the suggestion (what exactly you did/changed) and the corresponding result/output/error. – Bodo Nov 22 '20 at 14:37
  • Instead of `while true; do read request...`, just do `while read request; do ...` – William Pursell Nov 22 '20 at 15:51
  • `echo "Error" >&2`. Error messages belong on stderr. – William Pursell Nov 22 '20 at 15:52
  • @Bodo I hope the edit is clearer! – Michelle Nov 22 '20 at 15:55
  • @WilliamPursell unfortunately this did not make a difference - It still returns 'error'. (I have added in the stderr - thanks) – Michelle Nov 22 '20 at 16:01

1 Answers1

1

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
Bodo
  • 9,287
  • 1
  • 13
  • 29