The five minute package teaches you to write Shell scripts

Keywords: Linux ssh Ubuntu

Java developers must have come into contact with the Linux system, so many times during the development process, we break our project into a jar package or war package, upload it to the specified directory of our server through XFTP, and then run the startup script at one end, Make our project accessible, like. / sh service.sh start, and then start the Shell script of SH we have written. Next, let's learn about how Shell scripts are written.

Shell script

What is a shell script? Shell is a command interpreter, which is used to interpret and execute commands and programs entered by users, that is, every time our users input a command, the shell will execute a command accordingly. When a command or program statement is not executed on the command line, but through a program file, the program file is called a shell script.

In our Shell script, there will be various contents, assignment, calculation, loop and a series of operations. Next, let's see how to write this Shell script

1. View the default Shell of your current system

echo $SHELL

Output: / bin/bash

2. View the Shell supported by the system

cat /etc/shells

Output:

/bin/sh /bin/bash /usr/bin/sh /usr/bin/bash

In other words, our ECS supports us to arrange Shell scripts for him here

How did the Shell script come out

At this time, let's arrange the SH file, create a folder, and then create a sh file in it.

mkdir /usr/local/shelltest
touch test.sh

After creation, let's edit the content

vim test.sh
#!/bin/bash
echo "Hello World Shell"

Then we come out and run the first script of our Shell

bash test.sh

The result is Hello World Shell

A very simple script appears. Next, we will analyze a wave. What did we write?

#!/bin/bash

# Is a convention tag that tells the system what interpreter the script needs to execute, that is, which Shell to use

We also used echo $SHELL to check the default sh parser of our system. We saw / bin/bash before, so when we wrote the Shell script, we wrote this in the default convention at the beginning, which is explained by / bin/bash,

So how do we call him like the Shell script we called in our current directory? Like this. / sh service.sh start

1. Authorization,

Let's try without authorization to see if it can be called through. / test.sh

Bash:. / test.sh: permission denied will prompt this, that is, there is no authorization definition,

Authorization command: chmod +x test.sh

2. Execute. / test.sh

Then the call can be output normally, that is to say, execute the script command under the current directory.

Shell script variables
  1. Defining variables and using

Variable naming is actually very simple. Let's try it first

name=zhiyikeji

How do we use variables at this time? In fact, just add a symbol in front of it$

echo $name
[root@andy ~]# echo $name
zhiyikeji
[root@andy ~]# echo ${name}
zhiyikeji

The above two ways of writing are OK. There is little difference between adding and not adding curly braces. You can omit them. You can use the variables you define directly on $name

The meaning of using parentheses is generally to distinguish some variables. For example, you wrote a series of contents. It may be echo $nameismmyfriend. If it is a bit embarrassed to connect them, you can use brackets to distinguish them. When echo ${name}ismyfriend does not use parentheses, he will go to nameismyfriend, and he will not be able to come out the effect we want.

  1. Delete self defined variables
unset name

At this time, we will remove the variable name=zhiyikeji we just defined. We can call our variable to see what it is?

echo $name
[root@iZbp10j01t7sgfqekyefpoZ ~]# unset name
[root@iZbp10j01t7sgfqekyefpoZ ~]# echo $name

Does this prove that the variables defined by ourselves have been deleted

  1. read-only variable

So we need a keyword. You can definitely think of what the keyword is readonly

Let's assign a value to name first, then use readonly to set read-only, and then try to change it,

[root@iZbp10j01t7sgfqekyefpoZ ~]# name=zhiyikeji
[root@iZbp10j01t7sgfqekyefpoZ ~]# echo $name
zhiyikeji
[root@iZbp10j01t7sgfqekyefpoZ ~]# readonly name
[root@iZbp10j01t7sgfqekyefpoZ ~]# echo $name
zhiyikeji
[root@iZbp10j01t7sgfqekyefpoZ ~]# name=ceshi
-bash: name: readonly variable
[root@iZbp10j01t7sgfqekyefpoZ ~]# 

It turns out to be true. If you don't set read-only, can you assign values again? Let's test the age,

[root@iZbp10j01t7sgfqekyefpoZ ~]# age=10
[root@iZbp10j01t7sgfqekyefpoZ ~]# echo $age
10
[root@iZbp10j01t7sgfqekyefpoZ ~]# age=20
[root@iZbp10j01t7sgfqekyefpoZ ~]# echo $age
20

