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.
Related
I am writing a menu.
This is the script I have.
when I type q it dose not echo "done"
what I need to change in the script to get it to echo "done"?
#!/bin/bash
rap '' 2
while true
do
clear
echo "=============================="
echo "menu"
echo "=============================="
echo "Enter 1 to for option 1t: "
echo "Enter q to exit:"
echo -e "\n"
echo -e "Enter your selection"
read answer
case "$answer" in
1) echo "option 1"
echo "Option 1"
done;;
q) exit
echo "done" ;;
esac
echo -e "Enter return to continue \c"
read input
done
exit 0
there were some minor bugs in your code. 1) case does must not have done. 2) exit before calling echo does not make sense.
while true; do
clear
echo "=============================="
echo "menu"
echo "=============================="
echo "Enter 1 to for option 1t: "
echo "Enter q to exit:"
echo -e "\n"
echo -e "Enter your selection"
read answer
case "$answer" in
1) echo "option 1"
echo "Option 1";;
q) echo "done"
exit
;;
esac
echo -e "Enter return to continue \c"
read input
done
exit 0
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
I am making a script where the user selects a number 1-5 and it will loop until the user enters 5. I don't want to use the exit command. I wanted to check to make sure the user doesn't enter anything but 1-5 and if they do display invalid input.
any help would be appreciated
#!/bin/bash
PS3='Please enter your choice: '
options=("1.Move empty files" "2.Check file size" "3.Which files is newer" "4.File check rwx" select opt in "${options[#]}")
while($opt != 5); do
case $opt in
"Option 1")
echo "you chose choice 1"
;;
"Option 2")
echo "you chose choice 2"
;;
"Option 3")
echo "you chose choice 3"
;;
"Option 4")
echo "you chose choice 3"
;;
"Option 5")
break
;;
) echo invalid input;;
You seem confused. I don't even know where to begin correcting whatever misconceptions you have about how this works
In your original code the way you set options is unlikely to do anything useful.
options=("1.Move empty files" "2.Check file size" "3.Which files is newer" "4.File check rwx" select opt in "${options[#]}"
printf '%s\n' "${options[#]}"
This will emit
1.Move empty files
2.Check file size
3.Which files is newer
4.File check rwx
select
opt
in
The select command will not have been executed.
Here's something that does what you seem to want.
options=(
'Move empty files'
'Check file size'
'Which file is newer'
'File check rwx'
)
PS3='Please enter your choice: '
select opt in "${options[#]}" ; do
[[ -n $opt ]] && break || {
echo "invalid input"
}
done
echo "user chose '$opt'"
You could go with a while and case solution and get nearly the same results e.g.:
options=(
'Move empty files'
'Check file size'
'Which file is newer'
'File check rwx'
)
for (( i=0 ; i < ${#options[#]} ; i++ )) ; do
printf '%d) %s\n' $((i+1)) "${options[$i]}"
done
while true ; do
read -p 'Please enter your choice: ' opt
case "$opt" in
[1-5])
opt="${options[$opt]}"
break
;;
*)
echo "invalid input"
;;
esac
done
echo "user chose '$opt'"
But you don't need both and, as you can see, using select is much simpler.
Here is what I am talking about how do I get the script to jump back to the first echo statement if No is selected, Thanks?
echo "Please enter some input: "
read input_variable
echo "You entered: $input_variable"
read -r -p "Is this Correct? [y/N] " response
case $response in
[yY][eE][sS]|[yY]
do_something
;;
*)
{return to first line - echo "Please enter some input: "}
;;
esac
Wrap your prompts in a while loop that you only break out of once the input is confirmed:
while :; do # same as: `while true; do` - keep looping until exited with `break`
echo "Please enter some input: "
read -r input_variable
echo "You entered: $input_variable"
read -r -p "Is this Correct? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
break
;;
esac
done
do_something
Note that not only N, but any input other than what is matched by your case handler will cause the loop to remain active.
use a loop around the whole code:
quit=0
while [ $quit == 0 ]; do
echo "Please enter some input: "
read input_variable
echo "You entered: $input_variable"
read -r -p "Is this Correct? [y/N] " response
case $response in
[yY])
echo "do_something"
quit=1
;;
*)
;;
esac
done
I am not sure what step is next in the process to get the commands to do what I want. I want to select a letter to do a command. Right now it lets you use any letter.
#!/bin/bash
echo "Please select l to list files of a directory, b to backup a file or directory, u to edit a user's password, and x to exit the script"
read $answer
if [ $answer="l" ]; then
printf "Please select folder:\n"
select d in */; do test -n "$d" && break; echo ">>> Invalid Selection"; done
cd "$d" && pwd
ls
fi
Use a case statement
case expression in
pattern1 )
statements ;;
pattern2 )
statements ;;
...
esac
e.g:
case $arg in
l)
printf "Please select folder:\n"
select d in */; do test -n "$d" && break; echo ">>> Invalid Selection"; done
cd "$d" && pwd
ls
;;
cmd1)
echo "Some other cmds line 1"
echo "Some other cmds line 2"
;;
-q) exit;;
*) echo "I'm the fall thru default";;
esac
You can use the select builtin for this, which will let you use numbers for each option instead of letters, but will take care of reading and validating input:
select cmd in \
"List files of a directory" \
"Backup a file or directory" \
"Edit a user's password" \
"Exit";
do
case $cmd in
1) do_list_files ;;
2) do_backup_files ;;
3) do_edit_password ;;
4) exit 0 ;;
esac
done
You can change the prompt by setting the PS3 variable (e.g. PS3="Your choice? ")