Following script
read n
for (( c=1; c<=$n; c++ ))
do
echo "HI"
done
gives error solution.sh: line 2: ((: c<=1: syntax error: invalid arithmetic operator (error token is "")
I am using BASH. What is wrong with the for loop?
edit:
I am working on the BASH hackerrank IDE and although this code is not directly related to the problem in this link, I am getting this error.
I've reproduced this error message by pressing Ctrl-E after 1. It looked like this:
$ ./1.sh
1^E
./1.sh: line 3: ((: c<=1: syntax error: invalid arithmetic operator (error token is "")
So make sure you are not pressing some strange combination of keys before enter.
You need to add this line
#!/bin/bash
at the top of solution.sh.
(if your bash is at a different location, do, in a terminal,
which bash
to determine its location)
#picasso13 just a wild guess cause it got me(and yield the same mysterious error when I tried to loop with array constructed from the input). There are 2 inputs on hackerRank(the first one is actually the size of the second). It solved my problem when I threw away the first and make sure my iteration was working on the list of numbers:
freq=()
for i in {1..100}; do
freq[$i]=0
done
read ignore
read inputs
IFS=', ' eval 'array=($inputs)'
for i in "${array[#]}"; do
(( freq[$i]++ ))
done
for i in "${!freq[#]}"; do
if [[ freq[$i] -eq 1 ]]; then
echo $i
fi
done
if you comment out my read ignore you'll reproduce the issue.
Related
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 1 year ago.
I'm new to bash scripting and am having trouble trying to get this to work
local attempt=1
local success=false
while [[ "$attempt" -le "$retryAttempt" && "$success" -eq "false"]]; do
if ! [[ some condition here]];
then
echo true
return
fi
done
I'm getting an error on the while condition saying
line 10: [: missing ]
I cannot figure out what is wrong here, suggestions anyone?
Short: insert spaces around braces
Long: I wouldn't consider myself a bash pro (no idea what local is), but in a minimal syntax example (which is actually an infinity loop).
while [ $b -le $a] ; do
b=$a
done
gives me the same error. Changing the first line to
while [ $b -le $a ] ; do
works.
Depending on the complexity of the script, you might want to consider python or perl. In my opinion bash syntax can be a real pain in the a**. Especially when passing arguments with spaces through more than one level.
This question already has answers here:
How can I compare two floating point numbers in Bash?
(22 answers)
Closed 3 years ago.
My calculator generates the error mentioned in the title when I try to use the operator "/" as well as the numbers 4.5 and 2.
This is (just like the error states) most likely due to what's after the decimal point in 4.5, but I don't know how I could fix this and why the script actually manages to give me the correct result afterwards.
Code:
#!/bin/bash
read -p "Operator: " operator
read -p "First number: " ch1
read -p "Second number: " ch2
case $operator in
"+") echo "scale=2; $ch1+$ch2" | bc -l;;
"/") if [[ $ch1 -eq 0 || $ch2 -eq 0 ]]
then
echo "Nope..."
else
echo "scale=2; $ch1/$ch2" | bc -l
fi
;;
esac
Full output:
./script2.sh: line 9: [[: 4.5: syntax error: invalid arithmetic operator (error token is ".5")
2.25
Despite producing floating point results, Bash does not support other type of arguments than integers, so you need to rather invoke external tools like bc for your math or stick to integers only.
See the Bash documentation, section "6.5 Shell Arithmetic":
The shell allows arithmetic expressions to be evaluated, as one of the shell expansions or by using the (( compound command, the let builtin, or the -i option to the declare builtin.
Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error
I want to get the day of the year and subtract one then mod by three. It is a switch for running my scripts within crontab.
if (((date(%j) -1) %3))
echo 'hello'
exit()
else
echo hi
fi
This is just to test that it is working, however I am getting
./getdate.sh: line 4: syntax error near unexpected token `fi'
./getdate.sh: line 4: `fi'
This could be trivial for some of you but I am still very new to bash.
Thanks
Here's what I think you meant to do:
#!/bin/bash
if (( ( $(date +%-j) - 1 ) % 3 ))
then
echo 'hello'
exit
fi
echo hi
Always use a shebang.
To execute date and use what it prints wrap it in $().
Arguments to commands, in this case date, just follow the command (and a space); no brackets.
date wants a + to prefix the format.
%j will print leading zeros, which Bash will take to mean the number is octal. Use %-j to not get such zeros.
I think you want to subtract before modulus: Use brackets for ordering.
if needs a then.
exit should not have brackets.
No need for an else after an exit.
I recently had a problem in one of my Bash scripts that was caused by unexpected output from one of the SSH commands it ran (due to an OS version upgrade). The problem actually resulted in a Bash syntax error, because I was expecting this output to contain only a numeric status code (from a remote script), which I then used in an arithmetic expression.
What surprised me, though, was that my Bash script continued executing after reporting the syntax error. This, despite the fact that I have the following line near the beginning of the file:
# Terminate script if any command fails
set -o errexit
Although a syntax error is obviously not the same as a failed command, it seems to me that that the former represents an even more serious error condition.
I know that you can use the -n parameter to check the syntax of a Bash script without actually executing any commands, but that would not have caught this problem anyway.
Here's an example which causes the error but does not exit:
#!/bin/bash
set -o errexit
x=")"
echo $(( x + 1 ))
echo still running
Is there any way to force a Bash script to terminate immediately after a syntax error in an arithmetic expression is detected during its normal operation?
The quick answer is that you can trap it in this case by examining $?:
#!/bin/bash
x=")"
echo $(( $x + 1 ))
if [[ $? != 0 ]]; then
echo >&2 "caught syntax error; aborting"
exit $?
fi
echo success
However, this approach doesn't always work, e.g.
#!/bin/bash
x="-1"
(( x += 1 ))
if [[ $? != 0 ]]; then
echo >&2 "caught syntax error; aborting"
exit $?
fi
echo success
will result in:
caught syntax error; aborting
The bash(1) man page says:
The evaluation is performed according to the rules listed below
under ARITHMETIC EVALUATION. If expression is invalid, bash
prints a message indicating failure and no substitution occurs.
but this hides some details of Bash's fairly peculiar behaviour with respect to syntax errors. For example,
#!/bin/bash
x=")"
if echo $(( $x + 1 )); then
echo success
else
echo failure
fi; echo "almost end of script"
echo "end of script"
results in:
./foo.sh: line 3: ) + 1 : syntax error: operand expected (error token is ") + 1 ")
end of script
By the way, set -o errexit doesn't seem to make any difference to any of this.
Why wont this code run?
It is meant to allow the user to input a number into the script in a loop until the user enters a -1 then the script should terminate
However I keep getting this error:
-ne: unexpected operator
Here is the code:
#!/bin/sh
condition= 0
while [ $condition -ne -1 ]
do
echo $condition
read condition
done
You have to remove the space after the condition= and the 0. As spaces are the natural token separator of bash, you have to be very careful where you put spaces. In this case, the space separates a list of initialized variables (in this case with an empty value) of the supposed program (0).
The space after =. Remove it .
Well, many already gave you the answers, nevertheless, just see this one if you need it (is just another way, a little more verbose).
#!/bin/sh
while read condition; do
case "$condition" in
-1) printf "GOOD BYE!\n"; break ;;
*) printf "AGAIN, PUT A NUMBER!" ;;
esac
done
Add a semicolon ; after the closing bracket.