Hello I don't know if this will be a duplicate , but I really don't see the error in this piece of code . It gives me an error at the then statement and I think there is no error .
I have putted comments on the two lines where the code breaks .
Here is the code:
#!/bin/bash
echo -n "Enter first value:"
read firstvar
echo -n "Enter second value:"
read secondvar
echo -n "Enter last value:"
read compvar
echo -n "Enter operation:"
read ops
counter=0
result=0
while [ $result -eq $compvar ]
do
if [ $ops -eq "+" ]
then result = $((firstvar+secondvar))
elif [ $ops -eq "-" ]
then result = $((firstvar-secondvar))
#elif[ $ops -eq "*" ]
#then result = $((firstvar\*secondvar))
#elif[ $ops -eq "/" ]
#then result = $((firstvar/secondvar))
else
echo "Input valid operation !!!"
fi
(($counter++))
done
Oh shoot , I m retarded . Missed the space between "elif" and the "[" .
You need to learn how to space and indent to have your code legible.
#!/bin/bash
echo -n "Enter first value:"; read firstvar
echo -n "Enter second value:"; read secondvar
echo -n "Enter last value:"; read compvar
echo -n "Enter operation:"; read ops
counter=0; result=0 #initialize variables
while [ $result -eq $compvar ]; do
if [ $ops = "+" ]; then
result = $((firstvar+secondvar))
elif [ $ops = "-" ]; then
result=$((firstvar-secondvar))
else
echo "Input valid operation !!!"
fi
((++counter))
done
Writing code is the same as writing literature. There are forms to use to make it easy for the reader (even if it's just you 6 months later) to follow. Your code above is confusing, and will bite you on the butt later. Add comments to.
The while loop is gratuitous and will do nothing-- the -eq should at least be -ne. The counter could be initialized as an integer to avoid problems that could arise, but it's also never used.
Related
I have been trying to resolve an issue where my loop's count should decrease, however nothing is working. I need to create a while loop that will read over a given amount of times. For instance, if I enter in "files.txt -a 3" in the terminal, I need my loop to repeat "Enter in a string: " 3 times. With my code below, I am only able to get it to loop once. I am not to sure where to put the counter and I can say that I have put it everywhere. Inside the if statement, in inside of the for loop, and inside the while loop but none seem to work. The number that the user will put is held in the $count variable.
#!/bin/bash
if ["$1" = "-a" ]
then
read in user String and save into file
fi
while [ "$count" > 0 ]
do
for i in $count
do
if [ "-a" ]
then
read -p "Enter in a string: " userSTR
echo userSTR >> files.txt
count=$(($count - 1))
fi
done
done
For conditional expression you need to use [[ expression ]], e.g. this will loop four times:
count=4
while [[ $count > 0 ]] ; do
echo "$count"
count=$(( $count - 1 ))
done
To fetch the count from the command-line argument, you could replace the assignment count=4 above with the following, parsing the command-line arguments:
if [ $# -lt 2 ] ; then
echo "Usage: $0 -a [count]"
exit 1
fi
if [ "$1" = "-a" ] ; then
shift
count=$1
fi
I m doing a simple calculator with option of choosing to continue or not . But when I try multiplying operation I get error in the console :
calculator.sh: line 17: [: too many arguments
calculator.sh: line 22: [: too many arguments
calculator.sh: line 27: [: too many arguments
calculator.sh: line 32: [: too many arguments
Which basically means all my operations has this error , but that is not true when I use them , they act normally .
I searched in stack overflow for similarities , but the examples are different . I escaped the * with slash , but I think it shouts the error in the comparing with the character "*" in order getting to the body of the if statement.
Here is the code:
#!/bin/bash
choice="Y"
while [ $choice == "Y" ]
do
echo -n "Enter first value:"
read firstvar
echo -n "Enter second value:"
read secondvar
echo -n "Enter last value:"
read compvar
echo -n "Enter operation:"
read ops
counter=0
result=0
while [ $result != $compvar ]
do
if [ $ops == "+" ]
then result=$((firstvar+secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
elif [ $ops == "-" ]
then result=$((firstvar-secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
elif [ $ops == "*" ]
then result=$((firstvar\*secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
elif [ $ops == "/" ]
then result=$((firstvar/secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
else
echo "Input valid operation !!!"
echo "Do you want to continue ? Y/N"
read choice
break
fi
counter=$((counter+1))
done
done
The problem is likely not because the "*" in script, but because of an asterisk in your $ops variable.
You should double-quote the variables to avoid globbing being applied to them; rewrite your tests like this:
elif [ "$ops" = "*" ]
Here's a very helpful resource for checking your shell scripts.
First, see the comment by Charles Duffy WRT "==" vs "=" for string tests.
Change occurrences of $ops to "${ops}".
Remove the escape in result=$((firstvar*secondvar)).
I took the liberty to reformat the script just a bit.
Hope this helps.
#!/bin/bash
choice="Y"
while [ $choice = "Y" ]
do
echo -n "Enter first value:"
read firstvar
echo -n "Enter second value:"
read secondvar
echo -n "Enter last value:"
read compvar
echo -n "Enter operation:"
read ops
counter=0
result=0
while [ $result != $compvar ]
do
if [ "${ops}" = "+" ]; then
result=$((firstvar+secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
elif [ "${ops}" = "-" ]; then
result=$((firstvar-secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
elif [ "${ops}" = "*" ]; then
result=$((firstvar*secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
elif [ "${ops}" = "/" ]; then
result=$((firstvar/secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
else
echo "Input valid operation !!!"
echo "Do you want to continue ? Y/N"
read choice
break
fi
counter=$((counter+1))
done
done
I have a config.txt file where the first line has a number 1, I need to read this and do certain tasks based on whether the number is 1 or 2 or 3 etc
The problem is I cannot test that config value, none of the if statements below work, and I tried many more variances.
config=$(head -n 1 /mnt/writable/config.txt) #grabs vale from file
echo $config #prints 1
But the following if statements do not echo anything.
if [[ "$config" = "1" ]];
then
echo "is a 1";
fi
if [[ "$config" == "1" ]];
then
echo "is a 1";
fi
if [[ "$config" = 1 ]];
then
echo "is a 1";
fi
if [ $config = 1 ];
then
echo "is a 1";
fi
I also tried "declare -i config" at the top but that didnt work either. Spent a day and no luck so far.
Because you have space or tab in your first line( I think )
So if you print using
echo $config
it will print 1 correctly but when you use it in if condition, it may not work as your wish ...
so I suggest that you should try following in your script,
config=$(head -n 1 /mnt/writable/config.txt | tr -d '[:space:]')
and all your if condition may work.
Integer equality can be tested by using -eq option.
Do man test for further details about if condition.
if [ $config -eq 1 ]; then echo "config is 1"; else echo "config is not 1"; fi
I am trying to write a calculator script but the first if blocks the elif. What I mean is when I try to run it I press 1 but it runs as if I pressed 2. Here is my code.
echo "For Advanced mode press 1"
echo "For Help press 2"
loop=1
while [ $loop=1 ]; do
read n
if [ $n=2 ]; then
echo "To use the calculator follow the promptings."
echo "If asked, the operators are: "
echo "* for multiplication, / for division."
echo "+ for addition, - for subtraction."
echo "% for modulus, which is division remainder"
echo "== is equal to, != is not equal to."
echo "** for exponents"
n=""
elif [ $n=1 ]; then
read a
break
fi
done
echo "_______________________"
echo "What would you like to do?"
echo "Press 1 for basic arith"
echo -n "Press 2 for geometry"
read choice
loop=2
if [ $choice=1]; then
while [ $loop=2 ]; do
echo -n "Enter X Value: "
read x
echo -n "Enter Operator: "
read op
echo -n "Enter Y Value: "
read y
ans=$((x $op y))
echo "$x $op $y = $ans"
echo "____________________"
echo "To input a new function, press enter"
read cont
done
fi
The [ built-in is used for evaluating conditions. There must be a space after the opening [ and a space before before the closing ], otherwise it's incorrect syntax.
Inside a [ ... ] expression, you can use various conditional operators. One such operator is =. You must put spaces before and after an operator, otherwise it's not recognized as an operator.
For example, [ $n=2 ] where the value of n is 1, will be evaluated as [ 1=2 ]. The 1=2 is not evaluated as a condition, because there are no spaces around it. The 1=2 will be evaluated as a string "1=2". And any non empty string in Bash is truthy, so the statement [ 1=2 ] yields true, even if it doesn't "look like" it would be true.
You must write [ "$n" = 2 ] so that = is interpreted as the conditional operator, and the expression doesn't become a single string. Note that spaces are extremely important in Bash. Spaces separate the logical elements of a program, without spaces expressions are concatenated as strings.
Also notice that I added double quotes around the variable. This is to protect the expression in case the variable is empty. If for example you write [ $n = 2 ] and the variable is empty, then the expression will be evaluated as [ = 2 ], which is a malformed expression, and the script will crash with an error.
Put spaces between your condition elements like $n = 2 not $n=2.You should pass it's arguments as whitespace separated words. So your code should be as following:
echo "For Advanced mode press 1"
echo "For Help press 2"
loop=1
while [ $loop = 1 ]; do
read n
if [ $n = 2 ]; then
echo "To use the calculator follow the promptings."
echo "If asked, the operators are: "
echo "* for multiplication, / for division."
echo "+ for addition, - for subtraction."
echo "% for modulus, which is division remainder"
echo "== is equal to, != is not equal to."
echo "** for exponents"
n=""
elif [ $n = 1 ]; then
read a
break
fi
done
echo "_______________________"
echo "What would you like to do?"
echo "Press 1 for basic arith"
echo -n "Press 2 for geometry"
read choice
loop=2
if [ $choice = 1 ]; then
while [ $loop = 2 ]; do
echo -n "Enter X Value: "
read x
echo -n "Enter Operator: "
read op
echo -n "Enter Y Value: "
read y
ans=$((x $op y))
echo "$x $op $y = $ans"
echo "____________________"
echo "To input a new function, press enter"
read cont
done
fi
I have a shell script as follows (taken from some lecture slides):
#/bin/sh
echo -e "enter a number:\c"
read number
if [$number -ne 2]
then
echo "Number is not equals to 2"
fi
And I'm getting a syntax error where fi is. Any idea what the problem is?
Also, what does the extra term in echo -e "enter a number:\c" means (asides from the simple fact that it asks for a number)?
EDIT: now I did
#/bin/sh
echo -e "enter a number:\c"
read number
if [ "$number" -ne 2 ]
then
echo "Number is not equals to 2"
fi
And I'm still getting the error...
Same goes for
#/bin/sh
read -p "enter a number: " number
if [ "$number" -ne 2 ]
then
echo "Number is not equals to 2"
fi
SOLVED: I've made a copying error there. Thanks for the input by the way, guys.
Problem is this if condition:
if [$number -ne 2]
You need to put space after [ and before ] so use:
if [ "$number" -ne 2 ]
Your script can be rewritten as:
#/bin/sh
read -p "enter a number: " number
if [ "$number" -ne 2 ]
then
echo "Number is not equals to 2"
fi
However if bash is available then better to switch to bash instead of old bourne shell.