How to use if then in bash - 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/

Related

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.

Bash nested conditionals show unexpected behavior

please take a look at this:
This should echo X10, but echoes jackpot... can anyone see why it doesn't behave as it should?
Probably just some mistake I made that do not throw errors?
dice1=1
dice2=40
#These two lines are just tests to see if my brain still function:
echo "Is dice 1 less than 2? $(($dice1 < 2))"
echo "Is dice 2 between 6 and 54? $(($dice2 > 5 && $dice2 < 55))"
if [[ $dice1 == 1 ]]
then
if [[ $dice2 < 6 ]]
then
#dice1 has to be equal 1 and dice2 less than 6:
echo "jackpot"
else
#Since dice2 is larger than 5, if smaller than 55
#it should be between 6 and 54...
if [[ $dice2 < 55 ]]
then
echo "X10"
else
echo "X5"
fi
fi
else
echo "Dice one is not equal 1."
fi
When used with [[, the < and > operators sort lexicographically using the current locale.
I see two options.
Do the comparison in arithmetic context:
if (( $dice1 == 1 ))
then
if (( $dice2 < 6 ))
or the old-fashioned way:
if [[ $dice1 -eq 1 ]]
then
if [[ $dice2 -lt 6 ]]

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

Why won't my shell script work?

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.

Bash print the number of incorrect file line

A beginner asking for help (:
So, I have a script that checks brackets in a text file and tells whether they are closed correctly. However, I also want to make my script print out the number of the incorrect line (where brackets are closed incorrectly). I have tried counting file lines and then making a nested while loop, however, it doesn't work for me at all ): Are there any simple solutions for this? I would like to leave the LINE counter if that's possible o:
INPUT="$1"
count=0
LINE=0
# Check if file exists
[ ! -f $INPUT ] && { echo "file $INPUT do not exist."; exit ; }
# Count file lines and read every char
while IFS= read -r LINE
do
LINE=$(( LINE + 1 ))
while read -n1 char
do
[ "$char" == "(" ] && (( count++ ))
[ "$char" == ")" ] && (( count-- ))
if [ "$count" -lt 0 ]
then
break
fi
done
done < "$INPUT"
if [ "$count" -lt 0 ]
then
echo "Found a mistake in $LINE line "
else
echo "Everything's correct"
fi
You have a couple of problems:
Your read in the inner loop consumes the input from the file, not from LINE.
The line
LINE=$(( LINE + 1 ))
is really wrong: LINE is the content of the line of your file, and your trying to add 1 to it. Weird.
Your break only breaks the inner loop (it should break two loops). Use break 2 for this.
Here's a working version of your script:
input=$1
count=0
linenb=0
# Check if file exists
[[ -f $input ]] || { echo "Error: file $input do not exist."; exit 1; }
# Count file lines and read every char
while IFS= read -r line; do
((++linenb))
while read -n1 char; do
[[ $char == '(' ]] && ((++count))
[[ $char == ')' ]] && ((--count))
((count>=0)) || break 2
done <<< "$line"
done < "$input"
if ((count<0)); then
echo "Found a mistake in line #$linenb:"
printf '%s\n' "$line"
else
echo "Everything's correct"
fi
Note that I used more ((...)) and [[...]].
I also used lowercase variable names, as your computer isn't deaf: you don't need to shout the name of the variable. (And it's nicer to the eye). And it's good practice to use lowercase variable names, as there's no chance that they clash with Bash's own variables.

Resources