Shell programming in Linux

Shell programming in Linux

Quick Start with Shell Programming

Why should you learn Shell programming?
1. When Linux operation and maintenance engineers perform server cluster management, they need to write Shell programs for server management.
2. For JavaEE and Python programmers, due to work needs, your boss will ask you to write some Shell scripts for program or server maintenance, such as writing a script to regularly back up the database.
3. For big data programmers, they need to write Shell programs to manage the cluster.

What is Shell?
Shell is a command interpreter that provides users with a system-level program that sends requests to the Linux kernel to run program interfaces. Users can use Shell to start, suspend, stop, and even write some programs.

Quick introduction to Shell programming?
Enter the Linux terminal and write a Shell script hello.sh:

#!/bin/bash
echo 'hello world!'

You can directly create and write a file using the vim command. As shown below:

run:

# Method 1
sh hello.sh

# Method 2
sudo chmod +x hello.sh
./hello.sh



As can be seen from the two pictures above, each method will successfully output hello world!

Description:

  • #! Tell the system what interpreter this script requires to execute.
  • The file extension .sh is not mandatory.
  • Method 1: Run the interpreter directly, using hello.sh as a parameter of the Shell interpreter. At this time, the shell script does not need to specify the interpreter information, and the first line can be removed.
  • Method 2 hello.sh is run as an executable program, and the interpreter must be specified in the first line of the Shell script.

Shell variables

Definition
Shell variables are divided into system variables and custom variables. System variables include $HOME, $PWD, $USER, etc., as shown below:

The above are all system variables. We can display all system variables through set, as shown below:

For example, we can also output the value of the system variable BASH, as shown below:

The above are all system variables in Linux, but in Linux we use custom variables more often. Basic syntax of custom variables:

  • Define variables: variable name = variable value, there can be no spaces on both sides of the equal sign, and variable names are generally in uppercase letters.
  • Delete a variable: unset variable name.
  • Set read-only variables: readonly variable name is equivalent to setting read-only. In fact, it means that the variable value cannot be modified after it is set for the first time.
  • Use variables: $variablename

Define a shell script test2.sh to test the use of variables, as shown below:

The result of executing the shell script is as follows:

There is another point that needs attention. When I use sh test2.sh to execute the shell script, the second B variable is not output, as shown below:

Therefore, using the ./test2.sh command to execute a shell script is more powerful than using the sh test2.sh command to execute a shell script. In the future, try to use ./test2.sh to execute shell scripts.

Positional parameter variables

Basic Grammar

  • $n: $0 represents the command itself, $1-$9 represents from the 1st parameter to the 9th parameter, and parameters above 10 use curly brackets ${10}
  • $*: All parameters on the command line, and treat all parameters as a whole
  • $@: All parameters in the command line, and each parameter is treated differently
  • $#: Number of all parameters

An example is shown below:

The result of executing the test5.sh shell script is as follows:

Note that when $0 is set, the parameters will not be output.

Operator

Basic Grammar

  • $((operational expression)) or $[operational expression]
  • expr m + n Note that there needs to be a space between the expr operators. If you want to assign the result of expr to a variable, you need to use backticks, such as TEMP=`expr 2 + 3`
  • expr \* , /, % represent multiplication, division and remainder respectively. Note that the sign of multiplication here is quite special.

Define a test3.sh case as follows:

The result of executing the test3.sh shell script is as shown below:

Conditional judgment

Basic Grammar

# [ condition ] Note that there should be spaces before and after condition. Returns 0 if it is not empty, 0 is true, otherwise it is false.
# Possible situations are as follows:
if [conditional expression]
then
\tprogram
fi

if [conditional expression]
then
\tprogram
else
\tprogram
fi

if [conditional expression]
then
\tprogram
elif [conditional expression]
then
\tprogram
else
\tprogram
fi

Write a test4.sh example, as shown below:

The result of running the test4.sh shell script is as follows:

case branch

Basic Grammar

case $variable name in

value 1)
Program 1
;;

