Bash: Integer Expression Expected

Integer expression expected error in shell script

You can use this syntax:

#!/bin/bash

echo " Write in your age: "
read age

if [[ "$age" -le 7 || "$age" -ge 65 ]] ; then
echo " You can walk in for free "
elif [[ "$age" -gt 7 && "$age" -lt 65 ]] ; then
echo " You have to pay for ticket "
fi

Integer expression expected BASH scripting IF statement

You need to enclose the variable assignment in $(...) ("command substitution"):

v=$(expr $# % 2)

In the if statement, -eq should be correct. Also, to make sure it works, I would use double square brackets (this might depend on the shell you use):

if [[ ${v} -eq 0 ]]; then

Integer Expression expected; error when moving files

3.00 is a float value. Bash cannot process floating point values, only integers, hence the error message.

You could treat it as a string and compare it alphabetically if you can get your number in a fixed format (e.g. with two digits after the decimal point).

But I'd rather advise you use the command bc which is specifically made for calculations:

if [ $(bc <<< "$resolution > 3.00") == 1 ] ; then
mv $f $Above3
fi

What does the script (as a summary):

What does the script?

  • it takes all the files corresponding to *.ent in the current directory

  • it takes all the lines containing "REMARK 2 RESOLUTION." from all these files ; it takes the 4th word of these lines ; and puts the result into bash variable $resolution

  • if $resolution is greater than 3.00 it moves the corresponding file (one of the *.ent files) into the directory /mnt/d/Documents/Research/PhD/PhD/Research/Results/PDB/New/XRD/Above3

  • then it loops to proceed with the next file whose name matches *.ent

newprime.sh: line 14: [: 0+1: integer expression expected

expr requires parameters to be passed as separate arguments. Quoting the POSIX standard for expr:

The application shall ensure that each of the expression operator symbols [...] and the symbols integer and string in the table are provided as separate arguments to expr.

The code here is appending all the operators into a single argument, hence your problem.


Thus:

c=$(expr "$c" + 1)

...NOT...

c=$(expr $c+1)

But don't do that at all. It's more efficient and more readable to write:

c=$(( c + 1 ))

Error Integer Expression Expected in Bash script

There is a small change required in your code.
You have to use tilt "`" instead of single quotes "''" inside if.

if [ `ps -ef | grep "bash -i" | grep -v grep | wc -l`  -eq 0 ]

This worked for me. Hope it helps you too.

wc -l giving integer expression expected error

I just tried this:

wc -l test.txt

With following result:

5 test.txt

So the result contains a number, followed by the name of the file.

You can solve it like this:

wc -l test.txt | awk '{print $1}'

This only gives the number.

So, you might change your code into:

$ if [ "$(wc -l test.txt  | awk '{print $1}')" -gt "$5" ]; then

... and now correct:

$ if [ $(wc -l test.txt  | awk '{print $1}') -gt 5 ]; then

Bash Script integer expression expected , (floats in bash) replace part with awk

An alternative to awk is bc , something like:

#!/usr/bin/env bash

#define what values are too high (%)
inacceptableRAM="90"

#check RAM %
ram=$(free | awk '/Mem/{print $3/$2 * 100.0}')

#send Alarm for RAM
if (( $(bc <<< "$ram > $inacceptableRAM") )) ; then
echo "Alarm RAM usage is @$ram"
fi


Related Topics



Leave a reply



Submit