0

Hi can someone please help me with an infinite loop that is to take new arguments. Is there a read command that each space as a new argument like the command line does. all seem to set it all to one big argument

I have a script, when called it enters an infinite loop. The first argument will determine a case to call another script, the second argument will then be sent to that script. I then want to loop around again and ask the user for two arguments again in the same line. The first determining the case and using the second...

However I want these to be in the one line as if it was the command line. I believe the read command will set the whole line to a variable while i want to do $1 and $2 again.

I provided a little code to describe my issue, thank you in advance

I basically need a read command that will split into arguments like the command line.

while true :
do

        # make decision using case..in..esac 
        choice = $1
        case $choice in
                create)

                        ./create.sh $2
                        ;;
                add)
                        ./add.sh $2     

                        ;;
                    *)
                        echo "Error: Invalid option..." 
                        ;;
        esac
done
5
  • 1
    Please check your code at shellcheck.net first. Commented Oct 28, 2016 at 18:50
  • Updated code, removing warnings from shellcheck.net Commented Oct 28, 2016 at 18:52
  • 3
    @AshutoshJindal So you changed the code in this question, making it behave differently from what OP actually uses? Commented Oct 28, 2016 at 18:55
  • "I believe the read command will set the whole line to a variable while i want to do $1 and $2 again." Try read one two. Commented Oct 28, 2016 at 18:57
  • Kakawak, changes made by my edit removed these warnings. Check if you are happy with these please? If not, revert my edit. Commented Oct 28, 2016 at 19:02

2 Answers 2

2

You want to "ask the user for two arguments." There are at least three ways, listed below. I think you want to put any of these after the do and before the case.

  1. Like @John1024 said,

    read choice second_arg
    

    will fill in $choice and $second_arg. Edit use choice directly and you won't need choice="$1".

  2. This sequence:

    read
    set -- $REPLY
    

    will replace $1, $2, ... with the words from the command line. Then you can still use choice="$1" like you have it.

  3. To keep the command line parameters and be more general,

    read -a inputs
    

    will read the words into the array inputs, so you can reference them as ${inputs[0]}, ${inputs[1]}, ... for however many inputs there are. Then you can use

    choice="${inputs[0]}"
    

    rather than choice="$1".

Sign up to request clarification or add additional context in comments.

Comments

0

Hope this helps.

while true :
do
    echo -n "Enter Your Choice  > "
    read text

    arg1=$(echo "$text" | awk '{print $1}')
    arg2=$(echo "$text" | awk '{print $2}')
    # make decision using case..in..esac 
    choice="$arg1"
    case $choice in
            create)

                    ./create.sh $arg2
                    ;;
            add)
                    ./add.sh $arg2    
                    ;;
                *)
                    echo "Error: Invalid option..." 
                    ;;
    esac
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.