So we can be sure that readonly is to set the read-only keyword, remember?

Can read-only variables be deleted? After all, there are always tough interviewers who will ask this difficult question. However, all the ways that ah fan tried seem to fail. Ah fan directly restarted his server, so that the temporary variables do not exist!

Process control of Shell script

Seriously, the process control number of Shell scripts is usually yyds. Why do you say that? When you write most scripts, the process control places are always the most, such as judgment, selection, and a series of functions. When you skillfully use them at that time, you find it really interesting.

IF

Let's first talk about the simplest if else, which is also the judgment we most often use. When writing Shell scripts, it's not like writing them directly in Java

if(...){
    
}else{
    ....
}

The syntax in xshell is not like this. Xshell syntax:

if ...
then
    
    ...
else
    ...
fi

The fi at the end is the reverse spelling of if. We can write an if script to try to understand the process.

#! /bin/bash
  
if [ $1 -gt 2 ];


then
        echo "Value greater than 2"

else
        echo "Value less than 2"

        exit
fi

For the record,

  • -ge identifies the symbol greater than or equal to;
  • -le represents the less than or equal sign;
  • -gt indicates greater than symbol;
  • -lt means less than symbol;
  • -eq represents the equal sign;
  • -ne is not equal to the symbol;

What we wrote in the above script is that we pass in a value to the script, compare the value with the size of 2, and then output the content we specified.

You can see it after running

[root@iZbp10j01t7sgfqekyefpoZ shelltest]# sh test2.sh 1
 Value less than 2
[root@iZbp10j01t7sgfqekyefpoZ shelltest]# sh test2.sh 3
 Value greater than 2

$1 represents the first parameter we input to the shell script, $0 is the name of the shell script you write, and $2 is the second parameter we pass to the shell script

When you deploy some projects, whether the start command is very concise, that is, sh service.sh start is similar. Let's see how this is written. This uses another piece of content, similar to if, which is also available in Java, that is, Case

Case

Let's look at the syntax of Case first,

Case... esac is actually very similar to case in Java. The case statement matches a value with a pattern. If the matching is successful, execute the matching command. esac is an end flag.

case value in
 Match value 1)
    command1
    command2
    
    ;;
Match value (2)
    command1
    command2
    ;;
esac

Just talk but not practice. Let's do it. Let's try to write a script to do it. Use the SH service.sh start we just mentioned to test.

case $1 in
  
start)
        #Output start command
        echo "start Already started"
        ;;
stop)
        #Output stop command
        echo "stop Command execution"
        ;;
esac
exit

Let's look at the results

[root@iZbp10j01t7sgfqekyefpoZ shelltest]# sh service.sh start
start Already started
[root@iZbp10j01t7sgfqekyefpoZ shelltest]# sh service.sh stop
stop Command execution

So what does this Shell script mean? In fact, it is very simple. Match the first character we pass in and compare it with start and stop. If it matches, output the command and finally exit.

Does it feel less complicated?

For

When it comes to process control, we must say for. After all, for loops are an important play in Java.

Let's look at his format first

for i in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

So did we say the same way as the for loop in Java? For example, this for ((I = 1; I < = J; I + +))

In fact, it also supports this. Let's write a try.

j=$1
for ((i=1;i<=j;i++))
do
        echo $i
done

Take a look

[root@iZbp10j01t7sgfqekyefpoZ shelltest]# sh fortest.sh 6
1
2
3
4
5
6

Since there is for, is there a while? Yes, yes, it does have the meaning of while and loop, but the writing method is slightly different

While
while condition
do
    command
done

Let's try printing the 99 multiplication table

a=1
b=1
while ((a <=9))
do
        while ((b<=a))
                do
                        let "c=a*b"   #Declare variable c
                        echo -n  "$a*$b=$c "
                        let b++
                done
                        let a++

     let b=1  #Because each multiplication table starts with 1, b needs to be reset

             echo "" #Display to screen wrap

     done
[root@iZbp10j01t7sgfqekyefpoZ shelltest]# sh whileTest.sh 
1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

Isn't it easy?

In fact, the preparation of Shell scripts is generally improved in practical applications. Simply writing test scripts can also enable us to fully master knowledge. We generally write some simple scripts. Isn't there operation and maintenance for the complex?

Posted by pages on Wed, 22 Sep 2021 07:14:50 -0700