Trying to get user input shell script programming - shell

I'm trying to make a shell script (.sh) on my mac, but somehow I can't get the 'read' function to work. This is my code:
READ -p "What now? " var
ECHO $var
ECHO " "
if [ "$var" == "getfiles" ]
then
ECHO "Test1"
elif [ "$var" == "help" ]
then
ECHO "Test2"
else
ECHO "Please enter a valid command (type 'help' for a list of valid commands)"
fi
As you can see, I'm trying to get user input by using 'READ -p "What now? " var'. However, no matter what I type, it returns an empty variable. I've searched everywhere, but no one seems to have the same problem as I do. I checked the code a thousand times, but can't find any irregularities. Can anyone see what I'm doing wrong? Thanks in advance.

Use lowercase read and echo.
echo "${var}"is better than echo $var

Related

why is a bash read prompt not appearing when a script is run?

I have a bash script that prompts the user for different information based on what they're trying to do. The prompts are usually done with read -p. Usually it works just fine, the user sees what is being asked, enters what they need to enter, and everything does what it needs to do.
See the following (sanitized) snippet of a function in the script:
#!/bin/bash
function_name() {
if [ "$this_value" == "default" ];then
echo "Value set to default."
read -p "Enter desired value here: " desired_value
desired_value=${desired_value^^}
if [ "${#desired_value}" != 3 ] ;then
echo "$desired_value is an invalid entry."
exit 1
fi
if [ "$desired_value" != "$(some command that returns something to compare against)" ];then
echo "$desired_value is an invalid entry."
exit 1
fi
read -p "You entered $desired_value. Is this correct? [y/N] " reply
reply=${reply,,}
case "$reply" in
y|yes)
$some command that does what I want it to do
;;
*)
echo "User did not enter yes"
exit 1
;;
esac
fi
}
Usually the Enter desired value here and is this correct? lines appear just fine. But in a few instances I've seen, for some reason the read prompt is just blank. A user will see the following:
./script.bash
##unrelated script stuff
##unrelated script stuff
Value set to default.
user_entered_value_here
User did not enter yes. Exiting.
This is a real example that just happened that finally made me come here to ask what is going on (and I modified appropriately to make it an SO post).
What's happening is these two blank lines appear instead of the read -p text. For the first one, the user entered user_entered_value_here because they already know what is supposed to be entered there even without the read prompt. The second one, the Y/N prompt, they don't know, so they see it apparently hanging, and hit Enter instead of y, causing it to trigger the * case option.
I don't understand why the read -p text is not appearing, and especially why it's appearing for most users but not all users. I suspect there's some kind of environmental setting that causes this, but for the life of me I can't figure out what. This is being run only on RHEL 6.2, under bash 4.1.2.
I looked at the man of bash to catch some kind of detail about the read built-in. It is specified that -p option displays the "prompt on standard error, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal".
Let's consider the simple script input.sh:
#!/bin/bash
read -p "Prompt : " value
echo The user entered: "$value"
Example of execution:
$ ./input.sh
Prompt : foo
The user entered: foo
If stderr is redirected:
$ ./input.sh 2>/dev/null
foo
The user entered: foo
If the input is a pipe
$ echo foo | ./input.sh
The user entered: foo
If the input is a heredoc
$ ./input.sh <<EOF
> foo
> EOF
The user entered: foo
Rewrote your script with shell agnostic grammar and fixed some errors like comparing the string length with a string comparator != = rather than a numerical comparator -ne -eq:
#!/usr/bin/env sh
this_value=default
toupper() {
echo "$1" | tr '[:lower:]' '[:upper:]'
}
function_name() {
if [ "$this_value" = "default" ]; then
echo "Value set to default."
printf "Enter desired value here: "
read -r desired_value
desired_value=$(toupper "$desired_value")
if [ "${#desired_value}" -ne 3 ]; then
printf '%s is an invalid entry.\n' "$desired_value"
exit 1
fi
if [ "$desired_value" != "$(
echo ABC
: some command that returns something to compare against
)" ]; then
echo "$desired_value is an invalid entry."
exit 1
fi
printf 'You entered %s. Is this correct? [y/N] ' "$desired_value"
read -r reply
reply=$(toupper "$reply")
case $reply in
'y' | 'yes')
: "Some command that does what I want it to do"
;;
*)
echo "User did not enter yes"
exit 1
;;
esac
fi
}
function_name

Making a shell script but the script is not taking any input

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 research
#!/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
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
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.

bash let variable default to user input

I want to write a script for both interactive and batch use. If arguments are not provided the script will ask for the users input.
Unlike here the user shouldn't be bothered if the variable is already defined by arguments.
Using parameter expansion I tried this:
${FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"}
... but I always get a command not found error.
Any suggestions?
I would write
# earlier in program, $FILE may or may not be defined
if [[ -z $FILE ]]; then
read -p "Please provide the file: " FILE
fi
If you really want to cram it into one line, use the : command and the assignment expansion syntax
: ${FILE:=$(read -p "file?"; echo "$REPLY")}
Note, don't use ALL_CAPS_VARS: someday you'll use PATH and wonder why your script is broken.
It should be
FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"
You are trying to execute the command instead of initialization
${FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"}
Please provide the file:
Mahesh
-bash: Mahesh: command not found
$ FILE="$( echo "Please provide the file:" >&2 ; read a; echo $a )"
Please provide the file:
mahesh
$ echo $FILE
mahesh

read y or n to confirm deletion unix shell

I have trying to have a unix shell script ask the user whether they want to delete an item.
I have the following code
I keep getting the following error
./menu.sh 73: [: missing ]
echo "Confirm deletion: (y)es or (n)o: "
read confirmDeletion
if [ "$confirmDeletion"="y"];
then
echo "YES"
else
echo "NO"
pause
fi
I cannot seem to work out what is wrong
Any help will be appreciated
Thanks
You need spaces around the = operator:
if [ "$conformDeletion" = "y" ];
and before the ]

Bash Scripting General Questions (conditionals and variable passing)

I'm rather new to bash scripting, and Google isn't as useful as I'd like for this. I'm just playing around with a little password entry program in my .bash_profile and have something like this:
read PASSWORD
if $PASSWORD != 'pass'; then
echo "wrong. exiting"
exit
fi
Unfortunately, this doesn't work. I get these errors (darwin on 10.6)...
EDIT Sorry about this posting. My browser crashed and I didn't even realize this posted. I ended up figuring it out on my own – again sorry. But thanks for the answers!
You are missing square brackets. The if line should be:
if [ $PASSWORD != 'pass' ]; then
or even better:
if [ "$PASSWORD" != 'pass' ]; then
Which will avoid failure if $PASSWORD is empty.
Try:
read PASSWORD
if [ "x$PASSWORD" != "xpass" ] ; then
echo "Wrong. Exiting."
exit 1
fi
exit 0
You might like to know about two options to the read command:
-p string
Display a prompt without a trailing newline
and
-s
Silent mode. The characters typed by the user are not echoed to the screen.
So for prompting for a password you could do:
read -sp "Please enter your password: " PASSWORD
echo
This is an excellent resource.
use case/esac construct
read -p "enter: " PASSWORD
case "$PASSWORD" in
"pass") echo "ok;;
* ) echo "not ok";;
esac
Edit: For Dennis's qns
x=10
y=5
z=1
options=3
expression="$((x> y)) $((y> z)) $((options<=4))"
case "$expression" in
"1 1 1")
echo "x > y and y>z and options <=4";;
*) echo "Not valid";;
esac

Resources