Can you use logical operators in a case statement (bash)?
I'm pretty sure I already know the answer to this, but I want to make sure I'm not overlooking anything. I'm working on a log monitoring script and every 10 lines I want to display a summary of events. The thing is, there are a lot of possible events, that likely won't have happened, so I only want to include them in the summary if they've actually happened.
Right not I'm doing this through mulitple if statements, but that's incredibly ugly... I'm wondering if it's possible to do something like this (which does not work):
Code:
#!/bin/bash
a=5
case -n in
$a)
echo "yep"
;;
esac
The only other solution I could come up with was to write a function along these lines:
Code:
checkExist ()
{
if [[ -n $1 ]];then
case $1 in
possibleVariable)
yadda
;;
esac
fi
}
Actually, now that I'm typing all of this out...I think that's how I'll go about it, but I'm still curious if you can use logic in case, or if it's just string matching.
In case anyone cares, here is my script thus far (it's still in progress, so the formatting may be off)
Code:
#!/bin/bash
######################################################
# Program: logTail.sh
# Date Created: 3 June 2010
# Date Updated: NA
# Developer: DeCoTWC (Support Engineer)
# Description: Tails manager log and plays a single chirp for warnings and a double chirp for errors
######################################################
i=0
attempts=0
complete=0
live=0
streams=0
startTime=$(tail /opt/XXX/logs/manager.log|head -1|awk '{print $1,$2}')
missingRecent=0
dubFeed ()
{
echo && echo && echo && echo
}
sep ()
{
printf "+-++-++-++-++-++-++-++-++-++-+++-++-++-++-++-++-++-++-++-++-++-++-++-++-++\n"
}
tail -100 /opt/XXX/logs/manager.log|
while read line;do
line=($line)
case "${line[6]}" in
33102)
x=$(echo "${line[*]}"|awk '{print $13}')
y=$(echo "${line[*]}"|awk '{print $17}')
complete=$(($complete+$x))
attempts=$(($attempts+$y))
;;
33085)
live=$(($live+1))
liveRecent=$(echo "${line[0]}" "${line[1]}")
;;
17030)
streams=$(($streams+1))
streamRecent=$(echo "${line[0]}" "${line[1]}")
;;
17031)
missingAsset=$(($missingAsset+1))
missingRecent=$(echo "${line[0]}" "${line[1]}")
;;
17023)
EOS=$(($EOS+1))
EOSRECENT=$(echo "${line[0]}" "${line[1]}")
;;
esac
if [[ "${line[2]}" == "E" ]];then
echo "${line[*]}"
printf "\a"
sleep .5
printf "\a"
sleep .5
let i=i+1
elif [[ "${line[2]}" == "W" ]] && [[ "${line[8]}" != NTP ]];then
echo "${line[*]}"
printf "\a"
sleep .5
let i=i+1
else
echo "${line[*]}"
let i=i+1
fi
if [[ $i -eq 10 ]];then
dubFeed
sep
printf "Summary since $startTime\n"
printf "$complete Successful of $attempts recordings\n"
printf "$live live recordings started \t Most recent at $liveRecent\n"
printf "$streams successful sessions set up \t Most recent at $streamRecent\n"
if [[ -n $missingAsset ]];then
printf "$missingAsset session failures due to missing assets. \t Most recent at $missingRecent\n"
fi
sep
dubFeed
i=0
fi
done
---------- Post updated 07-14-10 at 03:37 AM ---------- Previous update was 07-13-10 at 01:16 PM ----------
In case anyone cares...
This is the (more or less) finished project. It's still a bit wonky in places, and I'd appreciate any input on how I could have streamlined this, or made it more efficient
Code:
[root@mgmt01 utils]# cat logTail.sh
#!/bin/bash
######################################################
# Program: logTail.sh
# Date Created: 3 June 2010
# Date Updated: 13 July 2010
# |_Added summary
# |_formatting fixes
# Developer: DeCoTWC (Support Engineer)
# Description: Tails manager log and plays a single chirp for warnings and a double chirp for errors
######################################################
#Colour variables
#################
pur='\e[0;35m'
nc='\e[0m'
#Zero out all values
####################
i=0
attempts=0
complete=0
ingestFail=0
live=0
streams=0
underFlow=0
writeFail=0
startTime=$(tail /opt/XXX/logs/manager.log|head -1|awk '{print $1,$2}')
missingRecent=0
EOS=0
phantom=0
#Function to insert two blank lines
###################################
dubFeed ()
{
echo && echo
}
#Function to draw separator
###########################
sep ()
{
printf "${pur}+-++-++-++-++-++-++-++-++-++-+++-++-++-++-++-++-++-++-++-++-++-++-++-++-++${nc}\n"
}
#Function to check for each kind of log entry, and print running totals
#######################################################################
sumVerify ()
{
if [[ $1 -ne 0 ]];then
case $1 in
$EOS)
printf "$EOS sessions torn down due to EOS or Pause. \t Most recent at $EOSRECENT\n"
;;
$missingAsset)
printf "$missingAsset session failures due to Missing Assets \t Most recent at $missingRecent\n"
;;
$deletedAsset)
printf "$deletedAsset assets deleted. \t\t\t\t Most recent at $deletedRecent\n"
;;
$live)
printf "$live live recordings started \t\t\t Most recent at $liveRecent\n"
;;
$complete)
printf "$complete Successful of $attempts recordings\n"
;;
$streams)
printf "$streams successful sessions set up \t\t Most recent at $streamRecent\n"
;;
$phantom)
printf "$phantom phantom asset deletion requests \t\t Most recent at $phantomRecent\n"
;;
$ingestFail)
printf "$ingestFail generic ingest failures \t Most recent at $ingestRecent\n"
;;
underFlow)
printf "$ingestFail ingest failures due to underflow \t Most recent at $underRecent\n"
;;
writeFail)
printf "$writeFail ingest failures due to write fail \t Most recent at $writeRecent\n"
esac
fi
}
#Start tailing log and breaking down informaiton
################################################
tail -F /opt/XXX/logs/manager.log|
while read line;do
line=($line)
#Start parsing each line of the logs to see if it's a known value we're checking for
####################################################################################
case "${line[6]}" in
33102)
x=$(echo "${line[*]}"|awk '{print $13}')
y=$(echo "${line[*]}"|awk '{print $17}')
complete=$(($complete+$x))
attempts=$(($attempts+$y))
;;
33085)
live=$(($live+1))
liveRecent=$(echo "${line[0]}" "${line[1]}")
;;
17030)
streams=$(($streams+1))
streamRecent=$(echo "${line[0]}" "${line[1]}")
;;
17031)
missingAsset=$(($missingAsset+1))
missingRecent=$(echo "${line[0]}" "${line[1]}")
;;
17023)
EOS=$(($EOS+1))
EOSRECENT=$(echo "${line[0]}" "${line[1]}")
;;
33079)
deletedAsset=$(($deletedAsset+1))
deletedRecent=$(echo "${line[0]}" "${line[1]}")
;;
33078)
phantom=$(($phantom+1))
phantomRecent=$(echo "${line[0]}" "${line[1]}")
;;
33048)
if [[ "${i[13]}" == "(50409)" ]];then
underFlow=$(($underflow+1))
underRecent=$(echo "${line[0]}" "${line[1]}")
elif [[ "${i[13]}" == "(50009)" ]];then
writeFail=$(($writeFail+1))
writeRecent=$(echo "${line[0]}" "${line[1]}")
else
ingestFail=$((ingestFail+1))
ingestRecent=$(echo "${line[0]}" "${line[1]}")
fi
esac
#Check if each log line is an error. If so, ring system bell twice
##################################################################
if [[ "${line[2]}" == "E" ]];then
echo "${line[*]}"
printf "\a"
#sleep .5
printf "\a"
#sleep .5
let i=i+1
#Check if each line is a warning. If so ring system bell once
#############################################################
elif [[ "${line[2]}" == "W" ]] && [[ "${line[8]}" != NTP ]];then
echo "${line[*]}"
printf "\a"
#sleep .5
let i=i+1
else
echo "${line[*]}"
let i=i+1
fi
#If 10 log lines have passed, print summary and reset counter
#############################################################
if [[ $i -eq 10 ]];then
dubFeed
sep
printf "Summary since $startTime\n"
sumVerify $complete
sumVerify $live
sumVerify $streams
sumVerify $deletedAsset
sumVerify $missingAsset
sumVerify $EOS
sumVerify $phantom
printf "Time now is $(date "+%D %H:%M:%S")\n"
sep
dubFeed
i=0
fi
done
I'm having an issue with bash read input when using a case statement.
The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here.
defaultans=8hrs
read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Hi Gurus,
I have the below BASH code which does not works for upper case alphabets except Z (upper case Z).
What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working.
case $var in
)
echo "Lower case alphabet"
;;
... (7 Replies)
Hello All,
I am writing a script that is to be placed on multiple servers, and of course I've started
running into some compatibility issues for certain shell commands.
The code below worked just fine on most of my machines except for a couple.
Here I had 4 separate lines in my script that... (3 Replies)
I have the following bash script and it is not accepting the lines
"--"|"--""-")
"--""-"")
while
do
echo "Current Argument is ${1}"
case "$1" in
"--"|"--""-")
echo "Argument is ${1}"
shift # Skip ahead one to the next argument.
... (1 Reply)
Hi
I want to send a status mail if daily or weekly or monthly batch completed or aborted. Here is the code.
if && &&
||
Else if &&
&& ||
Else if &&
&& ||
then
mailx –s “Status Report” sumone@sumthing.com
else
print ”try again”
Plz suggest the changes. (3 Replies)
Hi,
shell is /bin/ksh
I am trying to do the following in my code.. but its showing me an error
if ] && ] ]]; then
echo "id is $ida and chk_dy is $chk_dy"
fi
the error I get is
syntax error at line 23 : `"$ida"' unexpected
I need to execute the... (2 Replies)
Hi All,
I have 2 variables. Result1 and Result2. I want to put a condition that if Both are True then echo "All True" Else Show Error.
Right now i am doing this and getting error.
if ;
then
echo "All True"
else
echo "Failed"
fi;
Error.
line 8: '
Solution: Looking for (2 Replies)
greetings,
I have a script that is taking input like this:
a
b
c
d
aa
bb
aaa
bbb
ccc
ddd
and formating it to be like this:
a b c d
aa bb
aaa bbb ccc ddd (4 Replies)
Hi,
I'm trying to write a routine to parse a file that contains data that will be read
into arrays. The file is composed of labels to identify data types and arbitrary
lines of data with the usual remarks and empty new lines as is common with
config files.
The initial pass is built as so:... (3 Replies)
Hi there, probably a really simple question to answer but i cant seem to find it
can I use a logical AND (&&) within a CASE statement ie (ps this is useless syntax but youll get the idea
case "$var1","$var2" in
'Billy' && 'Bobby')
... (1 Reply)