I commented the things I have problem.
and also, is there any other way I can exit my loop without using the exit command?.................
#!/bin/bash
while [ "$done" != "true" ] #this don't work
do
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -n "Enter Choice: "
read scale # starting from this part for checking if user only inputs numbers 1-5 not working
if ! [[ "$scale" =~ ^[0-6]+$ ]]
then
echo "Invalid Input"
fi #up to this part is not working
read -r answer
case "$answer" in
1) ./move_empty
exit 55
;;
2) ./file_size
exit 0
;;
3) ;;
;;
4)
;;
5) done="true";;
esac
done
There are few things Which should be avoided
while [ "$done" != "true" ] --> while [ $done -ne 1 ] /* -ne is not equal to */
case "$answer" in --> case $answer in
Complete Code
var=0
flag=1
while [ $var -ne 1 ]
do
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -n "Enter Choice: "
read scale
if [ $scale -gt 0 -a $scale -lt 6 ]
then
echo "valid Input, you can procees for switch "
else
echo "invalid input.. go again & give correct one "
flag=0
fi
if [ $flag -eq 1 ]
then
read -r answer
case $answer in
1) ./move_empty
#exit 55
break
;;
2) ./file_size
#exit 0
break
;;
3)
exit 0
;;
4)
;;
5) done="true";;
esac
fi
done
To come out from loop, without using exit command, use break. Here is the answer from man 1 bash
break [n]
Exit from within a for, while, until, or select loop.
If n is specified, break n levels. n must be ≥ 1. If n
is greater than the number of enclosing loops, all
enclosing loops are exited. The return value is 0
unless n is not greater than or equal to 1.
Related
I am trying to make a simple menu-driven calculator script. I am trying to make it such that after selecting A (add) or B (subtract) from the menu, it'll call the function and display the corresponding message when:
The parameters entered when function called is greater than 3
No parameters are entered when the function is called
The operator entered when the function is called is neither "+" or "-"
Right now it is doing the parameter check when I call the script ./mycalc.sh
executing the script
Am not sure how to make it check parameters after the function is called?
#!/bin/bash
display() {
echo "Calculator Menu"
echo "Please select an option between add, subtract or exit"
echo "A. Add"
echo "B. Subtract"
echo "C. Exit"
}
#initialize choice n
choice=n
if [[ $# -ne 3 ]]
then echo " You have not entered 3 parameters"
exit 1
fi
if [ $# -eq 0 ]
then
echo " You have not entered any parameters, please input 3. "
fi
if [[ $2 != [+-] ]]
then
echo " Please enter an add or subtract operator."
exit 1
fi
add() {
echo " The sum of $one + $three equals $(( $one $op $three ))"
}
subtract () {
echo " The difference of $one - $three equals $(( $one $op $three )) "
}
while [ $choice != 'C' ]
do display
read choice
if [ $choice = 'A' ]
then
read -p "Please enter two operands and the operator '+': " one op three
add $one $op $three
elif [ $choice = 'B' ]
then
read -p " Please enter two operands and the operator '-': " one op three
subtract $one $op $three
elif [ $choice = 'C' ]
then
echo "Thank you for using this program. The program will now exit."
fi
done
sleep 3
exit 0
if [ $# > 3 ]
then
echo " You need to input 3 parameters. "
fi
Within [...], the > is simply redirection. You'll find an empty file named 3 in your current directory.
Since this is an error condition for your script, you'll want to exit:
if [[ $# -ne 3 ]]; then
echo "usage: $0 operand1 operator operand2" >&2
exit 1
fi
And to test the operator, there are many ways.
case, but it's a bit verbose
case $2 in
'+'|'-') : ;;
*) echo "Operator must be + or -" >&2
exit 1
;;
esac
Within [[...]] the == and != operators are pattern matching operators
if [[ $2 != [+-] ]]; then
echo "Operator must be + or -" >&2
exit 1
fi
I have a case statement like 1) 2) 3) ... as below where there are multiple if else condition on 1) format case.
I want to repeat the particular case on invalid condition.
When user select 1 first it ask for the name of the juice.
next it asks the availability. If yes it continues else asks for the count and does an operation. If the operation "exit 0" it just prints done. if the operation "exit 1" I wanted the loop to continue from the top ie from "name of the juice?"
1)
echo "Name of the Juice ?"
read juice
echo "Is it available? [Y/N]"
read deci
if [ "$deci" = "N" ]
then
echo "Continue.!"
else
echo "how many left in the warehouse ?"
read left
I have a command here which exits 0 or 1
if [ $? -eq 0 ]; then
echo "done"
else
<I have to continue from the start [ie;name of the juice]>
fi
fi
;;
echo "Name of the Pizza ?"
read juice
echo "Is it available? [Y/N]"
read deci
if [ "$deci" = "N" ]
then
echo "Continue.!"
else
echo "how many left ?"
read left
I have a command here which exits 0 or 1
if [ $? -eq 0 ]; then
echo "done"
else
<I have to continue from the start [ie;name of the juice]>
fi
fi
;;
Your code is a bit hard to read, but if I understand correctly, you want to loop within one of your cases and make decisions on whether to continue back to the start of that case statement, or whether to leave. That can be done by putting the contents of that particular case in an infinite loop with while : and then using continue to return to the beginning of the loop at any point, or using break to break out of the loop at any point.
Not passing on the remainder of your code, but you could handle case 1) of your case statement similar to:
1)
while :
do
printf "Name of the Juice?: "
read -r juice
printf "Is it available? [Y/N]: "
read -r deci
if [ "$deci" = "N" ] || [ "$deci" = "n" ]
then
printf "Continue.!\n"
else
printf "how many left in the warehouse?: "
read -r left
# I have a command here which exits 0 or 1
if [ $? -eq 0 ]; then
printf "done\n"
break ## break out of loop
else
continue
fi
fi
printf "Name of the Pizza?: "
read -r juice ## why juice - I thought this was pizza?
printf "Is it available? [Y/N]: "
read -r deci
if [ "$deci" = "N" ] || [ "$deci" = "n" ]
then
printf "Continue.!\n"
else
printf "how many left?: "
read -r left
# I have a command here which exits 0 or 1
if [ $? -eq 0 ]; then
printf "done"
break ## break out of loop
else
continue
fi
fi
done
;; # your case termination from where?
Please add further explanation of any additional requirements you have and post the complete script you are working on as A Minimal, Complete, and Verifiable example and I am happy to help further.
I want to create a select menu in it, like this:
echo "Choose your option:"
1) Factorial Calculation
2) Addition Calculator
3) Quit
And I have some shell scripts;
Factorial
./fact.sh
#!/bin/bash
fact=1
#taking input from user
echo -e "enter a number"
read n
#if enter value less than 0
if [ $n -le 0 ] ; then
echo "invalid number"
exit
fi
#factorial logic
if [ $n -gt 0 ] ; then
for((i=$n;i>=1;i--))
do
fact=`expr $fact \* $i`
done
fi
echo "The factorial of $n is $fact"
Addition
./add.sh
#!/bin/bash
#function to add two numbers
add()
{
x=$1
y=$2
echo -e "Number entered by u are: $x and $y"
echo "sum of $1 and $2 is `expr $x + $y` "
}
# main script
echo "enter first number"
read first
echo "enter second number"
read sec
#calling function
add $first $sec
echo "end of the script"
I have to create a menu, how should I proceed?
You can use select.
For your example:
select option in Factorial Addition Quit; do
case "$option" in
"Factorial")
echo "Factorial"
break ;;
"Addition")
echo "Addition"
break ;;
"Quit") exit ;;
esac
done
I keep getting unexpected End of file error while running a if else statement
#! /bin/bash
echo -e "1: Proband\n 2: mincount\n Enter an option:"
read promin
echo $promin
if ($promin == 1) then
echo -e "Enter the proband file name\n"
read proband_file
echo "$proband_file"
endif
if ($promin == 2) then
echo -e "enter the min count number\n"
read mincount
echo "$mincount mincount"
endif
I tried fi instead of elseif too. But i still get the same error. Can someone help me fix that?
This is how you write an if-statement in bash:
if - then - fi
if [ conditional expression ]
then
statement1
statement2
fi
if - then - else - fi
If [ conditional expression ]
then
statement1
statement2
else
statement3
statement4
fi
if - then - elif - else - fi
If [ conditional expression1 ]
then
statement1
statement2
elif [ conditional expression2 ]
then
statement3
statement4
else
statement5
fi
Example of a conditional expression:
#!/bin/bash
count=100
if [ $count -eq 100 ]
then
echo "Count is 100"
fi
IMPROVED
The if is syntax is not correct. In the if there should be a program (bash internal or external) run, which returns an exit code. If it is 0 then if is true, otherwise it is false. You can use grep or any other utility, like test or /usr/bin/[. But bash has a built-in test and [.
So [ "$var" -eq 1 ] returns 0 if $var equals 1, or return 1 if $var not equals 1.
In your case I would suggest to use case instead of if-then-elif-else-fi notation.
case $x in
1) something;;
2) other;;
*) echo "Error"; exit 1;;
easc
Or even use select. Example:
#!/bin/bash
PS3="Enter an option: "
select promin in "Proband" "mincount";do [ -n "$promin" ] && break; done
echo $promin
case "$promin" in
Proband) read -p "Enter the proband file name: " proband_file; echo "$proband_file";;
mincount) read -p "Enter the min count number: " mincount; echo "$mincount mincount";;
*) echo Error; exit 1;;
esac
This will print the "Enter an option: " prompt and wait until a proper answer is presented (1 or 2 or ^D - to finish the input).
1) Proband
2) mincount
Enter an option: _
Then it checks the answer in the case part. Meanwhile $promin contains the string, $REPLY contains the entered answer. It also can be used in case.
I just changed your code and I think it works now.
I think the problem is you should fi instead of endif...
#!/bin/sh
echo "1: Proband\n2: mincount\nEnter an option:"
read promin
echo $promin
if [ $promin -eq "1" ]
then
echo "Enter the proband file name\n"
read proband_file
echo "$proband_file"
elif [ $promin -eq "2" ]
then
echo "enter the min count number\n"
read mincount
echo "$mincount mincount"
fi
#! /bin/bash
echo -e "1: Proband\n2: mincount\nEnter an option:"
read promin
echo $promin
if (($promin == 1)); then
echo -e "Enter the proband file name\n"
read proband_file
echo "$proband_file"
elif (($promin == 2)); then
echo -e "Enter the min count number\n"
read mincount
echo "$mincount mincount"
fi
I don't know if you need an if-else statement or two if statemnts. The above has an if-else.
If you need two if statements, then insert a line of code below the "echo "$proband_file"" line with the text:
fi
Then replace the line "elif (($promin == 2)); then" with the following code:
if (($promin == 2)); then
I am making a new Menu Driven Shell Script in linux, I have simplified my table to just hello and bye to make this simpler, below is my basic menu layout
# Menu Shell Script
#
echo ----------------
echo menu
echo ----------------
echo [1] hello
echo [2] bye
echo [3] exit
echo ----------------
Basically I have the menu, I have been playing around with a few things recently but cant seem to get anything working as I am new to this, I think then next line would be
`read -p "Please Select A Number: " menu_choice`
but I am not sure what to do with the variable and what not.
I was wondering if anyone could help me with the next bit of code to simply get it to say hello when I press one, bye when 2 is pressed and exit when 3 when the user presses 3. It would be so much appreciated as I have been trying different ways for days and can't seem to get it to work.
you don't need those backticks for echo... and read
echo "----------------"
echo " menu"
echo "----------------"
echo "[1] hello"
echo "[2] bye"
echo "[3] exit"
echo "----------------"
read -p "Please Select A Number: " mc
if [[ "$mc" == "1" ]]; then
echo "hello"
elif [[ "$mc" == "2" ]]; then
echo "bye"
else
echo "exit"
fi
Edit
showMenu(){
echo "----------------"
echo " menu"
echo "----------------"
echo "[1] hello"
echo "[2] bye"
echo "[3] exit"
echo "----------------"
read -p "Please Select A Number: " mc
return $mc
}
while [[ "$m" != "3" ]]
do
if [[ "$m" == "1" ]]; then
echo "hello"
elif [[ "$m" == "2" ]]; then
echo "bye"
fi
showMenu
m=$?
done
exit 0;
Here is a sample
if [ $menu_choice -eq 1 ]
then
echo hello
elif [ $menu_choice -eq 2 ]
then
echo bye
elif [ $menu_choice -eq 3 ]
then
exit 0
fi
or using case
case $menu_choice in
1) echo hello
;;
2) echo bye
;;
3) exit 0
;;
esac