We help IT Professionals succeed at work.

New podcast episode! Our very own Community Manager, Rob Jurd, gives his insight on the value of an online community. Listen Now!

x

need a help to fix one if else shell script issue

The Rock
The Rock asked
on
166 Views
Last Modified: 2017-05-18
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
Comment
Watch Question

Chris GralikeSpecialist
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Sharath SData Engineer
CERTIFIED EXPERT

Commented:
#!/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

Author

Commented:
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"
Sharath SData Engineer
CERTIFIED EXPERT

Commented:
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.
Data Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks Sharath for your quick help really appreciated.

Thanks Chris too for your quick and alternative for the solution. It helps me too.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.