empty file that is over 19% percent full - bash

I am trying to write a script which empties file which is reaches 19% or more. I have the following code:
#!/bin/sh
ALERT="19" # setting alert to 19
df -h | grep /tmp | awk '{print $4}' | while read output; # reading the file system
do
echo $output
usep=$(echo $output | awk '{print $4}' | cut -d'%' -f1 )
if [ $usep -gt ALERT ]; then
cd /tmp
cat /dev/null > purge_foreign_mdn.log # filename
fi
done
I am getting this error:
purge_file.sh: line 10: [: -gt: unary operator expected
I tried -gt "19" and -gt '19'. Still getting different errors but at the same line.

You need to use $ALERT, not ALERT. As it is, you are comparing the $usep variable to the string ALERT, not the variable called $ALERT.

Related

Bash - disk utilization notification

This script should output a warning notification for the utilization of the main disk if over 50%, but it provides no output. My disk is currently sat at 60% so it should in theory work.
I have added an else statement to identify if the loop is not working but the else statement isnt triggered.
I'm provided no error so its hard to identify where i have gone wrong specifically.
#!/bin/bash
df -H | grep /dev/sda2 | awk '{ printf "%d", $5}' > diskOutput.txt
input="diskOutput.txt"
while IFS= read -r line
do
if [ $line -gt 50 ]
then
up="`uptime | cut -b 1-9`"
output="WARNING UTILISATION $line - $up"
echo "$output"
else
echo "no-in"
fi
done < $input
#rm diskOutput.txt
echo "finished"
Try this.
#!/bin/bash
df -H | grep /dev/sda2 | awk '{ printf "%d", $5}' > diskOutput.txt
echo "" >>diskOutput.txt
input="diskOutput.txt"
while IFS= read -r line
do
if [ $line -gt 50 ]
then
up="`uptime | cut -b 1-9`"
output="WARNING UTILISATION $line - $up"
echo "$output"
else
echo "no-in"
fi
done < $input
#rm diskOutput.txt
echo "finished"
You are setting an internal field separator as space here.
while IFS= read -r line
But when creating file, with %d you are removing all char except digits.

What's wrong with my function() in Shell Script! Syntax Token errors near {?

#!/bin/bash
# I get the newest file in Directory
latest_file=$(ls -t | head -n 1)
getAlldoublicate()
# getting Token syntax error here getAlldoublicate() '{
{
Alldoublicate=$(tr -s ',' ' ' <latest_file | awk '{print $2" "$3" "$4}' | uniq -d)
# here I try to find dublicate rows in csv
}
if [[ -s latest_file]] ; then
# here I check if file is emty
getAlldoublicate
else
cat "$latest_file" | mailx -s "$latest_file is empty" bla..`#bla
fi
I guess this is your code.
#!/bin/bash
# I get the newest file in Directory
latest_file=$(ls -t | head -n 1)
# getting Token syntax error here
getAlldoublicate()
{
# here I try to find dublicate rows in csv
Alldoublicate=$(tr -s ',' ' ' < $1 | awk '{print $2" "$3" "$4}' | uniq -d)
}
if [[ -s $latest_file ]]; then
# here I check if file is emty
getAlldoublicate $latest_file
else
cat $latest_file | mailx -s "$latest_file is empty" bla.. #bla
fi
Three points you need to pay attention:
function must be defined first before being use.
You can pass the latest_file as an argument when calling getAlldoublicate. Then you could use it by $1 in the function. ($0 stands for the function being called itself).
It would be better if you read the How to Format Tutorials before asking questions.

./test.ksh[9]: [: argument expected

We are running to script to find the zfs file system monitoring and having error as below.
argument expected
#!/bin/sh
USED_SPACE_PERCENT_WARN=20
PATH=/usr/bin:/usr/sbin; export PATH
# check zfs File system
if [ `df -F zfs | wc -l` -gt 0 ]; then
/usr/sbin/zpool list -H | while read line
do
USED_SPACE_PERCENT=`echo "$line" | nawk -F'[ % ]+' '{print $5}'`
if [ $USED_SPACE_PERCENT -gt $USED_SPACE_PERCENT_WARN ]; then
POOL=`echo "$line" | nawk -F'[ % ]+' '{print $1}'`
echo "ZFS pool $POOL has used $USED_SPACE_PERCENT% of its space."
fi
done
fi
When USED_SPACE_PERCENT is empty (line without 5 fields), the command
if [ $USED_SPACE_PERCENT -gt $USED_SPACE_PERCENT_WARN ]; then
will turn into
if [ -gt 20 ]; then
and that causes the error

Convert Floating Point Number To Integer Via File Read

I'm trying to get this to work when the "line" is in the format ###.###
Example line of data:
Query_time: 188.882
Current script:
#!/bin/bash
while read line; do
if [ $(echo "$line" | cut -d: -f2) -gt 180 ];
then
echo "Over 180"
else
echo "Under 180"
fi
done < test_file
Errors I get:
./calculate: line 4: [: 180.39934: integer expression expected
If you have:
line='Query_time: 188.882'
This expression:
$(echo "$line" | cut -d: -f2) -gt 180
Will give an error invalid arithmetic operator since BASH cannot handle floating point numbers.
You can use this awk command:
awk -F ':[ \t]*' '{print ($2 > 180 ? "above" : "under")}' <<< "$line"
above
You can use this awk:
$ echo Query_time: 188.882 | awk '{ print ($2>180?"Over ":"Under ") 180 }'
Over 180
It takes the second space delimited field ($2) and using conditional operator outputs if it was over or under (less than or equal to) 180.

execute mailx using shell script

I am trying to run the below script.
val = `wc -l /home/validate.bad | awk '{print $1}' | tail -n1`
valCount = `wc -l /home/validation.txt | awk '{print $1}'`
if [ "$val" -gt 1 ] && ["$valCount" -gt 1]
then
mailx -s "Validation failed" -r xyz#abc.com xyz#abc.com<<-EOF
Hi ,
Validation has failed. Please check.
EOF
elif [ "$valCount" -gt 1 ]
then
mailx -s "Validation pass" -r xyz#abc.com xyz#abc.com<<-EOF
Hi Team,
Validation success.
EOF
fi
But I am getting this error.
Error:
val: comand not found
valCount: command not found
line 3[: : integer expression expected
You can't have spaces around = :
val = `wc -l /home/validate.bad | awk '{print $1}' | t` # wrong
and should have been
val=`wc -l /home/validate.bad | awk '{print $1}' | t`
or preferrably
val=$(wc -l </home/validate.bad)
#`..` is legacy , $() supports nesting, one good reason to go for it
# You use awk and tail uselessly
Also
["$valCount" -gt 1]
should have been
[ "$valCount" -gt 1 ] # mind the spaces for the test constructie
# [spaceSTUFFspace] is the correct form
Sidenote
You may use [ shellcheck ] to check your scripts.

Resources