How do i stop echo in loop and re-run case statement? - bash

Pretty new to bash scripting , I was learning how to use multiple cases and inputs in case statement.
#!/usr/bin/env bash
echo "Choose from 1,2"
read -p "Enter here : " input1
echo "Choose from a,b"
read -p "Enter here : " input2
while true; do
case $input1+$input2 in
1+a )
echo "You have chosen 1 and a"
break;;
1+b )
echo "You have chosen 1 and b"
break;;
2+a )
echo "You have chosen 2 and a"
break;;
2+b )
echo "You have chosen 2 and b"
break;;
*+* )
echo "Invalid input";;
esac
done
PS \ I know i can use single wildcard asterisk in to cover remaining possibilities , i just used it twice for times when i need wildcard only for single case and not both of them together.
Currently
Everything else works fine , but if i try to invoke invalid output by entering random numbers , it is infinitely spamming - Invalid output .
Expectation
if user inputs anything which is not covered by case statement , echo "invalid output" only once and rerun the case statement ( ie echo those 2 questions again )
Can someone please suggest how do i fix it

Thanks to all the suggested comments , i came up with this
while true; do
echo "Choose from 1,2"
read -p "Enter here : " input1
case $input1 in
1|2 )
break;;
* )
echo "Enter Valid Input"
esac
done
while true; do
echo "Choose from a,b"
read -p "Enter here : " input2
case $input2 in
a|b )
break;;
* )
echo "Enter Valid Input"
esac
done
case $input1+$input2 in
1+a )
echo "You have chosen 1 and a";;
1+b )
echo "You have chosen 1 and b";;
2+a )
echo "You have chosen 2 and a";;
2+b )
echo "You have chosen 2 and b";;
esac

Related

Bash confirm input or re enter

I have a very simple BASH script that asks the user if they would like to enter some input. The response is y or n
If they enter n the script exits.
If they enter y then they are asked some questions. At the end I want to ask them to confirm the input.
If it's correct we'll carry on, if not I want to return them to the questions to re enter the information.
So far I've got:
while true; do
echo -e "Enter details ? "
read yn
case $yn in
[Nn]* ) exit;;
[Yy]* )
echo -e "description"
read desc
echo -e "Address"
read address
* ) echo "Please answer yes or no.";;
esac
done
This seems to work fine, so I've tried to add the confirmation.. but thats not working.
while true; do
echo -e "Enter details ? "
read yn
case $yn in
[Nn]* ) exit;;
[Yy]* )
echo -e "description"
read desc
echo -e "Address"
read address
echo -e "$desc - $address"
while true; do
echo -e "\nAre these details correct ? "
read conf
case $conf in
[Nn]* ) // return to enter details //
[Yy]* ) // carry on with the details entered // break;;
esac
done
* ) echo "Please answer yes or no.";;
esac
done
The aim is as follows
The user selects y and enters description and address. The page shows them a summary of what they've entered and asks them are these details correct ? if they are we move on and the script progresses. The the input is wrong then the script returns them to enter description followed by enter address.
Can someone advise how to do this.
Thanks
You could add one more loop level and use the optional level indicator of break [n]:
while true; do
echo -e "Enter details ? "
read yn
case $yn in
[Nn]*) exit;;
[Yy]*)
while true; do
echo -e "description"
read desc
echo -e "Address"
read address
echo -e "$desc - $address"
while true; do
echo -e "\nAre these details correct ? "
read conf
case $conf in
[Nn]* ) break 1;;
[Yy]* ) break 3;;
esac
done
done;;
*) echo "Please answer yes or no.";;
esac
done
Don't use case for this.
Script for example for you:
#!/bin/bash
while true; do
echo -e "Enter details ?(y/n)"
read yn
if [[ $yn == "y" ]];then
echo -e "description"
read desc
echo -e "Address"
read address
echo -e "$desc - $address"
while true; do
echo -e "\nAre these details correct ? (y/n)"
read conf
if [[ $conf == "y" ]];then
echo "All done"
break
elif [[ $conf == "n" ]];then
break
else
continue
fi
done
elif [[ $yn == "n" ]];then
break
else
continue
fi
There you go:
DONE=""
while [ "$DONE" != "true" ]; do
echo -e "Enter details ? "
read yn
case $yn in
[Nn]* ) exit;;
[Yy]* )
echo -e "description"
read desc
echo -e "Address"
read address
echo -e "$desc - $address"
while true; do
echo -e "\nAre these details correct ? "
read conf
case $conf in
[Nn]* )
break;;
[Yy]* )
echo "doing something with your values"
DONE="true"
break;;
esac
done
;;
* ) echo "Please answer yes or no.";;
esac
done

How to put break statement inside a condition which is inside a switch case in korn script

