0

I wanna Write a Bash script that can print if the number in the last column is odd or even or if no numbers in the line from a text file, the data is looking like this in a db.txt file :

sdn sddjk@gmail 123
ksd 234
sddd sddsd@gmail 

i tried this :

#!/bin/bash
input="db.txt"
while IFS=" " read -r rec_column3
do
if [ $((number % 2)) -eq 0 ]; then 
    echo even
elif [ $((number % 2)) -eq 1 ]; then 
    echo odd
elif [[ "$rec_column3" != "number" ]]; then
    echo not number
else 
    echo not found 
fi     
done

output is :

even
even

so can anyone helps me ? tnx

Barmar
  • 741,623
  • 53
  • 500
  • 612
ask stack
  • 1
  • 4
  • You never set the variable `number`. You're not reading from `$input`. – Barmar Jun 18 '22 at 13:30
  • 3
    Use shellcheck.net to verify your code – Barmar Jun 18 '22 at 13:32
  • The shell is the thing that executes the *shell script* you want to write. – chepner Jun 18 '22 at 13:34
  • @Barmar actually, it was rec_column3 but I tried to test another column and I got the same result, I don't know why it can't read the numbers from the last column even if I specified it – ask stack Jun 18 '22 at 13:35
  • 1
    You still never set `number` that's used in `$((number % 2))`. – Barmar Jun 18 '22 at 13:41
  • What is `"$rec_column3" != "number"` for? Why would the file contain the string `number`? – Barmar Jun 18 '22 at 13:42
  • You need `< "$input"` after `done` to read from the file. – Barmar Jun 18 '22 at 13:42
  • You have no code anywhere to get the last number from `$rec_column3` – Barmar Jun 18 '22 at 13:44
  • 1
    Can you use `awk`? There you can use `$NF` to get the last word of the line. – Barmar Jun 18 '22 at 13:44
  • @Barmar - how to use number then ? - "$rec_column3" != "number" to check if there is no numbers in the lines, actually I am still a beginner so I don't know I just tried a solve - ok I fixed the input - how to use awk to solve this problem? – ask stack Jun 18 '22 at 13:52
  • See https://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash for how to test if a variable contains a number – Barmar Jun 18 '22 at 14:07

2 Answers2

0
#!/bin/bash
input="db.txt"
#########################
# check third field 
#########################
echo "check third field"
while read -r _ _ rec_column3 
do
  if [[ -z "$rec_column3" ]]; then
    echo "not found" >&2;
  elif ! [[ $rec_column3 =~ ^[0-9]+$ ]] ; then
    echo "'$rec_column3' is not a number" >&2;
  elif [[ $((rec_column3 % 2)) -eq 0 ]]; then 
    echo "'$rec_column3' is even" >&2
  else 
    echo "'$rec_column3' is odd" >&2
  fi     
done < $input

echo "-----------------------"

#########################
# or check last field
#########################
echo "check last field"
while IFS=' ' read -r -a array  
do
  last_column=""
  [[ ${#array[@]} -ne 0 ]] && last_column=${array[-1]}
  if [[ -z "$last_column" ]]; then
    echo "not found" >&2
  elif ! [[ $last_column =~ ^[0-9]+$ ]] ; then
    echo "'$last_column' is not a number" >&2
  elif [[ $((last_column % 2)) -eq 0 ]]; then 
    echo "'$last_column' is even" >&2
  else 
    echo "'$last_column' is odd" >&2
  fi     
done < $input

$ cat db.txt 
sdn sddjk@gmail 123
ksd 234
ksd 
12345

sddd sddsd@gmail 234 
sddd sddsd@gmail 111 
sddd sddsd@gmail aaa 

$ ./script.sh 
check third field
'123' is odd
not found
not found
not found
not found
'234' is even
'111' is odd
'aaa' is not a number
-----------------------
check last field
'123' is odd
'234' is even
'ksd' is not a number
'12345' is odd
not found
'234' is even
'111' is odd
'aaa' is not a number
ufopilot
  • 3,269
  • 2
  • 10
  • 12
0

awk is probably a better tool for this job. You can do something like this

awk 'BEGIN {split("even odd", a)} $NF ~ /^[0-9]+$/ {print a[$NF%2+1]; next} {print "NAN"}' db.txt

Checks if the last field is odd or even (the +1 is because the array a is 1-based).

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134