Technology Sharing

Shift command learning

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Shift command learning

Reference link: https://www.cnblogs.com/wjq/p/14863580.html

1. Several special shell variables

#!/bin/bash
echo "$0:$0"    # 当前脚本的文件名(间接运行时还包括绝对路径)。
echo "$n:$n"    # 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是 $1 。
echo "$#:$#"    # 传递给脚本或函数的参数个数。
echo "$*:$*"    # 传递给脚本或函数的所有参数。
echo "$@:$@"    # 传递给脚本或函数的所有参数。被双引号 (" ") 包含时,与 $* 不同,下面将会讲到。
echo "$?:$?"    # 上个命令的退出状态,或函数的返回值。
echo "$$:$$"    # 当前 Shell 进程 ID。对于 Shell 脚本,就是这些脚本所在的进程 ID。
echo "$_:$_"    # 上一个命令的最后一个参数。
echo "$!:$!"    # 后台运行的最后一个进程的 ID 号。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

$* and$@ All parameters passed to a function or script are represented by "$1" when not enclosed by double quotes (""). 2 " … " 2" … " 2""n" will output all parameters. However, when they are enclosed in double quotes (""),$*" will treat all parameters as a whole, starting with "$1 $2 ... n" to output all parameters; "' n" to output all parameters; "`n"Output all parameters in the form of;"‘@`" will separate the parameters into "$1", " 2 " 、 … 、 " 2"、…、" 2""n" format to output all parameters.

  • Example
[root@localhost myself]# sh test.sh A B B D E
$0:test.sh
$n:
$#:5
$*:A B B D E
$@:A B B D E
$?:0
$$:109424
$_:$$:109424
$!:
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2. Use of shift

shift The command is used to move parameters (left shift). It is usually used to traverse each parameter in turn and then process it accordingly without knowing the number of parameters passed in. It is common in the startup scripts of various programs in Linux.

2.1 Shifting one parameter at a time

  • Example
#!/bin/bash
while [ $# != 0 ]
do
    echo -e "参数值为 $1, 参数个数为 $#"
    shift
done

# 每次运行 shift(不带参数的),销毁一个参数(变量的个数($#)减一),后面的参数前移(不管参数之间相隔一个或者多个空格)。
[root@localhost myself]# sh shift1.sh a b c d e
参数值为 a, 参数个数为 5
参数值为 b, 参数个数为 4
参数值为 c, 参数个数为 3
参数值为 d, 参数个数为 2
参数值为 e, 参数个数为 1

# 注意:上述脚本,如果 shift 移动参数变为 2 ,在参数个数为奇数时则会出现死循环,如果是偶数则没问题。
# 脚本示例:
#!/bin/bash
while [ $# != 0 ]
do
    echo -e "参数值为 $1, 参数个数为 $#"
    shift 2		# 这里移动位数变为2
done
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

2.2 Moving multiple parameters at a time

The number of parameters that the shift command moves at a time is specified by the parameters it carries. For example, now there are $1, $2, $3, …, $9 parameters. When the shell program processes the first 4 command line parameters (that is, there are only 5 parameters left), you can use shift 4 The command moves $9 to $1.

  • Example: Customize the number of parameters for each shift.
#!/bin/bash
while [ $# -gt 5 ]
do
    echo -e "参数值为 $1, 参数个数为 $#"
    shift
done
shift 4
echo -e "参数值为 $1, 参数个数为 $#"


[root@localhost myself]# sh shift2.sh a1 b2 c3 d4 e5 f6 g7 h8 i9
参数值为 a1, 参数个数为 9
参数值为 b2, 参数个数为 8
参数值为 c3, 参数个数为 7
参数值为 d4, 参数个数为 6
参数值为 i9, 参数个数为 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2.3 Use with case

Case Introduction

case ..... esac andif ... then ... fi They are all conditional judgment sentence structures. They use reverse alphabetical words to pair with normal alphabetical words, such asif Statement, end withfi Come pair up,esac andcase pair

case $参数变量 in	# 以关键字 case 作为开始
"变量值1")			# 变量值用双引号括起来,关键字用小括号括起来
    程序段1		# 对应的逻辑
    ;;              # 每个类别结尾使用两个连续的分号来处理
"变量值2")
    程序段2
    ;;
"变量值3")
    程序段3
    ;;
*)                 # 最后一个位置参数值会用*来代替所有其他值
    程序段4
    ;;
esac               # 关键字 esac(case 的反写) 结束
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • Example: shift Matchingcase ... esac Implement shell script manual parameter transfer processing
#!/bin/bash
while [ -n "$1" ]
do
  case "$1" in
    -a)
        echo "发现 -a 选项"
        ;;
    -b)
        echo "发现 -b 选项"
        echo "-b 选项的参数值是:$2"
        shift
        ;;
    -c)
        echo "发现 -c 选项"
        echo "-c 选项的参数值是:$2"
        shift
        ;;
    *)
        echo "$1 是一个未知选项"
        ;;
  esac
  shift
done

[root@localhost myself]# sh shift3.sh -a -b 200 -c 300 -d 400
发现 -a 选项
发现 -b 选项
-b 选项的参数值是:200
发现 -c 选项
-c 选项的参数值是:300
-d 是一个未知选项
400 是一个未知选项
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32