Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 4091  / 3 Years ago, fri, july 9, 2021, 6:11:29

I'm writing my first bash script and I've hit a snag. Here's an example where I ask for a name, ask for an age, and display a message that changes depending on whether or not the age is <= 35:



#!/bin/bash
ageLimit = 34
echo "What is your name?"
read userName
echo "How old are you?"
read userAge
if (( "$userAge" -le "$ageLimit" )); then
echo "Hey, $userName. At $userAge years old, you are young."
else
echo "Gosh, $userName. At $userAge years old, you're pretty old."
fi


No matter what number I answer for age, I am told I am young. This is running on Raspbian -- in case that helps. Thanks!



Jeremy


More From » bash

 Answers
4

Whitespaces make a lot of difference in whell script, you need to take care of them. There is no space between the variable name and the = sign and its value.



Rewrite your shell script as follows:



#!/bin/bash
ageLimit=34
echo "What is your name?"
read userName
echo "How old are you?"
read userAge
if [[ "$userAge" -le "$ageLimit" ]]; then
echo "Hey, $userName. At $userAge years old, you are young."
else
echo "Gosh, $userName. At $userAge years old, you're pretty old."
fi


Refer to a good tutorial on shell scripting while you make your hands dirty. One good tutorial googling returns is this:





You may also want to know this:




[#27549] Friday, July 9, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tubequ

Total Points: 11
Total Questions: 113
Total Answers: 115

Location: Equatorial Guinea
Member since Thu, Oct 7, 2021
3 Years ago
;