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

asked on

need a help to fix one if else shell script issue

I need a specific function just to calculate swap size as per ram like:

if ram < 2048 then swap should be 1.5 times
if ram > 2048 and ram < 16536 then swap size should be equal to ram
if ram > 16536 then swap size should be 16536

please help
SOLUTION
Avatar of Member_2_3684445
Member_2_3684445
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 Sharath S
#!/bin/bash
ram=103
if [ $ram -gt 16536 ]; then 
	swap=16536
elif [ $ram -gt 2048 ]; then
	swap=$ram
else
	swap=$(echo "$ram*1.5" | bc)
fi
echo -e "swap value: $swap"

Open in new window

Avatar of The Rock

ASKER

Thanks Chris and Sharath - Both approach looks fine with me  only one thing left like if i take Sharath sample code below:

#!/bin/bash
ram=103
if [ $ram -gt 16536 ]; then
        swap=16536
elif [ [$ram -gt 2048] && [$ram -lt 16536] ]; then  ---- This line gives me issues - i know i am doing something stupid but unable to find - please help
        swap=$ram
else
        swap=$(echo "$ram*1.5" | bc)
fi
echo -e "swap value: $swap"
You don't need to check if the value is less than 16536. because it reaches 2nd else only if the first condition fails. i.e. if ram less than 16536.
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
Thanks Sharath for your quick help really appreciated.

Thanks Chris too for your quick and alternative for the solution. It helps me too.