/bin/bash # Calculate the week number using the date command: WEEKOFFSET=$[ $(date +"%V") % 2 ] # Test if we have a remainder. If not, this is an even week so send a message. # Else, do nothing. if [ $WEEKOFFSET -eq "0" ]; then echo "Sunday evening, put out the gar...
if [ -f $file ] then echo "File exists" else echo "File does not exist" fi # Check if a command is successful if ls then echo "Command succeeded" else echo "Command failed" fi ``` 通过使用if-else语句,用户可以根据不同的条件执行不同的操作。这使得Bash脚本更加灵活和强大,可以满足各种不...
Here is a example of while loop controlled by standard input. Until the redirection chain from STDOUT to STDIN to the read command exists the while loop continues. #!/bin/bash# This bash script will locate and replace spaces # in the filenames DIR="." # Controlling a loop with bash re...
while read -r line; do COMMAND; done < input.file 通过-r 选项传递给 read 命令以防止阻止解释其中的反斜杠转义符。 在read 命令之前添加 IFS= 选项,来防止首尾的空白字符被去掉。 while IFS= read -r line; do COMMAND_on $line; done < input.file 这是更适合人类阅读的语法: #!/bin/bash input...
== 等于,如:if [ "a"=="b" ],与=等价 注意:==的功能在[[]]和[]中的行为是不同的,如下: [[ a == z* ]] # 如果a以"z"开头(模式匹配)那么将为true [[ a == "z*" ]] # 如果a等于z*(字符匹配),那么结果为true [ a == z* ] # File globbing 和word splitting将会发生 [ "a" ...
alias alias='any command' Used to define an alias. Common Utilities and Switches This cheat sheet will show you the most useful commands and switches to help you in your network and system administration. Commands Explanation ls -l List files by type and permission. ls -a List all files, ...
grep、awk、sed:grep, awk and sed – three VERY useful command-line utilities。 Greg's Wiki@ wooledge,有深度,是理解 shell 编程的绝佳资料; 数据和安全专家Adam Katz在How to get the first line of a file in a bash script?文章中的回答尽显对 grep、awk、sed 的娴熟掌握。
bash will use whatever regex engine is installed on the user's system. Stick to POSIX regex features if aiming for compatibility.CAVEAT: This example only prints the first matching group. When using multiple capture groups some modification is needed....
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi TEST-COMMAND执行后且它的返回状态是0,那么CONSEQUENT-COMMANDS就执行。返回状态是最后一个命令的退出状态,或者当没有条件是真的话为0。 TEST-COMMAND经常包括数字和字符串的比较测试,但是也可以是任何在成功时返回状态0或者失败时返回一些其他状态的一些命令。一元表达式...
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi TEST-COMMAND TEST-COMMANDS 执行后且它的返回状态是0,那么 CONSEQUENT-COMMANDS 就执行。返回状态是最后一个命令的退出状态,或者当没有条件是真的话为0。 TEST-COMMAND 经常包括数字和字符串的比较测试,但是也可以是任何在成功时返回状态0或者失败时返回一些其他状态...