Web Hosting Talk







View Full Version : Basic bash scripting


Rockbottom
03-25-2006, 09:34 AM
Hello,

I am just starting to learn some basic bash scripting and was wondering if someone could help me with the following basic script. How would I go about making it so that when the script is run it asks the user a question and when they answer it, it echos the output.

For example:

User runs the script
Script asks: How old are you? Users response: 23
Script echos: You are 23 years old.

I obviously won't actually use the above script as it is pointless, I just want to learn how something like that is done for a different script I am working on. :)


Cheers,

Steve.

azizny
03-25-2006, 04:48 PM
Is this HW.

Anyhow I will redirect to what you need to know and its up to you to research.

You will either use ECHO or PRINT to echo the statement. To get the first argument you use "$1", look more into "retrieving variables in bash".

Waiting for the input from the user, I'll also leave that to you to find.

Peace,

folsom
03-26-2006, 12:15 AM
echo How old are you?
read age
echo you are $age years old

Rockbottom
03-26-2006, 04:10 AM
Is this HW.
No, it's not homework, I'm just interested in linux and want to learn some basic bash scripting.

echo How old are you?
read age
echo you are $age years old
Thanks, I'll try that later - it looks easy enough. :)


Cheers,

Steve.

Bilco105
03-26-2006, 08:31 AM
Infact, you'll need echo -n for user input, makes it a lot tider.

#!/bin/bash
echo -n "Please enter your age: "
read age
if [ ! $age -gt "16" ]; then
echo "You're $age, thats too young for linux
exit 1
fi:D

innova
03-28-2006, 02:34 AM
Laff!~

That was hilarious. Nice unnecessary use of the negation operator as well :)

Nice choice of error code as well. True that.