integer expression expected error. - linux

I don't know why I am getting this error --> : integer expression expected: [: 82
value 82 comes from CURRENT. I am trying to grab the disk space usage and assign to variable called CURRENT and then email out. I don't know where I am going wrong? Any help is appreciated.
#!/bin/bash
ADMIN=email#email.com
THRESOLD=80
CURRENT=$(df -H | grep '/dev/mapper/cvs-cvs' | awk '{ print $5}' | cut -d'%' -f1)
if [ $CURRENT -ge $THRESOLD ]; then
echo "My CVS disk space usage is $CURRENT %" | mailx -s "Disk Space Usage" $ADMIN
fi

Probably your script is in DOS format that the assignment includes \r The extra value appended to the number causes the error. Try converting it first with either of the following commands:
sed -i 's|\r||' script
dos2unix script

It looks like your CURRENT value is not being assigned to an integer from the command substitution output.
I'd recommend running df on the directory you want, then parse the output.
#!/bin/bash
ADMIN=email#email.com
THRESOLD=80
read -ra DF_OUT < <(df -H /dev/mapper/cvs-cvs 2>&1|tail -n 1)
if [[ ${DF_OUT[#]} != "df:"* ]]; then
CURRENT=${DF_OUT[4]/\%/}
if [ $CURRENT -ge $THRESOLD ]; then
echo "My CVS disk space usage is $CURRENT %" | mailx -s "Disk Space Usage" $ADMIN
fi
else
echo -e "df encountered the following error:\n$DF_OUT"
fi
This first checks for an error on the df output before proceeding to parse the results for disk usage.
As others suggested, using -x with the script helps diagnose things.

try
#!/bin/bash
ADMIN=email#email.com
let "THRESOLD=80"
declare -i CURRENT
let CURRENT=$(df -H | grep '/dev/sda1' | awk '{ print $5}' | cut -d'%' -f1)
if (($CURRENT >= $THRESOLD )) ; then
echo "My CVS disk space usage is $CURRENT %" | mailx -s "Disk Space Usage" $ADMIN
fi

Related

Syntax error near unexpected token `fi` - Linux

i am using WSL (Ubuntu) in Windows. i used bash script.sh for the script below:
#! /bin/sh
#################LOAD FILES###################
lead_SNPs=`grep "lead_SNPs" ../prep/files.txt | cut -f2`
bfile=`grep -w "bfile" ../prep/files.txt | cut -f2`
bfile_list=`grep -w "bfile_list" ../prep/files.txt | cut -f2`
r2=`grep "r2" ../prep/parameters.txt | cut -f2`
###############LD###########################
if [ ${bfile} = "NA" ]; then
cat ${bfile_list} | while read line; do
file=${line}
file_n=`echo $file |awk -F '/' '{print $NF}'`
echo 'Calculating LD'
plink --bfile ${file} --r2 --ld-window-kb 1000 --ld-window 999999 --ld-window-r2 ${r2} --ld-snp-list ${lead_SNPs} --out C:/Users/naghm/Desktop/FDSP-github/ld/${file_n}
done
else
file=${bfile}
file_n=`echo $file |awk -F '/' '{print $NF}'`
echo ${file_n}
plink --bfile ${file} --r2 --ld-window-kb 1000 --ld-window 999999 --ld-window-r2 ${r2} --ld-snp-list ${lead_SNPs} --out C:/Users/naghm/Desktop/FDSP-github/ld/${file_n}
fi
but i get this error
Syntax error near unexpected token `fi`
can you correct my code please? i can not understand where i made mistake.
Try to change:
if [ ${bfile} = "NA" ]; then
to
if [ "${bfile}" = "NA" ]; then
I suspect ${bfile} is empty which expanded to:
if [ = "NA" ]; then
in your original line.
The first line in your script doesn't look right.
It should be
#!/bin/sh
or if using bash shell:
#!/bin/bash

issues with grep variable and white space

I am trying to get this bit of code to work. and I am getting hung up on the second piped grep with the variable $pkgname. I am unable to find a way to get it to read the variable correctly either I get no output or as the code is currently written I get grep: illegal byte sequence. if I put either text with no space in the variable it works or I enter the text as part of the grep statement it works.
#!/bin/bash
counter=0
results2=Executing\ SSH\ MOTD\ banner
pkgname=SSH\ MOTD\ banner
until [ $counter = 1 ]
do
echo $counter
echo $pkgname
echo $results2
result=$(grep "$(date +"%b %d")" /var/log/test.log | grep “$pkgname” | cut -d':' -f 4 | sed 's/^ *//g')
echo $result
if [ “$result” == “$results2” ]; then
counter=1
fi
done
echo finished
so the log file line I am looking for looks like this.
Tue Jun 28 10:58:57 machinename process: Executing SSH MOTD banner
change to
pkgname="SSH MOTD banner" # Here use quotes to avoid using \
As [ #jack ] rightly pointed out in this [ comment ], you need the neutral quotation mark " for a variable to be expanded. That said you can simplify the regex to below
pkgname="SSH MOTD banner"
d=$(date +"%b %d")
result="$(awk -v FS=":" -v d="$d" -v pkg="$pkg_name" '{if($0 ~ date && $0 ~ pkg){sub(/^ */,"",$4);print $4}}' /var/log/test.log)"

Bash integer expression on IF statement when retrieving free memory

I am getting the below error while running the script memory.sh script:
[root#test tmp]# ./memory.sh
./memory.sh: line 3: [: 2.05028: integer expression expected
Normal
The content of the memory.sh script is:
[root#test tmp]# cat memory.sh
threshold=80
MEMORY=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
if [ ${MEMORY} -gt ${threshold} ]; then
sudo sync;echo 3 > /proc/sys/vm/drop_caches
else
echo "Normal"
fi
Does anyone know how to prevent this error?
The error on the IF statement comes up when retrieving float values from
the Memory.
The following script casts the float memory value to an integer for the comparison:
#!/bin/bash
threshold=80
memory=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
castedMemory=$(echo $memory | cut -d'.' -f1)
if [[ "$castedMemory" -gt $threshold ]]; then
sudo sync
echo 3 > /proc/sys/vm/drop_caches
else
echo "Normal"
fi
Looks like you need to convert the computation result into integer type to be able to compare it. What about that suggestion:
#!/bin/bash
threshold=80
MEMORY=$(free | grep Mem | awk '{print int($3/$2 * 100.0)}')
if [ ${MEMORY} -gt ${threshold} ]
then
sudo sync;echo 3 > /proc/sys/vm/drop_caches
else
echo "Normal"
fi
This does not throw an error for me but outputs a friendly "Normal" ;-)

Error Shell Script

When I try to run this script this error appears : operating extra /home/ubuntu/Desktop/Destino/, and I do not know why , someone help me please.
#!/bin/bash
input="/home/ubuntu/Desktop/Output/SAIDA.txt"
dt=`date +"%Y%m%d%H%M%S"`
layout='C'
if [ -e "$input" ] ; then
header=$(head -n 1 $input)
export header
tail -n +2 $input | split -l 99 -d --additional-suffix=.txt \ --filter='{ printf %s\\n "$header"; cat; }' >/home/ubuntu/Desktop/Destino/$FILE - NOMENCLATURA_${dt}_
for arquivo in ´Is/home/ubuntu/Desktop/*.txt´
do
NOME= ´cat $arquivo | cut -d "." -f1´
touch/home/ubuntu/Desktop/Destino/$NOME.cfg
echo $dt > $NOME.cfg
echo $layout > $NOME.cfg
done
else
echo "The input file does not exist."
fi
You have some strange quote characters in your script. To substitute the output of a command, wrap it with $() or backticks, not ´ characters.
for arquivo in ´Is/home/ubuntu/Desktop/*.txt´
I guess Is was meant to be ls, but you left out the space after it. But there's no need to parse the output of ls, just use the wildcard directly.
for arquivo in /home/ubuntu/Desktop/*.txt
On this line:
tail -n +2 $input | split -l 99 -d --additional-suffix=.txt \ --filter='{ printf %s\\n "$header"; cat; }' >/home/ubuntu/Desktop/Destino/$FILE - NOMENCLATURA_${dt}_
you need to put the output filename in quotes because of the spaces.
tail -n +2 $input | split -l 99 -d --additional-suffix=.txt \ --filter='{ printf %s\\n "$header"; cat; }' >"/home/ubuntu/Desktop/Destino/$FILE - NOMENCLATURA_${dt}_"
Also, the FILE variable is not set, you need to assign that earlier.
On this line:
NOME= ´cat $arquivo | cut -d "." -f1´
you're again using the wrong type of quotes to capture the output of the command. Also, you must not have a space between = and the value you want to assign. It should be:
NOME=$(cat $arquivo | cut -d "." -f1)
There's no need to do export header. The variable is only being used in this script, not in any child processes.

Total count of the array values

Here I'm accepting few mount points from the user and using each value to get space available on the host.
./user_input.ksh -string /m01,/m02,/m03
#!/bin/ksh
STR=$2
function showMounts {
echo "$STR"
arr=($(tr ',' ' ' <<< "$STR"))
printf "%s\n" "$(arr[#]}"
for x in "${arr[#]}"
do
free_space=`df -h "$x" | grep -v "Avail" | awk '{print $4}'`
echo "$x": free_space "$free_space"
done
#echo "$total_free_space"
}
Problems:
How can I exit for loop if any of the user input mount not avaialble?
currently it only add error in the log.
How to get total_free_space (i.e. sum of free_space)?
If you want to keep your code , test this (no ksh here). If you don't care, read Ed Morton's answer.
./user_input.ksh -string /m01,/m02,/m03
#!/bin/ksh
STR=$2
function showMounts {
echo "$STR"
arr=($(tr ',' ' ' <<< "$STR"))
printf "%s\n" "${arr[#]}"
for x in "${arr[#]}"; do
free_space=$(df -P "$x" | awk 'NR > 1 && !/Avail/{print $4}')
echo "$x: free_space $free_space"
((total_free_space+=$free_space))
done
echo "$((total_free_space/1024/1000))G"
}
showMounts
Caution:
"${arr[#]}"
not
"$(arr[#]}"
As I said in your last question, you do not need ANY of that, all you need is a one-liner like:
df -h "${STR//,/ }" | awk '/^ /{print $5, $3; sum+=$3} END{print sum}'
I have to say "like" because you haven't shown us the df -h /m01 /m02 /m03 output yet so I don't know exactly how to parse it.

Resources