So far you’ve seen how to write Linux bash scripts that do the job without user inputs. Today we will continue our series about Linux bash scripting. I recommend you to review the previous posts if you want to know what we are talking about: [Değerli Ziyaretci, linki görmeniz icin bu mesaja cevap yazmanız gerekiyorÜye olmak icin burayı tıklayınız] and [Değerli Ziyaretci, linki görmeniz icin bu mesaja cevap yazmanız gerekiyorÜye olmak icin burayı tıklayınız]. Today we will know how to retrieve input from the user and deal with that input, so our script becomes more interactive. To pass data to your shell script, you should use command line parameters.
$ ./myscript 3 5
Here we send 2 parameters (3 and 5) to the script. So how to read these parameters in our bash script?
Read Parameters

The shell gives you some read to use variables to process input parameters:


  • $0 is the script’s name.
  • $1 is the 1st parameter.
  • $2 is the 2nd parameter.


Until the 9th parameter which is $9.
Let’s see those variables in action:
#!/bin/bash
# Script name
echo $0
# 1st parameter
echo $1
# 2nd parameter
echo $2
# 3rd parameter
echo $3
Check the results of the following command:
./myscript 5 10 15

Here is another example of how we can use two parameters and calculate the sum of them.
#!/bin/bash
total=$(( $1 + $2 ))
echo First passed parameter is $1.
echo Second passed parameter is $2.
echo Total is $total.
$ ./myscript 5 10

The parameters are not restricted to numbers, they could be strings like this:
#!/bin/bash
echo Hello $1, how do you do
./myscript Adam
And the result is as expected.

What if our parameter contains a space and we want to pass it as one value? I guess you know the answer from the previous posts. The answer is to use quotations.
If your script requires over nine parameters, you should use braces like this ${10}.
Check Parameters

If you don’t pass parameters and your code expecting it, your script will exit with an error.
That’s why we should an [Değerli Ziyaretci, linki görmeniz icin bu mesaja cevap yazmanız gerekiyorÜye olmak icin burayı tıklayınız] to make sure that they exist.
#!/bin/bash
if [ -n "$1" ]
# If first parameter passed then print Hi
then
echo Hi $1.

else

echo "No parameters found. "
fi

Counting Parameters

To get how many parameters passed, you can use this variable ($#).
#!/bin/bash
echo There were $# parameters passed.
./myscript 1 2 3 4 5

How awesome is Linux bash scripting? This variable also provides a geeky way to get the last parameter. Look at this trick:
#!/bin/bash
echo Last passed parameter is ${!#}

Grabbing All Parameters

The $* variable holds all the parameters as one value.
The $@ variable holds all the parameters as separate values, so you can iterate over them.
This code shows how to use them:
#!/bin/bash
echo "The first variable \$* method: $*"
echo "-----------"
echo "The second variable \$@ method: $@"

The same result, but if you want to know the difference look at the following example:
#!/bin/bash
total=1
for var in "$*"
do
echo "\$* =======> #$total = $var"
total=$(( $total + 1 ))
done
total=1
for var in "$@"
do
echo "\$@ =======> #$total = $var"
total=$(( $total + 1 ))
done
Check the result to see the difference:

The result is pretty clear. You can use any one of them according to your needs.
Shift command

The shift command moves every parameter variable to the left:
variable $3 ==> variable $2.
variable $2 ==> variable $1.
variable $1 ==> dropped.
variable $0 ==> (the script name) as it is.
You can use the shift command to iterate over parameters like this:
#!/bin/bash
total=1
while [ -n "$1" ]
# while loop starts

do

echo "#$total = $1"
total=$(( $total + 1 ))
shift
done
Here, we have a while loop checking $1 length. If $1 becomes zero, the loop stops. And the shift command is shifting all passed parameters to the left.

Careful when using shift command, the shifted parameter is gone and cannot be recovered.
Bash Scripting Options

Options are single letters with a dash before it.
#!/bin/bash
echo
while [ -n "$1" ]
# while loop starts
do
case "$1" in
# Message for -a option
-a) echo "-a option passed" ;;
# Message for -b option
-b) echo "-b option passed" ;;
# Message for -c option
-c) echo "-c option passed" ;;
# In case you typed a different option other than a,b,c
*) echo "Option $1 not recognized" ;;
esac
shift
done
$ ./myscript -op1 -op2 -op3 -op4

