Link to home
Start Free TrialLog in
Avatar of The Rock
The RockFlag for India

asked on

need help in fixing below script

Hi All

I want to incraese swap as per ram size so created one script.

but it gives some error - please help me to fix it.

#!/bin/sh

oram=/usr/bin/free -g | grep Mem | awk '{print $2}'
swap.sh: line 3: -g: command not found
oswap=/usr/bin/free -g | grep Swap | awk '{print $2}'
swap.sh: line 4: -g: command not found

if [ "$oram" < 2 ]
then
swapsize=$oram*1.5
elif [ "$oram" >= 2 && "$oram" <= 16  ]
then
swapsize=$oram
if [ "$oram" > 16 ]
then
swapsize=16
fi

# does the swap file already exist?
grep -q "swapfile" /etc/fstab

# if not then create it
if [ $? -ne 0 ]; then
        echo 'swapfile not found. Adding swapfile.'
        fallocate -l ${swapsize}G /swapfile
        chmod 600 /swapfile
        mkswap /swapfile
        swapon /swapfile
        echo '/swapfile none swap defaults 0 0' >> /etc/fstab
else
        echo 'swapfile found. No changes made.'
fi

# output results to terminal
cat /proc/swaps
cat /proc/meminfo | grep Swap

Open in new window

Avatar of Member_2_406981
Member_2_406981

change

oram=/usr/bin/free -g | grep Mem | awk '{print $2}'

into

oram=$(/usr/bin/free -g | grep Mem | awk '{print $2}') and it should work

Same mod with the oswap line.
SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of The Rock

ASKER

Thanks but now if statement not working

oram=$(/usr/bin/free -g | grep Mem | awk '{print $2}')
oswap=$(/usr/bin/free -g | grep Swap | awk '{print $2}')
echo "Current Ram = $oram "
echo "Current Swap = $oswap "
if [ "$oram" < 2 ]
then
swapsize=($oram*1.5)
echo "$swapsize"
elif [ "$oram" >= 2 && "$oram" <= 16  ]
then
swapsize=$oram
echo "$swapsize"
elif [ "$oram" > 16 ]
then
swapsize=16
echo "$swapsize"
fi
see comment from Gerwin above you need to exchange the comparision operators accordingly.
yeah it works thanks one last thing  any idea on swapsize=($oram*1.5)  its not working well :(
use bc for that "swapsize=($oram*1.5)" - like:

swapsize=$(echo "${oram}*1.5" | bc)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@gerwin thank for bc, didnt know that :)
Yup great but uts giving me in decimal i need to round off it
Current Ram = 2006
Current Swap = 1023
New Swap Size = 3009.0
round down using bc again:

swapsize=$(echo "${oram}*1.5/1" | bc)
>> Current Ram = 2006
You have 2006 G of ram? You specified "free -g" above which should give you "2" instead of "2006".
Thank you so much Andreas and Grewin for quick help. :)
You're welcome ;)
I don't care about points but you should have selected the first comment as an (assisted) solution as well because that got you the mem/swap values into variables.