Shell programming (Part 1)

1 Overview

The shell is the language we use to communicate with the operating system through the command line.

Shell scripts can be executed directly on the command line, or a set of logic can be organized into a file for easy reuse.
The command line of the terminal can be regarded as a "shell script executed line by line" .

Linux systems generally use bash by default, so the syntax in bash is explained next. The beginning of the file needs to be written #! /bin/bash, indicating that bash is the script interpreter.

2. Hello World example

#! /bin/bash
echo "Hello World!"

Operation mode

作为可执行文件

chmod +x test.sh  # 使脚本具有可执行权限

./test.sh  # 当前路径下执行
Hello World!  # 脚本输出

/xx/test.sh  # 绝对路径下执行
Hello World!  # 脚本输出

用解释器执行

bash test.sh
Hello World!  # 脚本输出

3. Notes

单行注释

# 这是一行注释

echo 'Hello World'  #  这也是注释

多行注释

:<<EOF
第一行注释
第二行注释
第三行注释
EOF

# 其中EOF可以换成其它任意字符串。例如:

:<<abc
第一行注释
第二行注释
第三行注释
abc

:<<!
第一行注释
第二行注释
第三行注释
!

4. Variables

4.1 Basic use

define variables

Note: It is stipulated that no spaces are allowed on both sides of the equal sign, otherwise an error will be reported

name1='shkd'  # 单引号定义字符串
name2="shkd"  # 双引号定义字符串
name3=shkd    # 也可以不加引号,同样表示字符串

use variables

To use variables, you need to add symbols, or symbols, orsymbol , or the {} symbol.花括号是可选的,主要为了帮助解释器识别变量边界

name=shkd
echo $name  # 输出yxc
echo ${name}  # 输出yxc
echo ${name}acwing  # 输出shkdacwing

read-only variable

Variables can be made read-only using readonly or declare.

name=yxc
readonly name
declare -r name  # 两种写法均可

name=abc  # 会报错,因为此时name只读

delete variable

unsetVariables can be deleted.

name=yxc
unset name
echo $name  # 输出空行

4.2 Variable types

  1. Custom variables (local variables):子进程不能访问的变量
  2. Environment variables (global variables):子进程可以访问的变量

Change custom variables to environment variables:

acs@xxx:~$ name=yxc  # 定义变量
acs@xxx:~$ export name  # 第一种方法
acs@xxx:~$ declare -x name  # 第二种方法

Change the environment variable to a custom variable:

acs@xxx:~$ export name=yxc  # 定义环境变量
acs@xxx:~$ declare +x name  # 改为自定义变量

4.3 Strings

Strings can use single quotes, double quotes, or no quotes.

The difference between single quotes and double quotes:

  • The content in single quotes will be 原样输出, 不会执行、不会取变量;
  • content in double quotes 可以执行、可以取变量;
name=yxc  # 不用引号
echo 'hello, $name \"hh\"'  # 单引号字符串,输出 hello, $name \"hh\"
echo "hello, $name \"hh\""  # 双引号字符串,输出 hello, yxc "hh"

get string length

name="yxc"
echo ${
    
    #name}  # 输出3

extract substring

name="hello, yxc"
echo ${name:0:5}  # 提取从0开始的5个字符

5. Default variables

file parameter variable

When executing a shell script, parameters can be passed to the script.$1 is the first parameter, $2 is the second parameter, and so on. 特殊的,$0是文件名(包含路径). For example:

Create the file test.sh:

#! /bin/bash

echo "文件名:"$0
echo "第一个参数:"$1
echo "第二个参数:"$2
echo "第三个参数:"$3
echo "第四个参数:"$4

Then execute the script:

acs@9e0ebfcd82d7:~$ chmod +x test.sh 
acs@9e0ebfcd82d7:~$ ./test.sh 1 2 3 4 #执行脚本 并传入参数
文件名:./test.sh
第一个参数:1
第二个参数:2
第三个参数:3
第四个参数:4

Other parameter dependent variables

parameter illustrate
$# Represents the number of parameters passed in by the file, as in the above example, the value is 4
$* A space-separated string consisting of all parameters, such as the value in the above example"$1 $2 $3 $4"
$@ Each parameter is a string enclosed in double quotes, as in the above example, the value is"$1" "$2" "$3" "$4"
$$ Process ID of the script currently running
$? The exit status of the last command (note that it is not stdout, but exit code). 0 means normal exit, other value means error
$(command) Return commandthe stdout of this command (can be nested)
command Return commandthe stdout of this command (not nestable)

6. Array

Multiple values ​​of different types can be stored in an array .只支持一维数组,初始化时不需要指明数组大小。 数组下标从0开始

definition

Arrays are denoted by parentheses, between elements 用空格隔开. For example:

array=(1 abc "def" yxc)

It is also possible to directly define the value of an element in an array:

array[0]=1
array[1]=abc
array[2]="def"
array[3]=yxc

read a value

格式

${array[index]}

举例:

array=(1 abc "def" yxc)
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}

read the entire array

格式

${array[@]}  # 第一种写法
${array[*]}  # 第二种写法

举例:

array=(1 abc "def" yxc)

echo ${array[@]}  # 第一种写法
echo ${array[*]}  # 第二种写法

array length

类似于字符串

${
    
    #array[@]}  # 第一种写法
${
    
    #array[*]}  # 第二种写法

例如

array=(1 abc "def" yxc)

echo ${
    
    #array[@]}  # 第一种写法
echo ${
    
    #array[*]}  # 第二种写法

Guess you like

Origin blog.csdn.net/qq_46312987/article/details/125047918