shell script (2)

Now I know what the shell does? After experiencing it for a while, let ’s take a look at the composition of the shell, like the composition of java.

 

1. Variables

A variable in a shell script can be a value, a command, or a path . The format for defining a variable is: variable name = value of the variable, the symbol $ needs to be added when referencing the variable in the script

The following example will add variables

  #!/bin/bash
## In this script we will use variables.
## Writen by homey 2019-11-13

    d=`date +%H:%M:%S`				#反引号的作用是将引号内的字符串当成shell命令执行,返回命令的执行结果
    echo "The script begin at $d." #引用变量前面要加$
    echo "Now we'll sleep 2 seconds."
    sleep 2 #睡2秒再执行下面
    d1=`date +%H:%M:%S`
    echo "The script end at $d1."

Run and see for yourself.

 

2. Numerical operations

The following example will add operations

#! /bin/bash 
## For get the sum of two numbers.
## Writen by homey 2019-11-13

a=1
b=2
sum=$[$a+$b]  #数学计算要用[]括起来,并且前面要加上符号$。
echo "$a+$b=$sum"

Run by myself

 

3. Interact with the user

#!/bin/bash
## Using 'read' in shell script.
## Writen by homey 2019-11-13

    read -p "Please input a number:" x
    read -p "Please input another number:" y
            sum=$[$x+$y]
    echo "The sum of two numbers is:$sum"

Run it yourself to experience it.

 

4. Shell script preset variables

#!/bin/bash
## 测试预设变量
## Writen by homey 2019-11-13

    sum=$[$1+$2]
    echo "sum=$sum"

The result is

cosun@cosun:/usr/local/sbin$ ./test4.sh 5 46
sum=51

$1Represents the first parameter of the $2script , represents the second parameter of the script , and so on .

There is no limit to the number of preset variables in a script. In addition $0, it represents the name of the script itself .

 

5. Logic judgment in Shell script

This shell script is still different from our java, please see below

#!/bin/bash 

read -p "Please input you score: " a
if ((a<60));then
        echo "You didn't pass the exam."
elif ((a>=60)) && ((a<90));then				# elif相对if,再做一次判断
        echo "Good!You passed the exam."
else
        echo "Very good!Your score is very high."
fi

Analyzing numerical size in addition can be used(( )) outside the form, may also be used [], but not >, <, =such a symbol, and to the use of -lt(less than), -gt(greater than), -le(or less), -ge(greater than or equal to), -eq(equal to), -ne(Not equal to) . See what you like to use.

In addition to if, you can also use case

#!/bin/bash

read -p "Input a number: " n
a=$[n%2]
case $a in
1)
        echo "The number is odd."				# odd:奇数
        ;;
0)
        echo "The number is even."				# even:偶数
        ;;
*)
        echo "It's not a number!"
esac

caseScripts are often used to write startup scripts for system services .

6. Loops in the shell

#!/bin/bash 

for i in `seq 1 5`; do				# seq 1 5 表示从1到5的一个序列
        echo $i
done

Or while loop

#!/bin/bash 

a=6
while [ $a -ge 1]
do
        echo $a
        a=$[$a-1]
done

7. Functions in shell

#!/bin/bash

function sum()
{
        sum=$[$1+$2]
        echo $sum
}
sum $1 $2				#预设变量$1 $2

Everyone can play freely, then join break or continue or even exit in the shell

In fact, just like our current JAVA programming language, the shell is just an entry here, you can go deeper if you want.

Finally, thanks again:

https://blog.csdn.net/miss1181248983/article/details/81278937

I basically follow his blog to practice.

Published 51 original articles · Like 4 · Visitor 7897

Guess you like

Origin blog.csdn.net/u012174809/article/details/103044542