Variables and Flow Control in Linux Shell Scripts

Variables and Flow Control in Linux Shell Scripts

Linux Shell scripts are a handy automation tool that can help us accomplish various complex tasks. In this article, we'll take a detailed look at variables and flow control statements in shell scripts, and how to use them to write efficient, readable scripts.

https://image.baidu.com/search/detail?ct=503316480&z=undefined&tn=baiduimagedetail&ipn=d&word=Linux%20Shell&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=undefined&hd=undefined&latest=undefined&copyright=undefined&cs=2812537524,3237249650&os=3061806688,3426950554&simid=4240442422,819455717&pn=0&rn=1&di=7214885350303334401&ln=767&fr=&fmq=1688355119667_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&is=0,0&istype=0&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=1e&objurl=https%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2Fcaf7feb16c03ee43c95e205585d7be40298d536b.jpg&rpstart=0&rpnum=0&adpicid=0&nojc=undefined&dyTabStr=MTEsMCwxLDYsNCw1LDMsNyw4LDIsOQ%3D%3D

variable

In shell scripts, variables are containers for storing and referencing data. Variable names consist of letters, numbers, and underscores, but cannot begin with a number.

define variable

In shell scripts, variables can be defined using the equal sign (=). There cannot be a space between the variable name and the equal sign.

name="John Doe"
age=30

reference variable

To reference a variable, precede the variable name with a dollar sign ($). When quoting variables, you can enclose the variable name in double quotes (") or single quotes ('). Variables inside double quotes are expanded, while those inside single quotes are not.

echo "My name is $name and I am $age years old."
echo 'My name is $name and I am $age years old.'

output:

My name is John Doe and I am 30 years old.
My name is $name and I am $age years old.

variable manipulation

  1. String concatenation:
string1="Hello, "
string2="world!"
combined_string="$string1$string2"
echo $combined_string

output:

Hello, world!
  1. String length:
string="Hello, world!"
length=${
    
    #string}
echo "The length of the string is $length."

output:

The length of the string is 13.
  1. String interception:
string="Hello, world!"
substring=${string:7:5}
echo "The substring is '$substring'."

output:

The substring is 'world'.

flow control statement

Flow control statements are used to control the execution order of scripts. Common flow control statements include: if-then-else, case, for, while, until, etc.

if-then-else statement

if-then-else statements are used to execute different blocks of code based on conditions.

number=5

if [ $number -lt 10 ]; then
    echo "The number is less than 10."
elif [ $number -eq 10 ]; then
    echo "The number is equal to 10."
else
    echo "The number is greater than 10."
fi

output:

The number is less than 10.

Note that test conditions need to be enclosed in square brackets ([ ]) with spaces between the square brackets.

case statement

A case statement is used to execute different blocks of code depending on the value of a variable.

fruit="apple"

case $fruit in
    "apple")
        echo "It's an apple."
        ;;
    "banana")
        echo "It's a banana."
        ;;
    *)
        echo "It's something else."
        ;;
esac

output:

It's an apple.

for loop

A for loop is used to repeatedly execute a block of code.

for i in {
    
    1..5}; do
    echo "Iteration $i"
done

output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

while loop

A while loop executes a section of code repeatedly while a condition is met.

i=1
while [ $i -le 5 ]; do
    echo "Iteration $i"
    i=$((i + 1))
done

output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

until loop

The until loop repeatedly executes a section of code when a condition is not met.

i=1
until [ $i -gt 5 ];do
    echo "Iteration $i"
    i=$((i + 1))
done

output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

Summarize

This article provides a detailed introduction to variables and flow control statements in Linux Shell scripts, and how to use them to write efficient and readable scripts. By mastering these basic concepts, you will be able to use Shell scripts more skillfully to complete various tasks. Of course, the functions of Shell script are far more than these, and there are many advanced features waiting for you to explore and learn.

Guess you like

Origin blog.csdn.net/qq_42076902/article/details/131512616