Linux Shell-Script code is not executing? - shell

This code is not running it shows error:- whl_case_lop1: 25: whl_case_lop1: Syntax error: "elif" unexpected (expecting ";;")
#if [ -z $1 ]
#then
#rental=****unknown item****
#elif [ -n $1 ]
#then
#rental=$1
#else [ -n $2 ]
#then
#rent=$2
#fi
echo "1. Vehicle_on_rent"
echo "2. Living_house"
echo -n "choose option [1 or 2]? "
read cate;
if [ $cate -eq 1 ];
then
echo "Enter your vehicle type"
read rental
case $rental in
"car") echo "for $rental in 20/km";;
"van") echo "for $rental in 15/km";;
"jeep") echo "for $rental in 10/km";;
"bike") echo "for $rental in 5/km";;
*) echo "We can't find $rental for your vehicle"
elif [ $cate -eq 2 ];
then
echo "Enter your Room requirement"
read rent
case $rent in
"1BHK") echo "for $rent is 10k";;
"2BHK") echo "for $rent is 15k";;
"3BHK") echo "for $rent is 20k";;
*) echo "we can't find $rent for your Requirement"
else
echo "Please check your requirements! Maybe you choose a wrong option"
fi

You need a ;; in your case's *) branch, and the case statement ends with the esac keyword: https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs
You might want to use the select statement, to restrict the user's responses:
PS3="Enter your vehicle type: "
select rental in car van jeep bike; do
case $rental in
car) echo "for $rental in 20/km"; break ;;
van) echo "for $rental in 15/km"; break ;;
jeep) echo "for $rental in 10/km"; break ;;
bike) echo "for $rental in 5/km"; break ;;
*) echo "We can't find $rental for your vehicle" ;;
esac
done
Indent your code to aid maintainability.

Related

bash menu script exit

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

Bash script to chose based on user input

Problem:
I need to make this bash script to choose based on the user inputs. Example how can i add choices? such that user just select from 1 to 3 and that is set in variable CLUSTER_NAME.
choices are test.com, try.com and me.com
Script
#!/bin/bash
sops_ops() {
sops --version
if [ "$?" -eq "0" ]; then
echo "proceed sops ops"
else
echo "check sops binary"
fi
read -p 'Enter cluster_NAME: = ' CLUSTER_NAME
test_environment="test.com"
test1_environment="test1.com"
test2_environment="test2.com"
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
stage.$test2_environment) ;;
test.$test_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
if [ $CLUSTER_NAME = test.$test_env ];then
printf "got cluster $CLUSTER_NAME"
elif [ $CLUSTER_NAME = "test.test.com" ];then
printf "got dev cluster $CLUSTER_NAME"
echo "not found cluster"
else
echo "Environment not available"
fi
}
sops_ops
Question:
How do I do that?
Any help is appreciated!

Bash: two loops inside while loop

As a beginner I'm not sure if this is the best way to do multiple loops!! though it works well, please suggest me if there is an elegant way of doing this (I'm not restricted to bash but I'm not familiar with other languages)
#!/bin/bash
while :; do
read -n1 -e -p"Top Levels 1-5: " top_levels
if [ "$top_levels" == "1" ]; then
echo "top level 1"
elif [ "$top_levels" == "2" ]; then
while :; do
read -n1 -e -p"Sub Levels 1-5: " sub_levels
if [ "$sub_levels" == "1" ]; then
echo "sub level 1"
elif [ "$sub_levels" == "2" ]; then
echo "sub level 2"
elif [ "$sub_levels" == "3" ]; then
while :; do
read -n1 -e -p"Final Levels 1-5: " final_levels
if [ "$final_levels" == "1" ]; then
echo "Final level 1"
elif [ "$final_levels" == "2" ]; then
echo "Final level 2"
elif [ "$final_levels" == "3" ]; then
echo "Final level 3"
elif [ "$final_levels" == "4" ]; then
echo "Final level 4"
else
echo "bye"
break
fi
done
elif [ "$sub_levels" == "4" ]; then
echo "sub level 4"
else
echo "bye"
break
fi
done
elif [ "$top_levels" == "3" ]; then
echo "top level 3"
elif [ "$top_levels" == "4" ]; then
echo "top level 4"
else
echo "bye"
exit
fi
done
I'd write it like this
#!/bin/bash
process_final_levels() {
while :; do
read -n1 -e -p"### Final Levels 1-5: " final_levels
case $final_levels in
1) echo "Final level 1";;
2) echo "Final level 2";;
3) echo "Final level 3";;
4) echo "Final level 4";;
*) echo "returning to sub levels"
break;;
esac
done
}
process_sub_levels() {
while :; do
read -n1 -e -p"# Sub Levels 1-5: " sub_levels
case "$sub_levels" in
1) echo "sub level 1" ;;
2) echo "sub level 2" ;;
3) process_final_levels;;
4) echo "sub level 4" ;;
*) echo "returning to top levels"
break;;
esac
done
}
while :; do
# get top levels to work with
read -n1 -e -p"Top Levels 1-5: " top_levels
case "$top_levels" in
1) echo "top level 1" ;;
2) process_sub_levels ;;
3) echo "top level 3" ;;
4) echo "top level 4" ;;
*) echo "Bye"
exit;;
esac
done
I changed the prompt and exiting strings to be more intuitive as to what level you are working on. I didn't add comments, b/c I have no idea what's this for. I functionalized, b/c any code that needs to be modified, tested, or maintained should be functionalized.

