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
How to Debug Linux Kernel Modules with Qemu
Why Do Shells Ignore Sigint and Sigquit in Backgrounded Processes
Linux X64: Why Does R10 Come Before R8 and R9 in Syscalls
Why Doesn't "Sort File1 > File1" Work
Return Code When Oom Killer Kills a Process
Why Percent Signs (%) Do Not Work in Crontab
Why Doesn't the Cd Command Work in My Shell Program
Delete All Files Older Than 30 Days, Based on File Name as Date
Bash: Integer Expression Expected
Assembly: Printf Not Printing New Line
Using Ls to List Directories and Their Total Sizes
How to Exclude Directories from Grep -R
How to Remove All .Svn Directories from My Application Directories
Changing Environment Variable of a Running Process
Using Software Floating Point on X86 Linux
Is Garbage Allowed in High Bits of Parameter and Return Value Registers in X86-64 Sysv Abi
Nginx: Serve Multiple Laravel Apps with Same Url But Two Different Sub Locations in Linux