Value 2)
Program 2
;;

Value 3)
Program 3
;;

*)
If the variable value is not equal to all the columns listed above then execute this procedure
;;

esac

An example is shown below:

Test the execution results of the test6.sh shell script, as shown below:

for loop

Basic Grammar

# Syntax 1
for variable name in value 1 value 2 value 3...
do
\tprogram
done


# Syntax 2
for ((i=1;i<=100;i + + )
do
\tprogram
done

An example is shown below:

The results of running the test7.sh shell script are as follows:

while loop

Basic Grammar

while [conditional judgment expression]
do
\tprogram
done

The example output is the value of 1 added to 100, as shown below:

The execution result of the test8.sh shell script file is as follows:

Function

Like other programming languages, Shell programming has system functions and custom functions.

System functions
The first system function is the basename function. This function can obtain the last file name under the absolute path;
The second system function is the dirname function. This function can obtain the path of the file under the absolute path. In fact, it removes the following file name;
Use the following image:

One is used to get the file name, and the other is used to get the project path of the file.

Custom function
Basic Grammar

function funcname(){<!-- -->
Action;
[return int;]
}

An example is shown below:

The result of executing the test9.sh shell script is as follows:

Can parameters be added to functions in shell programming?
Unlike most programming languages, functions in Shell cannot specify parameters when they are defined, but they can be passed when called. Pass parameters to the function: Just separate the given parameter list with a blank after the function name, such as: testfunc arg1 arg2…; In the function body, you can use $1,

2

,

Call these parameters; you can also use

2, …call these parameters; you can also use

2,…Call these parameters; you can also use special variables such as @, $*, $#.

An example of passing parameters after the function is as shown below:

Then execute our test10.sh shell script and see what the execution result is, as shown below:

nohup command

Basic concepts

nohup command Purpose: Run commands without hanging up.
Syntax: nohup Command [ Arg … ] [ & amp; ]
Description: The nohup command runs the command specified by the Command parameter and any related Arg parameters, ignoring all hangup (SIGHUP) signals. Use the nohup command to run programs in the background after logging out. To run the nohup command in the background, add & amp; (the symbol for “and”) to the end of the command.

nohup means to run continuously and is the abbreviation of no hup up, which means uninterrupted and uninterrupted. When running a process and don’t want it to close when you log out of your account, you can use nohup. What does that mean? After using nohup to start a deployed project on Linux, even if you log out and log in to the Linux server again, the project on the server will still be started, without you having to restart it.

Personal understanding: First understand that nohup is used to run program jar packages. If we want to start a program on Windows, then we can just click Run directly in idea. But on Linux, if we want to start a program, How to do it? You can directly use the command java -jar to program the jar package, as shown below:

It can be found that the startup parameters are wrong when we start. Why is this? Let’s take a look at the code in our startup main class, as shown below:

So how do we configure the startup parameters when we start the program? And the JVM startup parameters, stack memory, etc.? You can use nohup to start, just follow the startup parameters directly after the command, as shown below:

After the above shell script is started, our program can be started on Linux. Therefore, shell scripts can be used on Linux to start projects deployed on Linux.

Use the eval command to output the configuration in one file to the variable of another shell script

First we create a file.txt file with the following content:

Next, we can directly input the variables in this file into the corresponding variables of the shell script through the eval command in the shell script. We create a test9.sh script with the following content:

When we execute the above shell script, the three variable values output are actually the three variable values we defined in the file.txt file. The output result is as follows:

It can be found that the result of executing this script is consistent with the information configured in the file.txt file.

Pay special attention not to close the processes of other web projects in the shell script

When using a shell script to start a project, if the current project has already been started, you need to first find out the process ID of the current project, and then kill the process. Note that when querying the process ID, you must add the characteristics of the current project. For example, you can add the jar package of the current project directly after grep to filter, as shown below:

Use ps -ef | grep your project characteristics | grep -v grep | awk {print $2}’ command to filter out the process number of your project, as shown below: