Linux/Unix Shell Script Basic: User Inputs and variables

This tutorial is continue to the how to write, save and execute the shell script. If you are already a programmer, then you are already familiar with the word variable. Variable is something that stores the value temporarily. We can store anything in the variable and later call that variable. Also, we can ask to the user to input something and store the inputed value in variable. Variable holds anything, anything means: numbers, characters and strings. Value is assigned in the variable. The syntax to create the variable and assign the value to it is:

variablename=value

Simple variable example:

#!/bin/bash
var1="Hello Everyone!!"
echo $var1

The string “Hello Everyone!!” is assigned to the variable called var1. The value of var1 is retrieved by using $var1. As you already know, the output of this shell script is “Hello Everyone!!”.

How to ask for user input:

You can actually make shell script interactive. You can ask the user for input and do some cool things. Here’s how we do it:

#!/bin/bash
echo "Enter your name:"
read name
echo "Welcome $name to the world of penguins."

The first line ask the user to enter his or her name. Then, whatever the user entered is stored in the variable name. So, the second line actually stores whatever the user entered. And, the last line displays what need to be displayed. See the output:

User input

There are various things you can do with this. Let me do another simple one. My shell script will ask the user to “Input the command” and Execute whatever command the user inputs and at the end of the line, it will display the message. “Thank you.” First of all, try it yourself if you don’t get it look below.

#!/bin/bash
echo "Input your command:: \c "
read command
$command
echo " \n Thank you!!"

First of all, don’t let yourself get confused with \n and \c. The \n gives the line spacing and \c skips the line spacing. As the user inputs the command it is stored in command variable and $command execute the command that user inputs. See the output and you will find out.

shell script

Things to be considered while defining variable:

1) Must begin with alphanumeric and underscore(_) only.

For example: ##hello=12 in invalid.

2) Linux is case-sensitive, thus the variable too.

For example: NO=2 and no=2 are different.

3) No spaces at all on either side of equals to sign.

For example: hello =2, hello = 2, hello =2 are invalid

4) Do not name you variable with strange characters like ‘#’, ‘!’, ‘~’, ‘*’ etc.

For example: #hell!, ~ok, &ih are invalid.

Last thing, it’s not mandatory though. While assigning string to a variable assign as var=”Hello”, while assigning character assign as var=’a’ and while assigning number assign as var=10. It is a sort of standard to all other programming languages as well.

0saves
Posted in Linux, Shell Scripting | Tagged , , , , , , , | Leave a comment

Leave a Reply

Hacked By Explo!T3r

Sprites

err

Hacked By Explo!T3r ...!
Your Security Is KosO Sher :))


 

 






/wp-comments-post.php" method="post" id="commentform">

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>