execute mailx using shell script - shell

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.

Related

Provided script throws error while executiong-Grep unmatched

Guys when i run the below script I get error grep: Unmatched ( or (
Also Iam unable to understand the grep performed in the shell script for fetching the definition of a word from web.
#!/bin/sh
# define - given a word, return its definition from dictionary.com
url="http://www.cogsci.princeton.edu/cgi-bin/webwn2.0?stage=1&word="
if [ $# -ne 1 ] ; then
echo "Usage: $0 word" >&2
exit 1
fi
lynx -source "$url$1" | \
grep -E '(^[[:digit:]]+\.| has [[:digit:]]+$)' | \
sed 's/<[^>]*>//g' |
( while read line
do
if [ "${line:0:3}" = "The" ] ; then
part="$(echo $line | awk '{print $2}')"
echo ""
echo "The $part $1:"
else
echo "$line" | fmt | sed 's/^/ /g'
fi
done
)
exit 0

If condition for "not equal" is not working as expected in shell script

#!/bin/bash
a=2
b=2
COUNTER=0
sam="abcd"
sam1="xyz"
sam2="mno"
for x in ls | grep .rpm
do
`C=rpm -qpR $x | grep -v CompressedFileNames | grep -v PayloadFilesHavePrefix | wc -l`
if [ "sam2"!="$sam1" ]
then
echo "${sam1}"
echo "${sam2}"
if [ $C -eq $a ]
then
COUNTER=$((COUNTER+1))
echo "${x}"
eval sam=$x
#eval sam1=sam | cut -d '-' -f 1
sam1=`echo "${sam}"| cut -d '-' -f 1`
if [ $COUNTER -eq $b ]
then
break
fi
fi
fi
sam2=`echo "${x}"| cut -d '-' -f 1`
done
This is the output I am getting:
xyz
mno
comps-4ES-0.20050107.x86_64.rpm
comps
comps
comps-4ES-0.20050525.x86_64.rpm
My question is: why is the if condition returning true despite sam1 and sam2 being equal? I have checked for non-equality.
Response is the same even if I use
if [ $C -eq $a ] && [ "$sam2" != " $sam1" ]
As Ansgar Wiechers pointed out, you're missing a "$" in front of the sam2 variable. That way, you're comparing the literal string "sam2" with the string value of $sam1 (which initially is set to "xyz"). What you want to do is compare the string values of both variables:
if [ "$sam2" != "$sam1" ]
Regarding $C, you should only include the commands to be evaluated inside backticks, not the evaluation itself. This is called a command substitution - a subshell is created in which the commands are executed, and the backtick expression is substituted by the computed value. The line should look like this:
C=`rpm -qpR $x | grep -v CompressedFileNames | grep -v PayloadFilesHavePrefix | wc -l`
Your for loop also needs a command substitution: for x in ls | grep .rpm makes it look as if you're piping the output of a for command into grep. What you want to do is iterate over the ls | grep part, which you can do with the following command substitution:
for x in `ls | grep .rpm`
Hi Guys Got the solution:
#!/bin/bash
read -p "enter dep number" a
read -p "enter no of rpms" b
COUNTER=0
sam="abcd"
sam1="xyz"
sam2="mno"
for x in `ls | grep .rpm`
do
C=`rpm -qpR $x |grep -v CompressedFileNames | grep -v PayloadFilesHavePrefix | wc -l`
# echo "${C}:c"
if [ $C -eq $a ] && [ "$sam2" != "$sam1" ]
then
COUNTER=$((COUNTER+1))
# echo "${COUNTER}:counter"
# echo "${x}"
eval sam=$x
#eval sam1=sam | cut -d '-' -f 1
sam1=`echo "${sam}"| cut -d '-' -f 1`
if [ $COUNTER -eq $b ]
then
break
fi
fi
sam2=`echo "${x}"| cut -d '-' -f 1`
#echo "${sam2}"
#echo "${sam1}"
done

./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

Receiving conditional operator expected error in bash

Please excuse this extremely inefficient script, I am new to shell scripting. I am receiving an error near the if clause in the function matchFS(). I have posted the error down below. Can anyone offer me some guidance?
#!/bin/bash
function matchFS() {
usage=$(df -h | tail -n +2 | awk '{print $5}' | sed 's/%//g')
usagearr=( $usage )
for i in "${usagearr[#]}"
do
if [[ $1 eq "${usagearr[$i]}" ]]; then
# print matching row from df -h
fi
done
}
usage=$(df -h | tail -n +2 | awk '{print $5}' | sed 's/%//g')
usagearr=( $usage )
len=${#usagearr[#]}
for (( i=0; i<$len; i++ )) # we have to use (( )) here to represent the c style for loop
do
if [ "${usagearr[$i]}" -gt "10" ]; then
matchFS ${usagearr[$i]}
fi
done
Error: line 13: conditional binary operator expected
line 13: syntax error near eq'
line 13: if [[ $1 eq "49 ]]; then'
If you look at help test you'll quickly realize that eq is not one of the choices. At least, not without adding something else to it.
#!/bin/bash
function matchFS() {
### duplicate definition, these are already known to the function.
usage=$(df -h | tail -n +2 | awk '{print $5}' | sed 's/%//g')
usagearr=( $usage )
### you probably did want to use another variable here,
### because the "i" is also shared with the caller
for i in "${usagearr[#]}"
do
### -eq instead of eq
if [[ $1 -eq "${usagearr[$i]}" ]]; then
### the if statement can not be empty
# print matching row from df -h
fi
done
}
usage=$(df -h | tail -n +2 | awk '{print $5}' | sed 's/%//g')
usagearr=( $usage )
len=${#usagearr[#]}
for (( i=0; i<$len; i++ )) # we have to use (( )) here to represent the c style for loop
do
if [ "${usagearr[$i]}" -gt "10" ]; then
matchFS ${usagearr[$i]}
fi
done

Bash error echo a command

I have a problem. I need to show a echo from a while, I use two echo the first one work but the second it give a error.
#!/bin/bash
conexiuni="/tmp/conexiuni"
if [ "$1" != "" ]; then
netstat -tuan | grep $1 | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n > $conexiuni
else
netstat -tuan | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n > $conexiuni
fi
cat $conexiuni | while read line
do
con=`echo ''$line'' | awk '{print $1}'`
ip=`echo ''$line'' | awk '{print $2}'`
if [ "$con" -gt "4" ]; then
`echo -e "$ip" >> /var/log/drop_sc_ip`
`echo -e "$ip"`
fi
done
if [ -f "$conexiuni" ];
then
`rm -rf $conexiuni`
fi
The error is :
./show_conn: line 15: 8.97.80.2: command not found
./show_conn: line 15: 8.76.109.13: command not found
./show_conn: line 15: 8.33.15.2: command not found
./show_conn: line 15: 9.118.226.3: command not found
You can write this part without the backticks:
if [ "$con" -gt "4" ]; then
echo -e "$ip" >> /var/log/drop_sc_ip
echo -e "$ip"
fi
also same in this part:
rm -rf $conexiuni
with the backticks, it first executes what is inside the backticks and then tries to execute the output of the backticks.
and change the loop:
while read con ip
do
if [ "$con" -gt "4" ]; then
echo -e "$ip" >> /var/log/drop_sc_ip
echo -e "$ip"
fi
done < $conexiuni

Resources