Avatar of The Rock
The Rock
Flag 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

LinuxScripting LanguagesShell Scripting

Avatar of undefined
Last Comment
Gerwin Jansen

8/22/2022 - Mon
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
Gerwin Jansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
Member_2_406981

see comment from Gerwin above you need to exchange the comparision operators accordingly.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
The Rock

ASKER
yeah it works thanks one last thing  any idea on swapsize=($oram*1.5)  its not working well :(
Gerwin Jansen

use bc for that "swapsize=($oram*1.5)" - like:

swapsize=$(echo "${oram}*1.5" | bc)
ASKER CERTIFIED SOLUTION
Member_2_406981

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Member_2_406981

@gerwin thank for bc, didnt know that :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
The Rock

ASKER
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
Gerwin Jansen

round down using bc again:

swapsize=$(echo "${oram}*1.5/1" | bc)
Gerwin Jansen

>> Current Ram = 2006
You have 2006 G of ram? You specified "free -g" above which should give you "2" instead of "2006".
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
The Rock

ASKER
Thank you so much Andreas and Grewin for quick help. :)
Gerwin Jansen

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.