Why won't my shell script work? - bash

I'm using bash and this is my code
for i in $(seq 20)
do
if [ $i % 3 == 0 ]
then
echo HI
else
echo $i
fi
done
So the output should be:
1
2
hi
4
5
hi
...
and so on.
But I keep getting this error when I run my shell.
script.sh 4: [: 1: unexpected operator
1
then it just goes on like that until 20.
I've tried different things but i can't get it to work.

Even a bit simpler:
if ((i%3 == 0)) # No '$' needed
then
....

You need to change the line containing the if to:
if [[ $((i%3)) == 0 ]]
That will work.

Use this
for i in {1..20}
do
if [ `expr $i % 3` == 0 ]
then
echo HI
else
echo $i
fi
done

It should be like below mentioned format,
if [ $i%3 == 0 ];
then
but if you introduce "==" symbol it will only produce incremental value from 1 to 20.

Related

How to use if then in bash

We have a text file with 4 lines and a different number of words and i want to count the words per line and see if the number is even or odd but the result keeps saying
./oddwords.sh: line 14: syntax error near unexpected token `then'
./oddwords.sh: line 14: ` if[ n % 2 == 0 ]; then'
This is what ive done so far
#!/bin/bash
if [ $# -ne 1 ] ; then
{
echo "Wrong number of arguments!"
exit 1
}
fi
while read line ; do
n= echo "$(echo $line | wc -w)"
if[ n % 2 == 0 ]; then
echo "Is even"
else
echo "Is odd"
fi
done < $1
Using double brackets like
if [[ n % 2 == 0 ]]; then
and making the numbers variables at the top instead of just saying 2
a=2
b=1
at the beginning then referencing them later in the if statement like
if [[ $a%2 == 0 ]]; then
if you still have issues try
if [[ $a%2 -eq 0 ]]; then
Here are a couple sites i find useful for checking bash when I get stuck on something as well.
https://www.shellcheck.net/
https://explainshell.com/

I am getting an error: line 5: conditional binary operator expected ; syntax error near `%' ; line 5: `if [[ $i % 2 = 0 ]]'

I am getting an error which is:
exam.sh: line 5: conditional binary operator expected
exam.sh: line 5: syntax error near `%'
exam.sh: line 5: `if [[ $i % 2 = 0 ]]'
Here is my program code:
#!/bin/bash
i=1;
for user in "$#"
do
if [[ $i % 2 = 0 ]]
then
cd even
mkdir $user
.
else if [[ $i % 3 = 0 ]]
then
cd three
mkdir $user
.
else
cd other
mkdir $user
fi
fi
i=$((i + 1));
done
[[ doesn't do arithmetics. You need (( for that.
if (( i % 2 == 0 ))
Another option is to use $((...)) to generate a C-style
"boolean" that you can test explicitly.
if [ "$(( x % 2 == 0 ))" = 1 ]; then
echo "$x is even"
fi
This is objectively worse than if (( x % 2 == 0 )) in bash, but is useful if you need strict POSIX compliance.

How to say less than but no equal to in bash?

I'm getting an error with this, I did my research but found nothing.
if [ $value -lt 3 -ne 1 ]; then
execute code
fi
line 6: [: syntax error: -ne unexpected
One way to make this work is
if [ "${value}" -lt 3 ] && [ "${value}" -ne 1 ]; then
echo "Hello"
fi
I like to switch to arithmetic expressions using (( when I need tests like these:
declare -a values=(1 2 3)
for value in "${values[#]}"; do
if (( value != 1 && value < 3 )); then
echo "execute code for $value"
fi
done
The above outputs:
execute code for 2
use (( )) brackets for arithmetic operations and [[ ]] for strings comparison
$ is redundant in round brackets so (( $a == 1 )) is the same as (( a == 1 ))
typeset a=2
(( a < 3 )) && (( a != 1 )) && echo "Execute code"
more details : http://faculty.salina.k-state.edu/tim/unix_sg/bash/math.html

Unary operator expected error in while loop execution in shell scripting(UNIX)

I wrote a code to print
5 4 3 2 1
using while loop in shell script.
But error of expr: syntax error
scriptprog3.sh: line 3: [: -gt: unary operator expected
is showing. Please help out. Here is the code
a=5
while [ $a -gt 0 ]
do
echo $a
a=` expr $a -1 `
done
This is what you actually may want:
$ a=5
$ while (( a > 0 ));do echo $a ; (( a-- ));done
5
4
3
2
1
(( code )) makes the math over the a var without returning any value back to the caller.
I was executing the following code and it gave me the "[: -lt: unary operator expected"
error.
#while
i = 10
echo "This is a while loop"
while [ $i -lt 15 ]
do
echo $i
i=$[ $i+1 ]
done
The problem was using while loop as while [ $i -lt 15 ] like this,
so I changed it towhile [[ $i -lt 15 ]] and it worked (notice the double parentheses).
Below is the working example.
#while
i = 10
echo "This is a while loop"
while [[ $i -lt 15 ]]
do
echo $i
i=$[ $i+1 ]
done

Simple Shell/Bash Program Syntax

I have the following bash script:
#!/bin/bash
if [ ! -f numbers ]; then echo 0 > numbers; fi
count = 0
while [[$count != 100]]; do
count = `expr $count + 1`
done
When I run it in terminal on my Mac, I get the following output:
seq_file_gen.sh: line 3: count: command not found
seq_file_gen.sh: line 4: [[: command not found
Why am I getting these errors? This script was given to me by my teacher so I have no idea why I can't get this script to run. Any help would be greatly appreciated.
EDIT:
This is the correct way to write this script (with spaces)
#!/bin/bash
if [ ! -f numbers ]; then echo 0 > numbers; fi
count=0
while [[ $count != 100 ]]; do
count=`expr $count + 1`
done
Add spaces before/after [[ and ]] like so:
while [[ $count != 100 ]]; do

Resources