Simple and effective Linux shell script example (continued one)

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.

Simple and effective Linux shell script example (continued one)

10. Use the AND operator

The AND operator allows our program to check whether multiple conditions are met at once. All parts separated by the AND operator must be true. Otherwise, statements containing AND will return false. Check out the example bash script below to better understand how AND works.

#!/bin/bash

echo -n "Enter number:"
read num

if [[($ num -lt 10) && ($ num% 2 -eq 0)]]; then
echo "You just entered an even number: $ num"
else
echo "You just entered an odd number: $ num"
fi

AND in shell script

11. Use the "OR" operator

The OR operator is another crucial structure that enables us to implement complex and powerful programming logic in scripts. Contrary to AND, a statement composed of OR operators returns true when any of its operands is true. It returns false only if each operand separated by OR is false.

#!/bin/bash

echo -n "Enter any number:"
read n

if [[($ n -eq 25 || $ n -eq 50)]]
then
echo "You won"
else
echo "You lost"
fi

How the OR operator works in a Linux shell script

This simple example demonstrates how the OR operator works in a Linux shell script. Only when the user enters the number 25 or 50 does it declare the user as the winner. The symbol represents the "or" operator.

12. Use Elif

The elif statement stands for else if, and provides a convenient method for implementing chain logic. Understand the working principle of elif by evaluating the following examples.

#!/bin/bash

echo -n "Enter a number:"
read num

if [[$ num -gt 20]]
then
echo "The number is greater than 20."
elif [[$ num -eq 20]]
then
echo "The number is equal to 20."
else
echo "The number is less than 20."
fi

The elif statement stands for else if,

The above procedure is self-explanatory, so we will not analyze it line by line. You change certain parts of the script (such as variable names and values) to check how they work together.

13. Multi-branch selection structure

The multi-branch selection structure is another powerful feature provided by Linux bash scripts. It can be used where nested conditions are needed, but you don't want to use complicated if-else-elif chains. Then see the next example.

#!/bin/bash

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

case $ num in
100)
echo "100 !!" ;;
200)
echo "200 !!" ;;
*)
echo "neither 100 nor 200" ;;
esac

Simple and effective Linux shell script example (continued one)

The condition is written between the case and esac keywords. *) Used to match all inputs except 100 and 200.

14. Command line parameters

In many cases, it may be beneficial to obtain the parameters directly from the command shell. The following example shows how to do this in bash.

#! / bin / bash
echo "Total parameters: $ #"
echo "1st parameter = $ 1"
echo "2nd parameter = $ 2"

Therefore, $ 1 is used to access the first parameter, $ 2 is used to access the second parameter, and so on. $ # Is used to get the total number of parameters.

Simple and effective Linux shell script example (continued one)

15. Get parameters with names

The following example shows how to get the command line parameters with names.

#!/bin/bash

for arg in "$@"
do
index=$(echo $arg | cut -f1 -d=)
val=$(echo $arg | cut -f2 -d=)
case $index in
A) a=$val;;
B) b=$val;;
*)
esac
done
((result=a+b))
echo "A+B=$result"

It should return A + B = 270. The parameters here are stored in '$ @' and the script uses the Linux cut command to get them.

Simple and effective Linux shell script example (continued one)

16. Connection string

String processing is essential for various modern bash scripts. Thankfully, it is more comfortable to use in bash and allows a more precise and concise way to achieve this. See the following example for a quick look at bash string connection.

#!/bin/bash

Simple and effective Linux shell script example (continued one)

17, slice string

Contrary to many programming languages, bash does not provide any built-in functions for slicing string parts. The following example shows how to use parameter expansion to accomplish this.

This script should output "Learn Bash Commands". Parameter expansion takes the form of $ {VAR_NAME: S: L}. Here, S represents the starting position and L represents the length.

Simple and effective Linux shell script example (continued one)

18. Use cut to extract substrings

You can use the Linux cut command inside the script to "cut" a part of a string, that is, a substring. The next example shows how to do this.

subStr=$(echo $Str| cut -d ' ' -f 1-4)
echo $subStr

Simple and effective Linux shell script example (continued one)

19. Add two values

It is very easy to perform arithmetic operations in Linux Shell scripts. The following example shows how to receive two numbers as input from the user and add them.

#! / bin / bash
echo -n "Enter the first number:"
read a
echo -n "Enter the second number:"
read b
((sum = a + b))
echo "addition result = $ sum "

Simple and effective Linux shell script example (continued one)

As you can see, adding numbers in bash is fairly simple.

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

Guess you like

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