I need to write a script to show the Total Swap Size, Used Swap Size and % used.
Below are the commands, I'm using.
pgsize=`swapon | tail -1 | awk '{print $3}'`
pgused=`swapon | tail -1 | awk '{print $4}'`
pgpercent=$(($pgused * 100/ $pgsize))
The output of the commands are
pgsize = 16G
pgused = 22M
pgpercent - I'm getting the below error in this line
22M: value too great for base (error token is "22M")
Here, how do I need to convert the 22M into 22 and 16G into 16*1024 and divide it. For eg. (22*100)/(16*1024)
To print the percentage used of the last swap area listed by swapon:
swapon --bytes --show=USED,SIZE | awk 'END{print 100*$1/$2}'
To save it in a variable:
ppgpercent=$(/sbin/swapon --bytes --show=USED,SIZE | awk 'END{print 100*$1/$2}')
swapon has a --bytes parameter
hence:
swapon --raw --bytes | tail -1 | awk '{print $4 "/" $3}' | bc
Related
I have the below output, i want to get rid of /data & / as well in the output.
cat /etc/fstab | egrep -v '^#' | awk '{print $2}'| grep -i "^/" | egrep -v '/etc/fstab|proc|sys|shm|pts|/apps|/boot|home|/opt|/var|/var|/crash|/tmp|"' > /tmp/mounts.txt
Output:
/
/data
/data/logs/mount1
/data/logs/mount2
I just need /data/logs/mount1 & /data/logs/mount2 to be displayed. Any suggestions ?
Thanks,
KG
awk can do all that your pipeline does:
awk '
# skip comments and empty lines
$1 ~ /^#/ || NF == 0 {next}
# skip mountpoints not beginning with a slash
$2 ~/^[^/]/ {next}
# skip / and /data
$2 == "/" || $2 == "/data" {next}
{print $2}
' /etc/fstab
or, if you're a fan of linenoise:
awk 'NF&&$1!~/^#/&&$2!~/^[^/]/&&$2!="/"&&$2!="/data" {print $2}' /etc/fstab
Add " | grep /data/" that will not match / and /data.
For your example that is:
cat /etc/fstab | egrep -v '^#' | awk '{print $2}'| grep -i "^/" | egrep -v '/etc/fstab|proc|sys|shm|pts|/apps|/boot|home|/opt|/var|/var|/crash|/tmp|"' | grep /data/ > /tmp/mounts.txt
i am able to send mail of simple test in Linux
echo "body" | mail -s "test" xxx#yahoo.com
but below code is not working
#!/bin/bash
VAR1=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
VAR2=$(df /dev/sda1 | grep /dev/sda1 | awk '{ print $5}' | sed 's/%//g')
VAR3=$(df /dev/mapper/centos-var | grep /dev/mapper/centos-var | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=50
TODAY=$(date)
if [ "$VAR1" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' rupendra#3ess.in << EOF
Date of $TODAY
Your system partition remaining free space is critically low.
/ partition used is $VAR1%
/ usr partition used is $VAR2%
/ var partition used is $VAR3%
What error did you receive?
The following one worked for me,
#!/bin/bash
set -x
VAR1=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
VAR2=$(df /dev/sda1 | grep /dev/sda1 | awk '{ print $5}' | sed 's/%//g')
VAR3=$(df /dev/mapper/centos-var | grep /dev/mapper/centos-var | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=50
TODAY=$(date)
echo "VAR1=$VAR1, THRESHOLD=$THRESHOLD"
if [ "$VAR1" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' abcd#xyz.com << EOF
Date of $TODAY
Your system partition remaining free space is critically low.
/ partition used is $VAR1%
/ usr partition used is $VAR2%
/ var partition used is $VAR3%
EOF
fi
I am using following commands in my script:
max_length=`awk '{print length}' $File_Path_Name/$filnm | sort -nr | head -1`;
min_length=`awk '{print length}' $File_Path_Name/$filnm | sort -nr | tail -1`;
where the filenm variable contains the name of the file and File_Path_Name contains the directory path.
While executing this from script I am getting the error
sort: write failed: standard output: Broken pipe
Any suggestions what I am doing wrong?
you don't need to scan the file twice for getting max/min
try
$ read max min < <(awk '{print length}' file | sort -nr | sed -n '1p;$p' | paste -s)
or you can avoid sorting as well by calculating max/min within awk
$ awk '{len=length}
NR==1 {max=min=len}
max<len{max=len}
min>len{min=len}
END {print max, min}' file
I have the following script:
#!/bin/bash
TotalMem=$(top -n 1 | grep Mem | awk 'NR==1{print $4}') #integer
UsadoMem=$(top -n 1 | grep Mem | awk 'NR==1{print $8}') #integer
PorcUsado='scale=2;UsadoMem/TotalMem'|bc -l
echo $PorcUsado
The variable PorcUsado returns empty. I search for the use of bc, but something is wrong...
You're assigning PorcUsado to scale=2;UsadoMem/TotalMem and then piping the output of that assignment (nothing) into bc. You probably want the pipe inside a command substitution, e.g. (using a here string instead of a pipe):
PorcUsado=$(bc -l <<<'scale=2;UsadoMem/TotalMem')
But you'll also need to evaluate those shell variables - bc can't do it for you:
PorcUsado=$(bc -l <<<"scale=2;$UsadoMem/$TotalMem")
Notice the use of " instead of ' and the $ prefix to allow Bash to evaluate the variables.
Also, if this is the whole script, you can just skip the PorcUsado variable at all and let bc write directly to stdout.
#!/bin/bash
TotalMem=$(top -n 1 | grep Mem | awk 'NR==1{print $4}') #integer
UsadoMem=$(top -n 1 | grep Mem | awk 'NR==1{print $8}') #integer
bc -l <<<"scale=2;$UsadoMem/$TotalMem"
Why pipe top output at all? Seems too costly.
$ read used buffers < <(
awk -F':? +' '
{a[$1]=$2}
END {printf "%d %d", a["MemTotal"]-a["MemFree"], a["Buffers"]}
' /proc/meminfo
)
Of course, it can easily be a one-liner if you value brevity over readability.
I think the pipe is the problem try something like this:
PorcUsado=$(echo "scale=2;$UsadoMem/$TotalMem" | bc -l)
i haven't tested it yet but you have to echo the string and pipe the result from echo to bc.
EDIT: Correcting the variable names
You don't need grep or bc, since awk can search and do math all by itself:
top -n 1 -l 1 | awk '/Mem/ {printf "%0.2f\n",$8/$4;exit}'
#!/bin/bash
ac = "(free | grep Mem | awk '{print $4/$2 * 100.0}')"
echo "$ac"
I wanted to get the remaining(free) percentage of RAM usage as output storing in variable for further processing.
Instead m getting $ac output as
total 0.00
Please help
#!/bin/bash
ac="$(free | grep Mem | awk '{print $4/$2 * 100.0}')"
echo "$ac"
works fine for me.
(BTW, awk can do grep's work too:
free | awk '$1=="Mem:" { print 100*$4/$2; }'
)