I am writing a korn script to process some fixed/user provided input. I am not able to run this script as I am getting syntax error `)' unexpected. I am thinking that it is because I am using a break inside the if statement, where the if is inside a switch case. I have just picked up scripting last week and would really appreciate any help on this.
P.S There is a reason for using korn script.
#!/usr/bin/ksh93
typeset -A fileset_list #fileset_list is associative
fileset_list=([All filesets]="A B C D E"
[A]=AA
[B]=BB
[C]="CC CCC CCCC CCCC"
[D]=DD
[E]="EE EEE EEEE EEEE EEEEE"
)
fileset="All filesets"
echo "Recent update has found following unsupported filesets on the system:\n${fileset_list["All filesets"]}"
echo "Do you want to delete all the listed filesets along with their dependencies Y/N"
while true; do
read yn
case $yn in
[Yy]* )
set -A delete_list ${fileset_list["All filesets"]}
uninstall_fun
break;;
[Nn]* )
echo "Do you want to delete the partial list Y/N"
while true; do
read y_n
case $y_n in
[Yy]* ) echo "Enter the space separated filesets from the above list for deletion"
read user_list
echo "You have entered $user_list\nIs the list correct Y/N"
read selection
if [[ $selection == [Yy]* ]]
then
set -A delete_list $user_list
uninstall_fun
break;; #<<<<ISSUE
else
echo "Do you want to re-enter list Y/N" #<<<<<need this to go back and read y_n
fi
# break;;
[Nn]* )
break;;
* ) echo "Please answer yes(y) or no(n).";;
esac
done
break;;
* ) echo "Please answer yes(y) or no(n).";;
esac
done
uninstall_fun(){
echo "In uninstall_fun"
}
The ;; is used at the end of each case block. You had one in the middle of the if statement, but the case block is not finished yet.
The line with break;; # <<<<ISSUE should become break # without ;;,
and the line # break;; should be ;;.
[Yy]* ) echo "Enter the space separated filesets from the above list for deletion"
read user_list
echo "You have entered $user_list\nIs the list correct Y/N"
read selection
if [[ $selection == [Yy]* ]]
then
set -A delete_list $user_list
uninstall_fun
break # Without ;;
else
echo "Do you want to re-enter list Y/N" #<<<<<need this to go back and read y_n
fi
;; # Needed here
[Nn]* )

bash: preventing a redirected function's errors from mixing with output

Want to get user's input from a function. However, the prompt ("Please answer y or n." in this case) is also included in the return value.
#!/bin/bash
input() {
while true; do
read -p "input y/n: " yn
case $yn in
[Yy]* ) yn="y"; break;;
[Nn]* ) yn="n"; break;;
* ) echo "Please answer y or n.";;
esac
done
echo $yn
}
val=$(input)
echo "val is: $val"
If input an error value first and here is the results:
input y/n: other
input y/n: y
val is: Please answer y or n.
y
Thanks.
Echo your error to stderr (FD 2), not stdout (the default, FD 1):
echo "Please answer y or n." >&2
Better use a common global variable to pass values between the function and the caller. It's more efficient than summoning subshells with command substitution.
#!/bin/bash
input() {
while true; do
read -p "input y/n: " __
case "$__" in
[Yy]* ) __="y"; break;;
[Nn]* ) __="n"; break;;
* ) echo "Please answer y or n.";;
esac
done
}
input; val=$__
echo "val is: $val"

unexpected fi in bash script

This is the script and I keep getting an unexpected fi error. What am I missing? .. (I started using [] for the if statement but since I'm using this command I deleted the [] .. Is it ok this way?)
if type "java" 2>&1;
then
echo "All ok . . . ";
exit
else
read -n1 -r -p "Yo need to install"
while true; do
echo "Want to install??"
select yn in "y" "n"; do
case $yn in
y ) echo "Installing here..."; break;;
n ) echo "Ok... stopping..."; exit;;
esac
done
exit
fi
thanks!
while ends with done, not exit. Try this:
if type "java" 2>&1;
then
echo "All ok . . . ";
exit
else
read -n1 -r -p "Yo need to install"
while true; do
echo "Want to install??"
select yn in "y" "n"; do
case $yn in
y ) echo "Installing here..."; break;;
n ) echo "Ok... stopping..."; exit;;
esac # <-- ending `case`
done # <-- ending `select`
done # <-- while ends with `done`, not `exit`!
fi # <-- ending `if`
You have an exit before the last fi; I suppose that should be a done.

Having unix code problem?

Im having a problem with my unix code
#!/bin/bash
while : ; do
echo "SELECT OPTION"
echo "-------------"
echo "1- Create username"
echo "2- Create password"
echo "3- Delete username"
echo "4- Exit"
read -p "enter option 1 2 3 or 4:" option
case option in
1) read -p "Enter username:"
adduser $REPLY && echo "Username successfully entered" ;;
2) passwd && "Password successfully entered" ;;
3) read -p "Enter user to be deleted: "
deluser $REPLY && echo "User deleted" ;;
4) exit ;;
*) continue ;;
esac
done
Ok the select option works, but if i type in 1 or 2 as an option to create username or password, its takes me back to the select options again. Whatever i do press it will always show the select option
Can someone help me run this code in unix using bash.
Thank You
Try $option - option gets interpreted as a string.

Categories

Resources