Linux script shows unnecessary files - linux

I have written following script and it shows some unnecessary files when i'm running it. I just want to execute only the command and receive the alerts only.
the script as follows
df -h | nawk '/backup/ {print $5 " " $6}' | while read line;
do
usep=$(echo $line | nawk '{printf "%d", $1}' )
partition=$(echo $line | nawk '{print $2}')
if (( $usep >= 90)); then
echo "$partition ($usep%)" | mailx -s "172.27.68.101" 0234343456#alert.abc.lk;
echo "$partition ($usep%)" | mailx -s "172.27.68.101" amalw#abc.lk;
echo "$partition ($usep%)" | mailx -s "172.27.68.101" amalw#abc.lk;
fi
done
Follwing image shows the output problem
How can i add multiple recipient to this script without opening such directories?

To paste a multi-line bash code into terminal, add parenthesis around the lines otherwise each line gets run as a separate command as soon as it gets pasted:
(df -h | nawk '/backup/ {print $5 " " $6}' | while read line; do
usep=$(echo "$line" | nawk '{printf "%d", $1}')
partition=$(echo $line | nawk '{print $2}')
if(("$usep" >= 90)) ; then echo "$partition ($usep%)" | mailx -s "172.27.68.101" 0234343456#alert.abc.lk;
echo "$partition ($usep%)" | mailx -s "172.27.68.101" amalw#abc.lk;
echo "$partition ($usep%)" | mailx -s "172.27.68.101" amalw#abc.lk;
fi
done)

Firstly command df -h|grep backup|sed 's/\%//g'|awk '$5 >= 90 {print $5"% "$6}' for having FS(Partition) more than 90% usage.
the rest of command to alert trought mail. Then :
df -h|grep backup|sed 's/\%//g'|awk '$5 >= 90 {print $5"% "$6}'|while read USAGE PARTITION
do
echo "$PARTITION ($USAGE)"|mailx -s "172.27.68.101" "0234343456#alert.abc.lk,amalw#abc.lk"
done

Related

script to check when a directory is last updated

I am trying to write a script that takes the last update date of a hadoop path subtracts it from current date and sends out an email if the difference is more than a certain number of days (variable).It needs to loop through a pipe delimited config file which has the number of days, table name,hadoop path and email
example of config file
30|db.big_leasing_center|/TST/DL/EDGE_BASE/GFDCP-52478/BIG_LEASING_CENTER/Data/|abcd#gmail.com
2|db.event|/TST/DL/EDGE_BASE/GFDCP-52478/GFDCP01P-ITG_FIN_DB/EVENTS/Data/|cab#gmail.com
below is what i tried
#!/bin/ksh
check_last_refresh() {
DAYS=$1
TABLE=$2
HDP_PATH=$3
EMAIL_DL=$4
last_refresh_date=$(hdfs dfs -ls $HDP_PATH | grep '^-' | awk '{print $6}' | sort -rh | head -1)
echo $last_refresh_date
diff=$(( ( $(date '+%s') - $(date '+%s' -d "$last_refresh_date") ) / 86400 ))
echo $diff
if [ "$diff" -gt "$DAYS" ]; then
echo "HI for $TABLE has an issue" | mail -s "HI for $TABLE has an issue, Please check" -b $EMAIL_DL
fi
return 0
}
cat /data/scratchSpace/bznhd9/CONFIG.txt | while read line; do
DAYS=$(echo $line|awk -F'|' '{print $1}')
TABLE=$(echo $line|awk -F'|' '{print $2}')
HDP_PATH=$(echo $line|awk -F'|' '{print $3}')
EMAIL_DL=$(echo $line|awk -F'|' '{print $4}')
echo $TABLE
r=$(check_last_refresh $DAYS $TABLE $HDP_PATH $EMAIL_DL)
echo $r
done
Please help

Increment variable when matched awk from tail

I'm monitoring from an actively written to file:
My current solution is:
ws_trans=0
sc_trans=0
tail -F /var/log/file.log | \
while read LINE
echo $LINE | grep -q -e "enterpriseID:"
if [ $? = 0 ]
then
((ws_trans++))
fi
echo $LINE | grep -q -e "sc_ID:"
if [ $? = 0 ]
then
((sc_trans++))
fi
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
However when attempting to do this with AWK I don't get the output - the $ws_trans and $sc_trans remains 0
ws_trans=0
sc_trans=0
tail -F /var/log/file.log | \
while read LINE
echo $LINE | awk '/enterpriseID:/ {++ws_trans} END {print | ws_trans}'
echo $LINE | awk '/sc_ID:/ {++sc_trans} END {print | sc_trans}'
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
Attempting to do this to reduce load. I understand that AWK doesn't deal with bash variables, and it can get quite confusing, but the only reference I found is a non tail application of AWK.
How can I assign the AWK Variable to the bash ws_trans and sc_trans? Is there a better solution? (There are other search terms being monitored.)
You need to pass the variables using the option -v, for example:
$ var=0
$ printf %d\\n {1..10} | awk -v awk_var=${var} '{++awk_var} {print awk_var}'
To set the variable "back" you could use declare, for example:
$ declare $(printf %d\\n {1..10} | awk -v awk_var=${var} '{++awk_var} END {print "var=" awk_var}')
$ echo $var
$ 10
Your script could be rewritten like this:
ws_trans=0
sc_trans=0
tail -F /var/log/system.log |
while read LINE
do
declare $(echo $LINE | awk -v ws=${ws_trans} '/enterpriseID:/ {++ws} END {print "ws_trans="ws}')
declare $(echo $LINE | awk -v sc=${sc_trans} '/sc_ID:/ {++sc} END {print "sc_trans="sc}')
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done

