Simple and effective Linux shell script example

Historically, the shell has been the native command line interpreter for Unix-like systems. Over the years, it has proved to be one of the main features of Unix, and it has evolved into a whole new topic. Linux provides various powerful shells with powerful functions, including Bash, Zsh, Tcsh and Ksh. One of the most amazing features of these Shell programs is their programmability. It is very easy to create simple and effective Linux Shell scripts to handle daily tasks. In addition, a little understanding of this topic will make you an advanced user of Linux immediately. Please join us to introduce the Unix Shell script in detail.

Linux Shell script example

Most shell scripts done on Linux involve bash shell. Advanced users with designated options often use other shells, such as Zsh and Ksh. Due to the widespread use and huge availability of the examples, we will stick to Linux bash scripts for most of the examples. Our editor also attempts to outline some shell script examples that deal with shells other than bash. You will find quite familiar between different shell scripts.

Linux Bash script

Bash, also known as Bourne Again Shell, is the default command line interpreter in most Linux distributions today. It is an upgrade to the early Bourne Shell. Learning bash shell scripts will enable you to learn about other shell scripts faster. Therefore, please try these simple examples yourself to get first-hand experience.

1、Hello World

Save and exit the file. You need to use the following command to make the file executable.

Simple and effective Linux shell script example

It will print out the string passed to the echo inside the script.

2. Use echo to print

The echo command is used to print information in bash format. It is similar to the C function 'printf' and provides many common options, including escape sequences and redirection.

Run the script and see what it does. The -e option is used to tell echo that the string passed to it contains special characters and needs extended functions.

Simple and effective Linux shell script example

3. Use notes

Comments are useful for documentation and are requirements for a high-quality code base. It is a common practice to put comments in code that handles critical logic. To comment out a line, just use the # (hash) character in front of it. Check the following bash script example.

#!/bin/bash

#
Add two values ((sum = 17 + 19))


#Print result echo $ sum

The script will output the number 36. Please use # to check how to use comments before certain lines. But the first line is an exception. It is called a shebang and lets the system know which interpreter to use when running this script.

Simple and effective Linux shell script example

4. Multi-line comments

#! / bin / bash
: '
This script calculates
the square of 5.
'
((area = 5 * 5))
echo $ area

Note how the multi-line comments are placed in: 'and' characters.

Simple and effective Linux shell script example

5. While loop

The while loop structure is used to run certain instructions multiple times. Please review the following script named while.sh to better understand this concept.

#! / bin / bash
i = 0

while [ $i -le 2 ]
do
echo Number: $i
((i++))
done

Simple and effective Linux shell script example

Therefore, the while loop takes the following form.

while [condition]
do
commands 1
commands n
done

Square brackets are required.

6. For loop

The for loop is another widely used bash shell construct that allows users to efficiently traverse the code. A simple example is demonstrated below.

#!/bin/bash

for (( a=1; a<=5; a++ ))
do
echo -n "$a "
done

printf "\n"

Simple and effective Linux shell script example

7. Receive user input

Obtaining user input is essential for user interaction in scripts. The following Shell script example will demonstrate how to receive user input in a Shell program.

#!/bin/bash

echo -n "Enter content:"
read something

echo "You entered: $ something"

Simple and effective Linux shell script example

Therefore, the read structure is followed by a variable name, which is used to obtain user input. The input is stored in this variable, which can be accessed using the $ symbol.

8. If statement

If the statements are the most common conditional constructs in Unix shell scripts, they take the following form.

if CONDITION
then
STATEMENTS
fi

Only execute the statement if the condition is true. The fi keyword is used to mark the end of an if statement. Here is a simple example.

#!/bin/bash

echo -n "Please enter a number:"
read num

if [[$ num -gt 17]]
then
echo "Number is greater than 17."
fi

Simple and effective Linux shell script example

Only when the number provided by the input is greater than 17, the above program will display the output. The -gt means greater than; similarly -lt is less than; -le is less than or equal; and -Ge is greater than or equal to. [[]]is compulsory.

9. Use If Else for more control

The combination of else construction and if can better control the logic of the script. A simple example is shown below.

#!/bin/bash

read n
if [$ n -lt 10];
then
echo "This is a number"
else
echo "This is a two-digit number"
fi

Simple and effective Linux shell script example

The else part needs to be placed between the operation part of the if and fi.

If you like it, please continue to follow the Linux Commune.

Guess you like

Origin www.linuxidc.com/Linux/2020-04/162937.htm