Introduction to Shell Programming Basic Grammar

Introduction to Shell Script

concept

Shell script is a text file containing a series of commands
Shell reads this file and executes commands

Application scenarios

  • Repetitive operation
  • Bulk transaction processing
  • Automated operation and maintenance
  • Scheduled task execution

The first shell script

Write the first Shell script helloword.sh

#!/bin/bash
echo “Hello World”

#! is an agreed mark, which tells the system what interpreter the script needs to execute

Common execution methods of scripts

The first: use the relative path or absolute path of bash or sh+ script (do not give script +x permission)

sh helloworld.sh
bash helloworld.sh

The second type: use the absolute path or relative path of the input script to execute the script (must have executable permission +x)"

  • First, give the +x permission to the helloworld.sh script
chmod 777 helloworld.sh
  • Execute script
./helloworld.sh

Variables in Shell

System variable

Common system variables:

$HOME, $PWD, $SHELL, $USER, etc.

Note: Display all variables in the current shell: set

[root@cent02 ~]# echo $PWD
/root

Custom variable

Basic grammar

  • Define variable: variable = value
  • Undo variable: unset variable
  • Declare static variables: readonly variables, note: cannot be unset

Rules for defining variables

  • Variable names can consist of letters, numbers, and underscores, but they cannot start with numbers.
  • There can be no spaces on both sides of the equal sign
  • Variable names are generally used to uppercase

Assign the return value of the command to a variable

  • A=`ls -la` backquote, run the command inside, and return the result to the variable A
  • A=$(ls -la) is equivalent to backticks
       NN0[root@cent02 ~]# A=`ll`
[root@cent02 ~]# echo $A
total 48 -rw-------. 1 root root 1320 Aug 11 03:20 anaconda-ks.cfg -rw-r--r--. 1 root root 18430 Aug 18 03:57 ‪H:testschool.sql -rw-r--r--. 1 root root 10399 Aug 19 01:12 

Set environment variables

export variable name=variable value (function description: output shell variables as environment variables)

source configuration file (function description: make the modified configuration information take effect immediately)

echo $ variable name (function description: query the value of environment variable)

Positional parameter variable

  • $n (Function description: n is a number, $0 represents the command itself, $1-$9 represents the first to ninth parameters, parameters above ten, parameters above ten need to be enclosed in braces, such as ${10})
  • $* (Function description: This variable represents all the parameters in the command line, $* regards all the parameters as a whole)
  • $@ (Function description: This variable also represents all the parameters in the command line, but $@ treats each parameter differently)
  • $# (Function description: This variable represents the number of all parameters in the command line)

Predefined variables

  • $$ (Function description: Process ID (PID) of the current process)
  • $! (Function description: Process ID (PID) of the last process running in the background)
  • $? (Function description: the return status of the last executed command. If the value of this variable is 0, it proves that the previous command was executed correctly; if the value of this variable is not 0 (which number is determined by the command itself), then Prove that the previous command was executed incorrectly.)

Operator

  • "$((Expression))" or "$[Expression]"
    expr m + n Note that there must be spaces between expr operators
  • expr m-n
    expr *, /,% multiplication, division, remainder

Conditional judgment

[condition] (note that there must be spaces before and after condition)

#Non-empty returns true, you can use $? to verify (0 is true, >1 is false)

Commonly used judgment conditions

  • Comparison of two integers

    Options Description
    -eq equal
    -born not equal to
    -gt more than the
    -lt Less than
    -the less than or equal to
    -give greater than or equal to
  • Judge according to file permissions

    Options Description
    -d Determine whether it is a directory
    -e Determine whether the directory or file exists
    -f Determine whether it is a file
    -r Determine whether a file or directory is readable by the current user
    -w Determine whether the file or directory is writable by the current user
    -x Determine whether the file or directory is executable for the current user

Process control

if judgment

if [ 条件判断式 ];then 

	程序 

fi

or

if [ 条件判断式 ] 

	then

		程序 

	elif [条件判断式]

	then

		程序 

fi

Note: (1) [Conditional judgment formula], there must be a space between the brackets and the conditional judgment formula (2) The second method is recommended

case statement

case $变量名 in 

	"值 1") 

		如果变量的值等于值 1,则执行程序 1 

		;; 

	"值 2") 

		如果变量的值等于值 2,则执行程序 2 

		;; 

…省略其他分支… 

	*)

		如果变量的值都不是以上的值,则执行此程序

		;; 

esac

for loop

for 变量 in 值 1 值 2 值 3… 

	do

		程序 

	done
for (( 初始值;循环控制条件;变量变化 )) 

	do

		程序 

	done

while loop

while [ 条件判断式 ] 

	do

		程序 

	done

read read console input

read(选项)(参数) 

Options:

-p: Specify the prompt when reading the value;

-t: Specify the waiting time (seconds) when reading the value. If it is not entered within the specified time, it will not wait anymore. .

parameter

Variable: Specify the variable name to read the value

function

System function

  • basename basic syntax

Function: return to the last part of the full path, often used to get the file name

basename [pathname] [suffix]

basename [string] [suffix] (Function description: basename command will delete all prefixes including the last ('/')

Character, and then display the string.

Options:

suffix is ​​the suffix. If suffix is ​​specified, basename will remove suffix from pathname or string.

  • Basic syntax of dirname

Function: return to the last/before part of the complete path, often used to return to the path part

dirname file absolute path (function description: remove the file name from the given file name containing the absolute path (non-directory

Part), and then return to the remaining path (part of the directory))

Custom function

[ function ] funname[()] 

{
    
    

	Action; 

	[return int;] 

}
调用直接写函数名:funname []

Guess you like

Origin blog.csdn.net/zmzdmx/article/details/108281146