How can i get all the users and their authorize_key in linux using shell script?

I am using the following script to get the key count. But its is not working, is any other method exists? or how should i fix the error?
#!/bin/bash
for i in `cat /etc/passwd` ; do\
echo $i |\
awk -F: { print `grep -o "ssh-rsa" <<< (cat /home/"$1"/ .ssh/authorized_keys ) | wc -l` }
done
If you just want to display the number of keys, you can do this:
for homedir in $(awk -F':' '{print $6}' /etc/passwd); do
[[ -f "${homedir}/.ssh/authorized_keys" ]] && cat "${homedir}/.ssh/authorized_keys"
done
To display the counts for each file:
for homedir in $(awk -F':' '{print $6}' /etc/passwd); do
[[ -f "${homedir}/.ssh/authorized_keys" ]] && wc -l "${homedir}/.ssh/authorized_keys"
done
To display the counts by username:
while IFS='' read -r line || [[ -n "$line" ]]; do
USERNAME="$(echo "$line" | awk -F: '{print $1}')"
HOMEDIR="$(echo "$line" | awk -F: '{print $6}')"
if [[ -f "${HOMEDIR}/.ssh/authorized_keys" ]]; then
COUNT="$(grep -c 'ssh-' "${HOMEDIR}/.ssh/authorized_keys")"
else
COUNT=0
fi
echo "${USERNAME} has ${COUNT} keys."
done < "/etc/passwd"

trim ';' and split by ":"

I need to read a file like
#sys_platform:top_agent_id:channel
# 2 : 999 : 999
2:10086:10086;
2:999:999;
how to read sys_platform and top_agent_id, channel line by line
I write a shell , but not correctly
#!/bin/sh
sys_platform=""
top_agent_id=
channel=""
while read p; do
echo "line=$p"
echo $p | awk -F ':' '{print $1 $2 $3}' | read sys_platform top_agent_id channel
echo "sys_platform:${sys_platform}"
echo "top_agent_id:${top_agent_id}"
done < ./channellist.txt
result as :
line=#sys_platform:top_agent_id:channel
sys_platform:
top_agent_id:
line=# 2 : 999 : 999
sys_platform:
top_agent_id:
line=2:10086:10086;
sys_platform:
top_agent_id:
line=2:999:999;
sys_platform:
top_agent_id:
awk is your friend:
while read p; do
sys_platform=`echo $p | awk -F ':' '{print $1}'`
top_agent_id=`echo $p | awk -F ':' '{print $2}'`
channel=`echo $p | awk -F ':' '{print $3}' | tr -d ';'`
done < $filename
Nevertheless, you can do it directly with bash set builtin:
while read p; do
OFS=$IFS
IFS=':'
set -f
splitted=( $p )
set +f
sys_platform="${splitted[0]}"
top_agent_id="${splitted[1]}"
channel="${splitted[2]}"
IFS=$OFS
done < $filename
Less readable but should be more efficient.

Getting error while running script to find disk space

I am running below script:-
#!/bin/bash
threshold="20"
i=2
result=`df -kh |grep -v “Filesystem” | awk ‘{ print $5 }’ | sed ‘s/%//g’`
for percent in $result; do
if ((percent > threshold))
then
partition=`df -kh | head -$i | tail -1| awk ‘{print $1}’`
echo “$partition at $(hostname -f) is ${percent}% full”
fi
let i=$i+1
done
But I get the following error:
awk: ‘{
awk: ^ invalid char '▒' in expression
sed: -e expression #1, char 1: unknown command: `▒'
Please help me to resolve this.
What awk does not work? (your script does work fine on my Ubuntu)
This line:
result=`df -kh |grep -v "Filesystem" | awk '{ print $5 }' | sed 's/%//g'`
could be changed to:
result=$(df -kh | awk '!/Filesystem/ {print $5+0}')
Avoid using old and outdated backtics if parentheses works like this: var=$(code...)
This:
partition=`df -kh | head -$i | tail -1| awk '{print $1}'`
could be changed to:
partition=$(df -kh | awk -v line="$i" 'NR==line {print $1}')
This
let i=$i+1
could be change to:
((i++))
This would then give some like this:
#!/bin/bash
threshold="20"
i=2
result=$(df -kh | awk '!/Filesystem/ {print $5+0}')
for percent in $result; do
if ((percent > threshold))
then
partition=$(df -kh | awk -v line="$i" 'NR==line {print $1}')
echo "$partition at $(hostname -f) is ${percent}% full"
fi
((i++))
done
You're using ‘ for a single quote not '. Try re-encoding your file with an editor.
You got the answer to your syntax error, now re-write the whole script as just:
#!/bin/bash
df -kh |
awk -v t=20 -v h="$(hostname -f)" '(NR>1)&&($5+0>t){printf "%s at %s is %s full\n",$1,h,$5}'

Resources