我们创建了一个名为“go-geek”的小程序,它会随机返回零或一的退出代码。下一个脚本调用go-geek. 它使用$?shell 变量获取退出代码,并将其用作case语句的表达式。真实世界的脚本会根据生成退出代码的命令的成功或失败进行适当的处理。#!/bin/bashgo-geekcase $? in "0") echo "Response was: Success"...
shell 变量获取退出代码,并将其用作case语句的表达式。 真实世界的脚本会根据生成退出代码的命令的成功或失败进行适当的处理。 #!/bin/bash go-geek case $? in "0") echo "Response was: Success" echo "Do appropriate processing in here" ;; "1") echo "Response was: Error" echo "Do appropriate e...
Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi 二、if语...
Shell case esac 语句case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构。case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令。case 语句格式如下:case 值in 模式1) command1 command2 command3 ;; 模式2) command1 command2 command3 ;; *) command1 command2 ...
bash脚本:case语句 case语句结构: case SWITCH in value1) statement ... ;; value2) statement ... ;; *) statement ... ;; esac 联系: 1.接受从键盘上输入的字符,并作出判断是数字、大小写字母或特殊字符。 #!/bin/bash case $1 in [0-9])...
We can use acasestatement to print a different message for each color using theechocommand: $ cat case_script.sh #!/usr/bin/env bash color="green" case $color in red) echo "The color is red." ;; green) echo "The color is green." ;; blue) echo "The color is blue." ;; yell...
statement ... ;; esac #!/bin/bash # DEBUG=0 case $1 in -v|--verbose) DEBUG=1 ;; *) echo "Unknown options." exit 7 ;; esac [ $DEBUG -eq 1 ] && echo "hello." 只接受参数start,stop,restart,status其中之一 #!/bin/bash ...
Shell case条件语句的应用实践 语法格式 case "变量" in 值1) 指令1 ;; 值2) 指令2 ;; *) 指令3.../bin/bash read -p "请输入[1-3]数字:" a case "$a" in 1) echo "1" ;; 2) echo "2"...echo "niginx关闭成功" else echo "nginx关闭失败" fi echo "nginx关闭成功" fi } case ...
statement* ... ;; esac e.g. #!/bin/bash # case $1 in 'start') echo "Start server...";; 'stop') echo "Stopping server...";; 'status') echo "Running...";; 'restart') echo "Restarting server...";; *) echo "`basename $0` {start|stop|restart:status}";; esac script 1...
Shell case条件语句的应用实践 语法格式 case "变量" in 值1) 指令1 ;; 值2) 指令2 ;; *) 指令3.../bin/bash read -p "请输入[1-3]数字:" a case "$a" in 1) echo "1" ;; 2) echo "2"...echo "niginx关闭成功" else echo "nginx关闭失败" fi echo "nginx关闭成功" fi } case ...