How to include 4 more JVM instances? - shell

I want to include 4 more JVMs to check logs are exiting or not and also want include those 4 More jvms to pull logs to path, as per below script. How can I modify the script which can used for multiple JVM?
#!/bin/bash
Minute=$(expr `date +%M` - 15)
Hour=$(expr `date +%H`)
Current_Time=$(date +%d/%b/%Y:%H:%M:%S)
f=$(find /apps_01/webapps/JBoss/logs/xxxx JVM1_server*/http_logs/ -name access_log.`date +%Y-%m-%d`.log |wc -l)
if [ "$f" -gt 0 ]
then
tail -500 /apps_01/webapps/JBoss/logs/xxxx JVM1_server*/http_logs/access_log.`date +%Y-%m-%d`.log|awk '{print $4}'| cut -d '[' -f2 >> /home/tmp/xxx_logs
h=$(awk -F'[: ]' '{print $2}' /home/tmp/xxx_logs|tail -1)
if [ "$h" -ge "$Hour" ]
then
for m in `awk -F'[: ]' '{print $3}' /home/tmp/xxx_logs`
do
if [ "$m" -ge "$Minute" ]
then
echo $m
fi
done
fi
fi

Related

bash script loop to check if variable contains string - not working

i have a script which copy files from one s3 bucket to local server, do some stuff and upload it to another s3 bucket.
in the original bucket i have few folders, one of them called "OTHER"
i dot want my script to work on this folder
i tried to define a loop to check if the path string does not contains the string "OTHER" only then to continue to other commands but for some reason it is not working.
what am i doing wrong ?
#!/bin/bash
shopt -s extglob
gcs3='s3://gc-reporting-pud-production/splunk_printer_log_files/'
gcs3ls=$((aws s3 ls 's3://gc-reporting-pud-production/splunk_printer_log_files/' --recursive) | sed 's/^.*\(splunk_printer.*\)/\1/g'| tr -s ' ' | tr ' ' '_')
ssyss3=s3://ssyssplunk
tokenFile=/splunkData/GCLogs/tokenFile.txt
nextToken=$((aws s3api list-objects-v2 --bucket "gc-reporting-pud-production" --prefix splunk_printer_log_files/ --max-items 5) |grep -o 'NEXTTOKEN.*' |awk -F " " '{print $2}')
newToken=$( tail -n 1 /splunkData/GCLogs/tokenFile.txt )
waterMark=$(aws s3api list-objects-v2 --bucket "gc-reporting-pud-production" --prefix splunk_printer_log_files/ --max-items 5 --starting-token
$newToken|sed 's/^.*\(splunk_printer.*zip\).*$/\1/'|sed '1d'|sed '$d')
while true; do
for j in $waterMark ; do
echo $j
if [ "$j" != *"OTHER"* ]; then
gcRegion=$(echo $j | awk -F'/' '{print $2}')
echo "gcRegion:"$gcRegion
if [ "$gcRegion" != "OTHER" ]; then
gcTech=$(echo $j | awk -F'/' '{print $3}')
echo "GCTech:"$gcTech
gcPrinterFamily=$(echo $j | awk -F'/' '{print $4}')
echo "gcPrinterFamily:" $gcPrinterFamily
gcPrinterType=$(echo $j | awk -F'/' '{print $5}')
echo "gcPrinterType:" $gcPrinterType
gcPrinterName=$(echo $j| awk -F'/' '{print $6}')
echo "gcPrinterName:" $gcPrinterName
gcFileName=$(echo $j| awk -F'/' '{print $7}'| awk -F'.zip' '{print $1}')
echo "gcFileName:" $gcFileName
cd /splunkData/GCLogs
dir="/splunkData/GCLogs/$gcRegion/$gcTech/$gcPrinterFamily/$gcPrinterType/$gcPrinterName"
echo "dir:"$dir
mkdir -p $dir
aws s3 sync $gcs3$gcRegion/$gcTech/$gcPrinterFamily/$gcPrinterType/$gcPrinterName/ $dir
find $dir -name '*.zip' -exec sh -c 'unzip -o -d "${0%.*}" "$0"' '{}' ';'
aws s3 cp $dir $ssyss3/$gcRegion/$gcTech/$gcPrinterFamily/$gcPrinterType/$gcPrinterName/ --recursive --exclude "*.zip"
newToken=$( tail -n 1 /splunkData/GCLogs/tokenFile.txt )
nextToken=$(aws s3api list-objects-v2 --bucket "gc-reporting-pud-production" --prefix splunk_printer_log_files/ --max-items 5 --starting-token $newToken |grep -o 'NEXTTOKEN.*' |awk -F " " '{print $2}')
waterMark=$(aws s3api list-objects-v2 --bucket "gc-reporting-pud-production" --prefix splunk_printer_log_files/ --max-items 5 --starting-token $newToken|sed 's/^.*\(splunk_printer.*zip\).*$/\1/'|sed '1d'|sed '$d')
echo "$nextToken" > "$tokenFile"
fi
fi
done
done
You need to use the double-bracket conditional command to turn == and != into pattern matching operators:
if [[ "$j" != *"OTHER"* ]]; then
# ^^ ^^
Or use case
case "$j" in
*OTHER*) ... ;;
*) echo "this is like an `else` block" ;;
esac
Paste your code into https://www.shellcheck.net/ for other things to fix.
I think glenn jackman was on the right path. Try this:
if [[ "$j" != *OTHER* ]]; then
The [[ ]] is required for pattern string matching (and you have to remove the " ). The case statement is also a good idea. You can abandon the shell test altogether and use grep as follows:
if
grep -q '.*OTHER.*' <<< "$j" 2>/dev/null
then
...
fi
Here's a check of the [[ ]]:
$ echo $j
abOTHERc
$ [[ "$j" == *OTHER* ]]
$ echo $?
0
As per BenjaminW., the quotes around $j in [[ ]] are unnecessary. However, the quotes around *OTHER* do make a big difference. See below:
$ j="OTHER THINGS"
$ [[ $j == "*OTHER*" ]] ; echo "$j" matches '"*OTHER*"': $?
OTHER THINGS matches "*OTHER*": 1
$ [[ $j == *OTHER* ]] ; echo "$j" matches '*OTHER*': $?
OTHER THINGS matches *OTHER*: 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

how to add a condition before attaching a file in a shell script in unix?

cd $TGTDIR
count1=`cat File1.csv | wc -l`
count2=`cat File2.csv | wc -l`
(echo "Exceptions/Reports ";
if [ $count1 -gt 0 ]; then uuencode File1.csv File1.csv fi
if [ $count2 -gt 1 ]; then uuencode File2.csv File2.csv fi )
| mailx -s " $WORKFLOW_NAME was successful at $Datetime. " $MAIL_USER
The above code is not running and showing a syntax error for missing ')'.
Can anyone tell me how to add an if condition just before attaching a file in unix shell script.
#!/usr/bin/env bash
count1=`cat ~/File1.txt | wc -l`
count2=`cat ~/File2.txt | wc -l`
echo "Exceptions/Reports "
$(
if [[ $count1 -gt 0 ]]; then
uuencode ~/File1.txt File1.txt
elif [[ $count2 -gt 1 ]]; then
uuencode ~/File2.txt File2.txt
fi
) | mail -s "I was successful at $(date)." your#gmail.com
Hope this will help you
Seems, the Unix server that I am working on is older version and there were some syntax errors:
cd $TGTDIR
count1=cat File1.csv | wc -l
count2=cat File2.csv | wc -l
(echo "Exceptions/Reports ";
if [ $count1 -gt 0 ] then uuencode File1.csv File1.csv fi
if [ $count2 -gt 1 ] then uuencode File2.csv File2.csv fi ) | mailx -s " $WORKFLOW_NAME was successful at $Datetime. " $MAIL_USER
*NO semi colon after if
*the chunk of | mailx -s should be just after the ')' bracket in the same line
Thank you for answering the question. :)

