• 学习笔记丨Shell


    Usage of shell script

    References: Learn Shell, Shell 教程 | 菜鸟教程

    The first line of shell script file begins with #!, followed by the full path where the shell interpreter is located. For example,

    #!/bin/bash
    
    • 1

    To find out the currently active shell and its path, type the commands followed,

    ps | grep $$
    which bash
    
    • 1
    • 2

    1 Variables

    • Variable name can consist of a combination of letters and the underscore “_”.
    • Value assignment is done using the “=” sign and there is no space permitted on either side of = sign when initializing variables.
    • A backslash “” is used to escape special character meaning. (转义特殊字符含义)
    • Encapsulating the variable name with ${} is used to avoid ambiguity.
    • Encapsulating the variable name with “” will preserve any white space values.

    Example

    BIRTHDATE="Dec 13, 2001"
    Presents=10
    BIRTHDAY='date -d "$BIRTHDATE" +%A'if [[ "$BIRTHDATE" == "Dec 13, 2001" ]]; then
        echo "BIRTHDATE is correct, it is ${BIRTHDATE}"
    fi
    if [[ $Presents == 10 ]]; then
        echo "I have received $Presents presents"
    fi
    if [[ "$BIRTHDAY" == "Thursday" ]]; then
        echo "I was born on a $BIRTHDAY"
    fi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    special variables

    • $0 - The filename of the current script.
    • $n - The Nth argument passed to script was invoked or function was called.
    • $# - The number of argument passed to script or function.
    • $@ - All arguments passed to script or function.
    • $* - All arguments passed to script or function.
    • $? - The exit status of the last command executed.
    • $$ - The process ID of the current shell. For shell scripts, this is the process ID under which they are executing.
    • $! - The process number of the last background command.

    2 Passing Arguments

    The i-th argument in the command line is denoted as $i$0 references to the current script, $# holds the number of arguments passed to the script,$@ or $* holds a space delimited string of all arguments passed to the script.

    Example

    # function
    function File {
        # print the total number of arguments
        echo $#
    }# '-lt': less than 
    if [[ ! $# -lt 1 ]]; then
        File $*
    fi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3 Array

    • An array is initialized by assign space-delimited values enclosed in ().
    • The total number of elements in the array is referenced by ${#arrayname[@]}.
    • Traverse all the elements in the array is referenced by ${arrayname[@]}.
    • The array elements can be accessed with their numeric index. The index of the first element is 0.
    • Some members of the array can be left uninitialized.
    • The elements in array can be compared with elements in another array by index and loops.

    Example

    # arrays
    fruits=("apple" "banana" "tomato" "orange")
    # The total number of elements in the array is referenced by ${#arrayname[@]}
    echo ${#fruits[@]}
    fruits[4]="watermelon"
    fruits[5]="grape"
    echo ${fruits[@]}
    echo ${fruits[4]}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4 Basic Operators

    • a + b addition (a plus b)
    • a - b substraction (a minus b)
    • a * b multiplication (a times b)
    • a / b division (integer) (a divided by b)
    • a % b modulo (the integer remainder of a divided by b)
    • a**b exponentiation (a to the power of b)

    Example

    # basic operators
    A=3
    B=$((12 - 3 + 100 * $A + 6 % 3 + 2 ** 2 + 8 / 2))
    echo $B
    
    • 1
    • 2
    • 3
    • 4

    5 Basic String Operations

    • String Length: ${#stringname}
    • Find the numerical position in $STRING of any single character in $SUBSTRING that matches.
      • expr index "$STRING" "$SUBSTRING"
    • Substring Extraction
      • Extract substring of length $LEN from $STRING starting after position $POS. Note that first position is 0.
        • echo ${STRING:$POS:$LEN}
      • If $LEN is omitted, extract substring from $POS to end of line.
        • echo ${STRING:2}
    • Substring Replacement
      • Replace first occurrence of substring with replacement
        • echo ${STRING[@]/substring/newsubstring}
      • Replace all occurrences of substring
        • echo ${STRING[@]//substring/newsubstring}
      • Delete all occurrences of substring (replace with empty string)
        • echo ${STRING[@]// substring/}
      • Replace occurrence of substring if at the beginning of $STRING
        • echo ${STRING[@]/#substring/newsubstring}
      • Replace occurrence of substring if at the end of $STRING
        • echo ${STRING[@]/%substring/newsubstring}
      • Replace occurrence of substring with shell command output
        • echo ${STRING[@]/%substring/$(shell command)}

    Example

    # string operation
    STRING="this is a string"
    echo "The length of string is ${#STRING}"
    #Find the numerical position in $STRING of any single character in $SUBSTRING that matches
    SUBSTRING="hat"
    expr index "$STRING" "$SUBSTRING" # 1 't'
    # substring extraction
    POS=5
    LEN=2
    echo ${STRING:$POS:$LEN}
    echo ${STRING:10}
    # substring replacement
    STRING="to be or not to be"
    # replace the first occurrence
    echo ${STRING[@]/be/eat} 
    # replace all occurences
    echo ${STRING[@]//be/eat} 
    # the begin
    echo ${STRING[@]/#be/eat now}
    # the end
    echo ${STRING[@]/%be/eat}
    echo ${STRING[@]/%be/be on $(date +%Y-%m-%d)} 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    6 Decision Making

    6.1 If-else
    if [ expression1 ]; then
        #statement1
    elif [ expression2 ]; then
        #statement2
    else
        #statement3
    fi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    6.2 Types of numeric comparisons
    $a -lt $b    $a < $b
    $a -gt $b    $a > $b
    $a -le $b    $a <= $b
    $a -ge $b    $a >= $b
    $a -eq $b    $a is equal to $b
    $a -ne $b    $a is not equal to $b
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    6.3 Types of string comparisons
    "$a" = "$b"     $a is the same as $b
    "$a" == "$b"    $a is the same as $b
    "$a" != "$b"    $a is different from $b
    -z "$a"         $a is empty
    
    • 1
    • 2
    • 3
    • 4
    6.4 logical combinations
    &&, ||, !
    
    • 1
    6.5 case structure
    case "$variable" in
        "$condition1" )
            command...
        ;;
        "$condition2" )
            command...
        ;;
    esac
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    7 Loops

    # bash for loop
    for arg in [list]
    do
     command(s)...
    done
    # bash while loop
    while [ condition ]
    do
     command(s)...
    done
    #bash until loop
    # basic construct
    until [ condition ]
    do
     command(s)...
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • break and continue can be used to control the loop execution of for, while and until constructs.

    8 Shell Functions

    function function_name {
      command...
    }
    
    • 1
    • 2
    • 3

    9 Trap Command

    It often comes the situations that you want to catch a special signal/interruption/user input in your script to prevent the unpredictables.
    Trap is your command to try:

    • trap

    Example

    trap "echo Booh!" SIGINT SIGTERM
    ​
    function booh {
        echo "booh!"
    }
    trap booh SIGINT SIGTERM
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    10 File Testing

    选项功能
    -e filenameif file exist
    -r filenameif file exist and has read permission for the user
    -w filenameif file exist and has write permission for the user running the script/test
    -x filenameif file exist and is executable
    -s filenameif file exist and has at least one character
    -d filenameif directory exist
    -f filenameif file exist and is normal file

    11 Pipelines

    command1 | command2
    
    • 1

    12 Input and Output Redirection

    command > file  # Redirect the output to file.
    command < file  # Redirect the input to file.
    command >> file # Redirect the output to file appends.
    n > file  # Redirect the file with descriptor 'n' to file.
    n >> file  # Redirect the file with descriptor 'n' to file appends.
    n >& m  # Merge the output files m and n.
    n <& m  # Merge the input files m and n.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    13 printf

    printf  format-string  [arguments...]
    #example
    printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg  
    
    • 1
    • 2
    • 3
  • 相关阅读:
    typescript80-使用配置文件编译文件
    【深度学习手记】使用DNN训练与CNN训练对数据集的要求不一样,为什么CNN网络训练输入的数据需要4维的
    m基于数字锁相环DPLL的分频器simulink仿真
    阐述 Git 命令 reset 和 revert
    风光储一体化能源中心 | 数字孪生智慧能源
    Node.js之path路径模块
    模块datetime
    Disruptor-简单使用
    二维码智慧门牌管理系统升级解决方案:采集项目的建立与运用
    实施 DevSecOps 最佳实践
  • 原文地址:https://blog.csdn.net/qq_45909595/article/details/136308235