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.


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

if [ "$oram" < 2 ]
elif [ "$oram" >= 2 && "$oram" <= 16  ]
if [ "$oram" > 16 ]

# 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
        echo 'swapfile found. No changes made.'

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

Open in new window

The RockAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andreasSystem AdminCommented:

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


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

Same mod with the oswap line.
Gerwin Jansen, EE MVETopic Advisor Commented:
You are comparing numbers, instead of using < and > use -lt, -le, -gt etc. instead.
The RockAuthor Commented:
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 ]
echo "$swapsize"
elif [ "$oram" >= 2 && "$oram" <= 16  ]
echo "$swapsize"
elif [ "$oram" > 16 ]
echo "$swapsize"
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

andreasSystem AdminCommented:
see comment from Gerwin above you need to exchange the comparision operators accordingly.
The RockAuthor Commented:
yeah it works thanks one last thing  any idea on swapsize=($oram*1.5)  its not working well :(
Gerwin Jansen, EE MVETopic Advisor Commented:
use bc for that "swapsize=($oram*1.5)" - like:

swapsize=$(echo "${oram}*1.5" | bc)
andreasSystem AdminCommented:
use let. Shell scripts do not support decimals neither in results nor in operands.

su you could try let swapsize=$oram*3/2.

Will give 4 for an oram of 3
if you need to be sure its at least 1.5 than add 1.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andreasSystem AdminCommented:
@gerwin thank for bc, didnt know that :)
The RockAuthor Commented:
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, EE MVETopic Advisor Commented:
round down using bc again:

swapsize=$(echo "${oram}*1.5/1" | bc)
Gerwin Jansen, EE MVETopic Advisor Commented:
>> Current Ram = 2006
You have 2006 G of ram? You specified "free -g" above which should give you "2" instead of "2006".
The RockAuthor Commented:
Thank you so much Andreas and Grewin for quick help. :)
Gerwin Jansen, EE MVETopic Advisor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.