The code check for one of the correct options. If you typed one of them, the suitable commands will run.
Separate Options From Parameters

Sometimes you need to use options and parameters on the same script. You have to separate them. By doing this, you are telling the bash where are the parameters and where are the options.
The double dash (–) is used to end the options list. After the shell sees the double dash, the remaining inputs are treated as parameters and not as options.
#!/bin/bash
while [ -n "$1" ]
# while loop starts
do
case "$1" in
# Message for -a option
-a) echo "-a option passed" ;;
# Message for -b option
-b) echo "-b option passed";;
# Message for -c option
-c) echo "-c option passed" ;;
# The double dash which separates options from parameters
--) shift
# Exit the loop using break command
break ;;
*) echo "Option $1 not recognized";;
esac
shift
done
total=1
for param in $@
do
echo "#$total: $param"
total=$(( $total + 1 ))
done
$ ./myscript -a -b -c -- 5 10 15

As you can see from the result, all the parameters after the double dash treated as parameters and not options.
Process Options With Values

When you dig deep onto Linux bash scripting, sometimes you need options with additional parameter values like this:
./myscript -a value1 -b -c value2
There should be a way to identify any additional parameter for the options and be able to process it.
#!/bin/bash
while [ -n "$1" ]
# while loop starts
do
case "$1" in
-a) echo "-a option passed";;
-b) param="$2"
echo "-b option passed, with value $param"
shift ;;
-c) echo "-c option passed";;
# The double dash makes them parameters
--) shift
break ;;
*) echo "Option $1 not recognized";;
esac
shift
done
total=1
for param in "$@"
do
echo "#$total: $param"
total=$(( $total + 1 ))
done
And if we run it with these options:
$ ./myscript -a -b test1 -d

From the results, you can see that we get the parameter for the -b option using the $2 variable.
Standard Options

When you start your Linux bash scripting, you are free to choose which letter is suitable for your option.
However, there are some letters that are commonly used in Linux programs.
And here is the list of the common options:
-a List all items.
-c Get count of items.
-d Output directory.
-e Expands items.
-f To specify a file.
-h To show help page.
-i To ignore character case.
-l To list a text.
-n To say no for a question.
-o To send output to a file or so.
-q Keep silent don’t ask the user.
-r To process something recursively.
-s Stealth mode.
-v Verbose mode.
-x Specify executable.
-y To say yes without prompting the user.
If you work with Linux, many of these options may look familiar to you.
it’s good to follow standards.
Getting User Input

Sometimes you need data from the user while the bash scripting is running.
The bash shell uses the read command for this purpose.
The read command reads input from standard input (the keyboard) or from a file descriptor and stores it in a variable:
#!/bin/bash
echo -n "What's your name: "
read name
echo "Hi $name,"
The -n option is used to disable the newline, so you can type your text in the same line.

You can specify multiple inputs like this:
#!/bin/bash
read -p "What's your name: " first last
echo "Your data for $last, $first…"

If you don’t specify variables for the read command, all incoming inputs are saved in REPLY variable.
#!/bin/bash
read -p "What's your name: "
echo Hello $REPLY,.

You can use the -t option to specify a timeout for input in seconds.
#!/bin/bash
if read -t 5 -p "What's your name: " name
then
echo "Hi $name, how are you?"
else
echo "You took much time!"
fi
If you do not enter data for five seconds the script will execute the else clause and print a sorry message.

Reading Password

In Linux bash scripting, sometimes you don’t want the user input to be displayed on the screen, like entering a password.
The -s option hides the data entered in the read command.
The data is actually printed on the screen, but the read command makes the text color as the background color of the shell.
#!/bin/bash
read -s -p "Enter password: " mypass
echo "Your password is $mypass? "

Read Files

The read command can read files one line on each call.
Now, if you want to get all file data, you can get the content using the cat command, then send it to the read command using while loop like this:
#!/bin/bash
count=1
# Get file content then pass to read command by iterating over lines using while command
cat myfile | while read line
do
echo "#$count: $line"
count=$(( $count + 1 ))
done
echo "Finished"

We just pass the file content to the while loop and iterate over every line and print the line number and the content, and each time you increase the count by one.
I hope you find this post interesting. Keep coming back.