Updating sub-menus variables

I have a problem with this bash script:
#!/bin/bash
G=100
echo $G
main_menu()
{
while :
do
clear
echo "Select from menu"
echo "[1] Press 1 to show savings"
echo "[2] Press 2 to withdraw savings"
echo "[3] Press 3 to exit"
echo "Enter your menu choice [1-3]: \c "
read -r m_menu
case "$m_menu" in
1) option_1;;
2) option_2;;
3) exit 0;;
*) echo "\nERROR: Please select a valid menu choice";
echo "Press ENTER To Continue..." ; read ;;
esac
done
}
option_1()
{
clear
echo "Your balance is $G"
echo "\nPress ENTER to return to menu..."
read
return
}
option_2()
{
clear
echo "Withdraw savings"
read -rp "Enter amount to withdraw: " num
if [ $num -le $G ]; then
answer=$(echo $(( G - num )))
echo "Your new balance is: $answer"
echo "$answer" | tee "$G"
elif [ $num -gt $G ]; then
echo "No: not eough money in your balance"
fi
read
return
}
main_menu
The problem is the following: how do I make sure that once I withdraw savings, my saving count will be updated? Because if I withdraw $90, obviously when I return to the balance it should be $10 and yet this doesn't work for me (it still says saving balance is $100)
What can I do?
Thank you (sorry for my poor English)
You have to update G in your code.
if [ $num -le $G ]; then
G=$(( G - num ))
echo "Your new balance is: $G"

bash find parameters

#!/bin/bash
menu=0
dir=""
size=""
name=""
modif=""
while [ $menu -ne 6 ]
do
echo "1. Name: $name"
echo "2. Directory $dir"
echo "3. Last modified: $modif"
echo "4. Minimum size: $size"
echo "5. Search"
echo "6. End"
read menu
case "$menu" in
"1") read name ;;
"2") read dir ;;
"3") read modif ;;
"4") read size;;
"5") if [ -z $name ]
then
option1=""
else
option1="-name $name"
fi
find "$option1";;
"6") ;;
*) echo "Wrong number!"
esac
done
I need to make a script which will be working like find command, but I've encountered a problem. When user doesnt input for example name the find command should have name option disabled.
I came up with something like this above, but it doestn work when the variable name doesnt contain anything (user didnt input anything). I keep getting error:
find: paths must precede expression: BASH ....
I honestly have no idea how to make it work instead of having 2^4 IF's and executing find with only those specified options that user has choosen.
Is there any way to make it easier?
edit: Now i modified it to:
#!/bin/bash
menu=0
dir=""
modif=""
while [ $menu -ne 6 ]
do
echo "1. Name: $name"
echo "2. Directory $dir"
echo "3. Last modified: $modif"
echo "4. Minimum size: $size"
echo "5. Search"
echo "6. End"
read menu
case "$menu" in
"1")
read name
;;
"2") read dir ;;
"3") read modif ;;
"4")
read size
;;
"5")
if [ -z $name ]
then
unset tablica[0]
else
tablica[0]="-name $name"
fi
find "${tablica[#]}"
;;
"6") ;;
*) echo "Wrong number!"
esac
done
But another error:
find: unknown predicate `-name example.txt'
You can build an array of parameters:
myarray=()
if [[ $size ]]
then
myarray+=(-size +"$size")
fi
if [[ $name ]]
then
myarray+=(-name "$name")
fi
...
find "$dir" "${myarray[#]}"
The benefit of this approach is that it also handles spaces correctly, and doesn't allow code injection.

Resources