引言 无论你是DevOps/SysOps工程师、开发人员,还是仅仅对Linux感兴趣的人,你都可以使用Bash脚本来组合不同的Linux命令并自动化无聊和重复的日常任务,从而让你专注于更高效和有趣的事情。 在这里你可以找到完成的Bash脚本速查表 Bash脚本头部(Shebang) 选项 1: 选项 2: 变量 用户输入 注释 要在Bash中添加注释,你需要在行首加上#符号。注释永远不会显示在屏幕上。 参数 然后运行文件并传递三个参数: 数组 条件表达式 文件表达式 字符串表达式 算术运算符 条件语句 循环 For循环 While循环 Until循环 函数 示例 声明: 本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。
无论你是DevOps/SysOps工程师、开发人员,还是仅仅对Linux感兴趣的人,你都可以使用Bash脚本来组合不同的Linux命令并自动化无聊和重复的日常任务,从而让你专注于更高效和有趣的事情。
在这里你可以找到完成的Bash脚本速查表
选项 1:
#!/bin/bash
选项 2:
#!/usr/bin/env bash
#!/bin/bash name="DevDojo" echo "Hi there $name"
#!/bin/bash echo "What is your name?" read name echo "Hi there $name" echo "Welcome to DevDojo!"
要在Bash中添加注释,你需要在行首加上#符号。注释永远不会显示在屏幕上。
# This is a comment and will not be rendered on the screen
#!/bin/bash echo "Argument one is $1" echo "Argument two is $2" echo "Argument three is $3"
然后运行文件并传递三个参数:
bash ./arguments.sh dog cat bird
my_array=("value 1" "value 2" "value 3" "value 4") # Access a single element, this would output: value 2 echo ${my_array[1]} # This would return the last element: value 4 echo ${my_array[-1]} # This would output the total number of elements in the array, in our case, it is 4: echo "${#my_array[@]}" # This would output all of the elements of the array: printf '%s\n' "${my_array[@]}"
## True if file exists. [[ -a ${file} ]] ## True if file exists and is a block special file. [[ -b ${file} ]] ## True if file exists and is a character special file. [[ -c ${file} ]] ## True if file exists and is a directory. [[ -d ${file} ]] ## True if file exists. [[ -e ${file} ]] ## True if file exists and is a regular file. [[ -f ${file} ]] ## True if file exists and is a symbolic link. [[ -h ${file} ]] ## True if file exists and is readable. [[ -r ${file} ]] ## True if file exists and has a size greater than zero. [[ -s ${file} ]] ## True if file exists and is writable. [[ -w ${file} ]] ## True if file exists and is executable. [[ -x ${file} ]] ## True if file exists and is a symbolic link. [[ -L ${file} ]]
# True if the shell variable varname is set (has been assigned a value). [[ -v ${varname} ]] # True if the length of the string is zero. [[ -z ${string} ]] # True if the length of the string is non-zero. [[ -n ${string} ]] # True if the strings are equal. = should be used with the test command for POSIX conformance. When used with the [[ command, this performs pattern matching as described above (Compound Commands) [[ ${string1} == ${string2} ]] # True if the strings are not equal. [[ ${string1} != ${string2} ]] # True if string1 sorts before string2 lexicographically. [[ ${string1} < ${string2} ]] # True if string1 sorts after string2 lexicographically. [[ ${string1} > ${string2} ]]
# Returns true if the numbers are equal [[ ${arg1} -eq ${arg2} ]] # Returns true if the numbers are not equal [[ ${arg1} -ne ${arg2} ]] # Returns true if arg1 is less than arg2 [[ ${arg1} -lt ${arg2} ]] # Returns true if arg1 is less than or equal arg2 [[ ${arg1} -le ${arg2} ]] # Returns true if arg1 is greater than arg2 [[ ${arg1} -gt ${arg2} ]] # Returns true if arg1 is greater than or equal arg2 [[ ${arg1} -ge ${arg2} ]] # As with other programming languages you can use AND & OR conditions: [[ test_case_1 ]] && [[ test_case_2 ]] # And [[ test_case_1 ]] || [[ test_case_2 ]] # Or
#!/bin/bash # Bash if statement example read -p "What is your name? " name if [[ -z ${name} ]] then echo "Please enter your name!" else echo "Hi there ${name}" fi
#!/bin/bash users="devdojo, bobby, tony" for user in ${users} do echo "${user}" done
#!/bin/bash counter=1 while [[ $counter -le 10 ]] do echo $counter ((counter++)) done
#!/bin/bash count=1 until [ $count -gt 10 ] do echo $count ((count++)) done
function function_name() { your_commands }
示例
#!/bin/bash function hello(){ echo "Hello World Function!" } hello
声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。