Parsing in bash - extract content from brackets

The input to my bash script can be of the form [fec1::1]:80 or []:80. The second input implies that there's no IP address given. My bash script is to split the input into IP and port. With the said second input, the script should 'understand' that no IP was given.
The following logic seems to solve my problem, on the bash prompt:
$ ip=[]:78
$ echo $ip
[]:78
$ temp=(`echo $ip | awk -F'[][]' '{print $2}'`)
$ echo $temp
$
When I try to do the same thing from within a script, the result is different:
local_endpoint="$1"
printf 'local_endpoint: %s\n' "$local_endpoint"
IN="$local_endpoint"
local_ip=$(echo "$IN" | awk -F'[][]' '{print $2}')
if [ -z "$local_ip" ] ; then
local_ip=$(echo "$IN" | awk -F':' '{print $1}')
local_port=$(echo "$IN" | awk -F':' '{print $2}')
else
local_port=$(echo "$IN" | awk -F'[][]' '{print $3}' | awk -F':' '{print $2}')
fi
printf 'IP: %s\n' $local_ip
printf 'port: %d\n' $local_port
if [ -z "$local_port" -a -z "$local_ip" ] ; then
printf 'No port and IP was given\n'
elif [ -z "$local_ip" ] ; then
printf 'No IP was given\n'
elif [ -z "$local_port" ] ; then
printf 'No port was given\n'
fi
exit 2
Output:
# ./temp.sh []:829
local_endpoint: []:829
IP: []
port: 829
Any idea on what's happening? Also, why do I see the extra comma (,) at the end of the output?
Your script is missing quoting at many places and there are stray commas too in printf. This script should work:
local_endpoint="$1"
printf 'local_endpoint: %s\n' "$local_endpoint"
IN="$local_endpoint"
if [[ "$IN" == "["* ]] ; then
local_ip=$(echo "$IN" | awk -F'[][]' '{print $2}')
local_port=$(echo "$IN" | awk -F'[][]' '{print $3}' | awk -F':' '{print $2}')
else
local_ip=$(echo "$IN" | awk -F':' '{print $1}')
local_port=$(echo "$IN" | awk -F':' '{print $2}')
fi
printf 'IP: <%s>\n' "$local_ip"
printf 'port: <%d>\n' "$local_port"
if [ -z "$local_port" -a -z "$local_ip" ] ; then
printf 'No port and IP was given\n'
elif [ -z "$local_ip" ] ; then
printf 'No IP was given\n'
elif [ -z "$local_port" ] ; then
printf 'No port was given\n'
fi
exit 2
Process substitution is:
var=$(command ...)
not
var=(command ...)

Resources