0

I am trying to make a shell script that works as an ordering system. I have started with the first steps but it does not take the input I am not sure of what I am doing wrong. I have attached an image of what end result should be. What is the next step I should take and what should I begin to researchenter image description here

#!/bin/bash

clear
echo "orderBeds"
read -p "Please Enter you choice (Quit/Order)" order
if [$order -e Order|order]
then
    echo "Please enter you name?"
elif [$order -e Quit|quit]
then 
     exit 
fi
done
4
  • "Does not take the input" is not a good description of the error. But -e is for testing the existence of files; to test the equality of two strings, use ==. Commented Apr 17, 2020 at 17:26
  • Hi I tried your suggestion, but this has given me a new error on line 6 and 9 which is "command not found". what have I done wrong here? Commented Apr 17, 2020 at 17:35
  • 1
    if [$order -e Order|order] will attempt to execute a command named [$order with arguments -e and Order and pipe the output to a command named order]. That is almost certainly not what you want. Commented Apr 17, 2020 at 17:38
  • 2
    Actually, the POSIX way is a single "=" sign; but "==" will work on the most recent shells. Commented Apr 17, 2020 at 17:49

2 Answers 2

2

I'll start giving some general advice.


1) [ is a command. That means you probably don't want to expand a variable just next to it without separating them with white spaces.

2) If you will check against more than one option, use the case construct. Apart from giving you the chance of a better structure, you'll be able to use globbing expressions as options to match against.


That said, let's rewrite your code:

#! /bin/bash

clear
echo "orderBeds"

read -p "Please Enter your choice (Quit/Order)" order

case "$order" in
    [Oo]rder)
        read -p "Please enter your name: " name
        echo "$name placed an order."
        break
    ;;
    [Qq]uit)
        exit
    ;;
esac
Sign up to request clarification or add additional context in comments.

1 Comment

That's a nice one :)
1

the -e flag is for numerical equivalency.

Here is a corrected bash script to get you started:

#!/bin/bash
clear

echo "orderBeds"

read -p "Please enter your choice (Quit/Order) " order

if [ $order == "order" ] || [ $order == "Order" ]
 then
  read -p  "Please enter your name " name
  echo "$name placed an order"

elif [ $order == "quit"] || [ $order == "Quit" ]
 then
  exit
fi

Note the use of == instead of -e and the separation of the or clauses.

3 Comments

Thanks for the advice, I've tested this out and the first function to order seems to work but the function to quit does not, I will do some additional testing to see what changes need to be made. The error is ( line 13: [: missing `]' )
@KevinMedjiako You also need a space before the ].
@KevinMedjiak Walter is correct. I updated the script (I had a typo). Bash cares about things like that.

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.