The article directories

  • 1. Why learn Shell programming
  • 2. What is a Shell
  • 3. The execution mode of Shell scripts
  • 4. Shell variables
  • 5. Definition of shell variables 1
  • 6. Definition of shell variables 2

1. Why learn Shell programming

  1. Linux O&M engineers need to write Shell programs to manage server clusters.
  2. For JavaEE and Python programmers, the need to write Shell scripts for programs or server maintenance, such as a script to periodically back up a database.
  3. For big data programmers, you need to write Shell programs to manage clusters

2. What is a Shell

  • Shell is a command line interpreter. It provides users with an interface system level program that sends requests to the Linux kernel to run programs. Users can start, suspend, stop and even write programs with Shell. Look at a schematic:

3. The execution mode of Shell scripts

  • Script Format Requirements
  1. Script to#! /bin/bashAt the beginning
  2. The script must have executable permission
  • Write your first Shell script

    Requirements: Create a Shell script, outputhello world!
vim hello.sh
Copy the code
#! /bin/bash
echo "hello,world"
Copy the code
  • Common script execution mode
  1. Method 1(Enter the absolute or relative path of the script)

    Explanation: First to givehello.shThe script+xPermission, and then execute the script

    Such as./hello.shOr use an absolute path/home/test/hello.sh



    Execute using absolute path:

  2. Method 2(sh+ Script)

    Note: Do not assign scripts+xThe permission can be executed directly.

    Such assh hello.sh, absolute paths can also be used

4. Shell variables

Introduction to Shell variables

  1. Variables in Linux Shell are divided into system variables and user – defined variables.
  2. System variables:$HOME, $PWD, $SHELL, $USERAnd so on, for example:echo $HOME, etc.

  3. Show the currentshellAll variables:set

5. Definition of shell variables 1

  • The basic grammar
  1. Define variables:Variable name = value, note: Do not use Spaces
  2. Undo variable:The unset variables
  3. Declare static variables:Readonly variable, note: Nounset
  • Quick start
  1. Case 1: Define variable A
#! /bin/bash
# Case 1: Define variable A
A=100
Output variables need $
echo $A
Copy the code



Add the output variable name:

#! /bin/bash
# Case 1: Define variable A
A=100
Output variables need $
echo A=$A
Copy the code



It can also be enclosed in double quotes, and the output is the same:

echo "A=$A"
Copy the code
  1. Case 2: Undo variable A
#! /bin/bash
# Case 1: Define variable A
A=100
Output variables need $
echo "A=$A"
# Case 2: Undo variable A
unset A
echo "A=$A"
Copy the code



3. Case 3: Declare static variable B=2, nounset

#! /bin/bash
# Case 1: Define variable A
A=100
Output variables need $
echo "A=$A"
# Case 2: Undo variable A
unset A
echo "A=$A"
# example 3: Declare static variable B=2, cannot unset
readonly B=2
echo "B=$B"
Copy the code



Static variables cannot be unset

#! /bin/bash
# Case 1: Define variable A
A=100
Output variables need $
echo "A=$A"
# Case 2: Undo variable A
unset A
echo "A=$A"
# example 3: Declare static variable B=2, cannot unset
readonly B=2
echo "B=$B"
unset B
Copy the code

  • Set line number:set nu

6. Definition of shell variables 2

  • Rules for defining variables
  1. Variable names can consist of letters, numbers, and underscores, but cannot begin withdigitalAt the beginning.5A=200(×)
  2. No Spaces on either side of the equal sign
  3. Variable names are generally capitalized. This is a specification that we should follow
  • Assigns the return value of the command to the variable
  1. A= ‘date’, back quotes, run the command inside, and return the result to variable A
  2. A=$(date) is equivalent to back citation
A=`date`=$(date)
Copy the code
  • Example: Assign the result returned by an instruction to a variable
Assign the result returned by the instruction to the variable
C=`date`
echoC ="$C"Copy the code



or

```bash
Assign the result returned by the instruction to the variable
D=$(date)
echo"D =$D"Copy the code