Basic Linux Shell Scripting Concepts

Input Parameters

Shell scripts can also accept input parameters. This allows you to build a multi-function script that can perform different actions each time it is executed. Input parameters are easy to use. They are automatically store in variables that represent their ordinal position when specified. For example, the first input parameter can be referenced as $1, the second as $2 and so on. Consider the following example.

#!/bin/bash
echo You told me to say $1

Save this script as [talk] and then run the following

[./talk Hello]

Looping

Looping allows the repeated execution of a section of code. This can be used to parse though repeated values, or to add multiple entries to a file. The following is the syntax of a loop.

while test; do
repeated code
loop

The keyword [break] can be used anywhere in the code to exit the loop at any point. This is typically found inside a nested if. Next is an example of a loop that counts to 10, printing the output each time.

#!/bin/bash
ctr=1
while ctr <=10; do echo value is $ctr $ctr=$ctr+1 loop