#Bash Scripts

##intro

###run

You can run the script in the following ways:

./hello_world.sh

bash hello_world.sh.

###she-bang

First line of shell Scripts usually is the she-bang:

TEXT
#!/bin/sh

Means using /bin/sh to run the script.

但是/bin/sh通常是符号链接,在不同机器上实际上是不同的shell,有时会有以外情况。所以建议写成

TEXT
#!/bin/bash

###注释

以 # 开头的行就是注释,会被解释器忽略。

多行注释可以使用以下格式:

bash
:<<EOF
注释内容...
注释内容...
注释内容...
EOF

以上例子中,: 是一个空命令,用于执行后面的 Here 文档, «‘EOF’ 表示开启 Here 文档,COMMENT 是 Here 文档的标识符,在这两个标识符之间的内容都会被视为注释,不会被执行。

EOF 也可以使用其他符号:

bash

: <<'COMMENT'
这是注释的部分。
可以有多行内容。
COMMENT

:<< '
注释内容...
注释内容...
注释内容...
'

:<<!
注释内容...
注释内容...
注释内容...
!

##variables

###define

We can define a variable by using the syntax variable_name=value. To get the value of the variable, add $ before the variable.

bash
#!/bin/bash
# A simple variable example
greeting=Hello
name=Tux
echo $greeting $name

注意,变量名和等号之间不能有空格

字符串:

bash
my_string='Hello, World!my_string="Hello, World!"
TEXT
RUNOOB="www.runoob.com"
LD_LIBRARY_PATH="/bin/"
_var="123"
var2="abc"
bash
for file in \` ls / etc \`
for file in $ (ls / etc)

数组:

TEXT
my_array=(1 2 3 4 5)

只读变量

bash
#!/bin/bash

myUrl="https://www.google.com"
readonly myUrl
myUrl="https://www.runoob.com"

使用 unset 命令可以删除变量。语法:

TEXT
unset variable_name

###变量类型

字符串变量: 在 Shell中,变量通常被视为字符串。

你可以使用单引号 ’ 或双引号 " 来定义字符串,例如:

TEXT
my_string='Hello, World!'
my_string="Hello, World!"

单引号字符串的限制:

双引号的优点:

拼接字符串

bash
your\_name = "runoob"
\# 使用双引号拼接
greeting = "hello, " $your\_name "!"
greeting\_1 = "hello, ${your\_name}!"
echo $greeting $greeting\_1

\# 使用单引号拼接
greeting\_2 = 'hello, ' $your\_name '!'
greeting\_3 = 'hello, ${your\_name}!'
echo $greeting\_2 $greeting\_3

输出结果为:

TEXT
hello, runoob ! hello, runoob !
hello, runoob ! hello, ${your_name} !

获取字符串长度

bash
string = "abcd"
echo ${#string} \# 输出 4

###数组

bash支持一维数组(不支持多维数组),并且没有限定数组的大小。

类似于 C 语言,数组元素的下标由 0 开始编号。获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于或等于 0。

在 Shell 中,用括号来表示数组,数组元素用"空格"符号分割开。定义数组的一般形式为:

TEXT
数组名=(值1 值2 ... 值n)

例如:

TEXT
array_name=(value0 value1 value2 value3)

或者

TEXT
array_name=(
value0
value1
value2
value3
)

还可以单独定义数组的各个分量:

TEXT
array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen

可以不使用连续的下标,而且下标的范围没有限制。

读取数组元素值的一般格式是:

TEXT
${数组名[下标]}

例如:

TEXT
valuen=${array_name[n]}

使用 @ 符号可以获取数组中的所有元素,例如:

TEXT
echo ${array_name[@]}

获取数组长度的方法与获取字符串长度的方法相同,例如:

bash
\# 取得数组元素的个数
length = ${#array\_name\[@\]}
\# 或者
length = ${#array\_name\[\*\]}
\# 取得数组单个元素的长度
length = ${#array\_name\[n\]}

###运算

Arithmetic Expressions:

Below are the operators supported by bash for mathematical calculations:

Operator Usage
+ addition
- subtraction
* multiplication
/ division
** exponentiation
% modulus

Numeric Comparison logical operators

Comparison is used to check if statements evaluate to true or false. We can use the below shown operators to compare two statements:

Operation Syntax Explanation
Equality num1 -eq num2 is num1 equal to num2
Greater than equal to num1 -ge num2 is num1 greater than equal to num2
Greater than num1 -gt num2 is num1 greater than num2
Less than equal to num1 -le num2 is num1 less than equal to num2
Less than num1 -lt num2 is num1 less than num2
Not Equal to num1 -ne num2 is num1 not equal to num2

###参数

名称 含义
$# 传给脚本的参数个数
$0 脚本本身的名字
$1 传递给该shell脚本的第一个参数
$2 传递给该shell脚本的第二个参数
$@ 传给脚本的所有参数的列表
$* 以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 脚本运行的当前进程ID号
$? 显示最后命令的退出状态,0表示没有错误,其他表示有错误

假设执行 ./test.sh a b c 这样一个命令,则可以使用下面的参数来获取一些值:

当执行系统自身的命令时, $? 对应这个命令的返回值。
当执行 shell 脚本时, $? 对应该脚本调用 exit 命令返回的值。如果没有主动调用 exit 命令,默认返回为 0。
当执行自定义的 bash 函数时, $? 对应该函数调用 return 命令返回的值。如果没有主动调用 return 命令,默认返回为 0。

下面举例说明 "$*""$@" 的差异。假设有一个 testparams.sh 脚本,内容如下:

bash
#!/bin/bash

for arg in "$*"; do
    echo "****:" $arg
done
echo --------------
for arg in "$@"; do
    echo "@@@@:" $arg
done

这个脚本分别遍历 "$*""$@" 扩展后的内容,并打印出来。

执行 ./testparams.sh 脚本,结果如下:

bash
$ ./testparams.sh This is a test
****: This is a test
--------------
@@@@: This
@@@@: is
@@@@: a
@@@@: test

可以看到, "$*" 只产生一个字符串,for 循环只遍历一次。
"$@" 产生了多个字符串,for 循环遍历多次,是一个字符串参数数组。

注意 :如果传入的参数多于 9 个,则不能使用 $10 来引用第 10 个参数,而是要用 ${10} 来引用。

即,需要用大括号 {} 把大于 9 的数字括起来。
例如, ${10} 表示获取第 10 个参数的值,写为 $10 获取不到第 10 个参数的值。
实际上, $10 相当于 ${1}0 ,也就是先获取 $1 的值,后面再跟上 0。
如果 $1 的值是 “first”,则 $10 的值是 “first0”。

在 bash 中,可以使用 $# 来获取传入的命令行或者传入函数的参数个数。
要注意的是, $# 统计的参数个数不包括脚本自身名称或者函数名称。
例如,执行 ./a.sh a b ,则 $# 是 2,而不是 3。

##pairs

{},[],(),``

###{} 花括号

####常规用法

大括号拓展。(通配(globbing))将对大括号中的文件名做扩展。在大括号中,不允许有空白,除非这个空白被引用或转义。第一种:对大括号中的以逗号分割的文件列表进行拓展。如 touch {a,b}.txt 结果为a.txt b.txt。第二种:对大括号中以点点(..)分割的顺序文件列表起拓展作用,如:touch {a..d}.txt 结果为a.txt b.txt c.txt d.txt

TEXT
# ls {ex1,ex2}.sh
ex1.sh  ex2.sh
# ls {ex{1..3},ex4}.sh
ex1.sh  ex2.sh  ex3.sh  ex4.sh
# ls {ex[1-3],ex4}.sh
ex1.sh  ex2.sh  ex3.sh  ex4.sh

代码块,又被称为内部组,这个结构事实上创建了一个匿名函数 。与小括号中的命令不同,大括号内的命令不会新开一个子shell运行,即脚本余下部分仍可使用括号内变量。括号内的命令间用分号隔开,最后一个也必须有分号。{}的第一个命令和左括号之间必须要有一个空格。

####几种特殊的替换结构

TEXT
${var:-string}
${var:+string}
${var:=string}
${var:?string}
  1. ${var:-string}${var:=string}:若变量var为空,则用在命令行中用string来替换${var:-string},否则变量var不为空时,则用变量var的值来替换${var:-string};对于${var:=string}的替换规则和${var:-string}是一样的,所不同之处是${var:=string}若var为空时,用string替换${var:=string}的同时,把string赋给变量var: ${var:=string}很常用的一种用法是,判断某个变量是否赋值,没有的话则给它赋上一个默认值。

  2. ${var:+string}的替换规则和上面的相反,即只有当var不是空的时候才替换成string,若var为空时则不替换或者说是替换成变量 var的值,即空值。(因为变量var此时为空,所以这两种说法是等价的)

  3. ${var:?string}替换规则为:若变量var不为空,则用变量var的值来替换${var:?string};若变量var为空,则把string输出到标准错误中,并从脚本中退出。我们可利用此特性来检查是否设置了变量的值。

在上面这五种替换结构中string不一定是常值的,可用另外一个变量的值或是一种命令的输出。

TEXT
default_name="Guest"
unset name  # 确保 name 未定义

# 如果 name 为空,则使用 default_name 的值
echo "Hello, ${name:-$default_name}"  # 输出: Hello, Guest

四种模式匹配替换结构

模式匹配记忆方法:

TEXT
# 是去掉左边(在键盘上#在$之左边)
% 是去掉右边(在键盘上%在$之右边)

#%中的单一符号是最小匹配,两个相同符号是最大匹配。

TEXT
${var%pattern}
${var%%pattern
${var#pattern}
${var##pattern}
TEXT
# var=testcase
# echo $var
testcase
# echo ${var%s*e}
testca
# echo $var
testcase
# echo ${var%%s*e}
te
# echo ${var#?e}
stcase
# echo ${var##?e}
stcase
# echo ${var##*e}

# echo ${var##*s}
e
# echo ${var##test}
case

####字符串提取和替换

TEXT
${var:num}
${var:num1:num2}
${var/pattern/pattern}
${var//pattern/pattern}
TEXT
[root@centos ~]# var=/home/centos
[root@centos ~]# echo $var
/home/centos
[root@centos ~]# echo ${var:5}
/centos
[root@centos ~]# echo ${var: -6}
centos
[root@centos ~]# echo ${var:(-6)}
centos
[root@centos ~]# echo ${var:1:4}
home
[root@centos ~]# echo ${var/o/h}
/hhme/centos
[root@centos ~]# echo ${var//o/h}
/hhme/cenths

###[]

bash
type [

[ is a shell builtin

This means that ‘[’ is actually a program, just like ls and other programs, so it must be surrounded by spaces.

###[[]]

bash
if ($i<5)
if [ $i -lt 5 ]
if [ $a -ne 1 -a $a != 2 ]
if [ $a -ne 1] && [ $a != 2 ]
if [[ $a != 1 && $a != 2 ]]

for i in $(seq 0 4);do echo $i;done
for i in \`seq 0 4\`;do echo $i;done
for ((i=0;i<5;i++));do echo $i;done
for i in {0..4};do echo $i;done

###[][[]]

Bash 脚本中,[ ]test 命令)和 [[ ]](关键字)都用于条件判断,但它们在功能、性能和安全性上有显著区别。以下是详细对比:

####基本定义

语法 类型 说明
[ ] 内置命令 等价于 test 命令,需严格遵循参数规则(如空格和引号)。
[[ ]] Shell 关键字 Bash/ksh/zsh 的扩展语法,更灵活,支持额外功能(如模式匹配、逻辑组合)。

####核心区别

(1) 字符串比较

(2) 数值比较

(3) 逻辑运算符

(4) 文件测试

(5) 正则匹配(仅 [[ ]]

bash
[[ "hello" =~ ^h ]]   # 正则匹配(返回 true)

####性能与安全性

特性 [ ] [[ ]]
执行速度 较慢(外部命令或内置命令) 更快(Bash 关键字)
安全性 变量未加引号易报错 自动处理空格,更健壮
兼容性 所有 Shell(sh、dash 等) 仅 Bash/ksh/zsh

####何时使用?

####经典示例

(1) 字符串比较

bash
# [ ] 必须加引号
[ "$name" = "Alice" ] && echo "Hello Alice"

# [[ ]] 更灵活
[[ $name == A* ]] && echo "Name starts with A"

(2) 文件检查

bash
# [ ] 和 [[ ]] 均可,但后者更安全
[ -f "/path/$file" ] && echo "File exists"
[[ -f /path/$file ]] && echo "File exists"

(3) 逻辑组合

bash
# [ ] 用 -a/-o
[ "$age" -gt 18 -a "$age" -lt 60 ] && echo "Valid age"

# [[ ]] 用 &&/||
[[ $age -gt 18 && $age -lt 60 ]] && echo "Valid age"

####总结

需求 [ ] [[ ]]
字符串比较 需严格加引号 自动处理空格,支持通配符
数值比较 -eq-lt [ ],但可用 (( )) 替代
逻辑运算符 -a-o &&||
正则匹配 ✅(=~
性能 较慢 更快

###()

###(())

bash
var=$((expression))

###`` back ticks

var= commands

Example: Suppose we want to get the output of a list of mountpoints with tmpfs in their name. We can craft a statement like this: df -h | grep tmpfs.

To include it in the bash script, we can enclose it in back ticks.

bash
#!/bin/bash

var=\`df -h | grep tmpfs\`
echo $var

###""

bash
repo=~"/projects/repo1"  # 或直接写 ~/projects/repo1(不加引号)
cd "$repo"               # 此时 $repo 已是展开后的绝对路径
bash
#!/bin/bash

# 显式展开家目录
HOME_DIR=$(eval echo "~")  # 或直接写 HOME_DIR="$HOME"

repos=(
    "$HOME_DIR/projects/repo1"
    "$HOME_DIR/work/repo2"
)

###""''

Bash 脚本中,双引号 "" 和单引号 '' 都用于定义字符串,但它们在 变量扩展命令替换特殊字符处理 上有本质区别。以下是详细对比:

特性 双引号 "" 单引号 ''
变量扩展 ✅(如 "$var" 会展开) ❌(如 '$var' 原样输出)
命令替换 ✅(如 "$(date)" 会执行) ❌(如 '$(date)' 原样输出)
转义字符 \, $, `, " 生效 所有字符均按字面处理(无转义)
用途 需保留变量或命令结果时使用 需完全按字面输出时使用
bash
name="Alice"

echo "Hello, $name"   # 输出: Hello, Alice
echo 'Hello, $name'   # 输出: Hello, $name
bash
echo "Today is $(date)"  # 输出: Today is Mon Jul 1 12:00:00 UTC 2024
echo 'Today is $(date)'  # 输出: Today is $(date)
bash
echo "Path is \$HOME"    # 输出: Path is $HOME(\$ 被转义)
echo 'Path is \$HOME'    # 输出: Path is \$HOME(完全字面)
bash
echo "She said: 'Hello!'"   # 输出: She said: 'Hello!'
echo 'He said: "Hi!"'       # 输出: He said: "Hi!"

特殊情况处理

bash
alias rm='rm -i'          # 单引号内定义别名(避免变量提前展开)
echo "It's a sunny day."  # 双引号内嵌套单引号(无需转义)
bash
echo "Line 1\nLine 2"     # 输出: Line 1\nLine 2(\n 未被转义为换行)
echo -e "Line 1\nLine 2"  # 输出两行(-e 启用转义)
echo 'Line 1\nLine 2'     # 输出: Line 1\nLine 2(完全字面)
  1. 默认用双引号
    保护变量中的空格(如 "$file"),避免路径或文件名错误拆分。
    bash
    cp "$file" "/backup/$file"  # 正确处理含空格的文件名
  2. 需要纯文本时用单引号
    如正则表达式、代码生成、避免意外展开。
    bash
    sed -e 's/foo/bar/g' file.txt

##read, write

###read

####read in console

bash
echo "input a"
read a
echo "input b"
read b

read -n 3 -t 5 -s -p "input 3 charater, wait for 5 seconds, silent" c
echo "$c"

printf "a=%s,b=%s,sum is %s" $a $b $((a + b))

It is possible to give arguments to the script on execution.

$@ is a list of the parameters.

bash
#!/bin/bash

for x in $@
do
    echo "Entered arg is $x"
done

Run it like this:

./script arg1 arg2

####read in file

Suppose we have a file sample_file.txt, We can read the file line by line and print the output on the screen.

bash
#!/bin/bash

LINE=1

while read -r CURRENT_LINE
    do
        echo "$LINE: $CURRENT_LINE"
    ((LINE++))
done < "sample_file.txt"

###write

##redirection, pipe

###direction

####输出重定向

符号 作用 示例
> 覆盖写入文件(标准输出) ls > file.txt
>> 追加到文件(标准输出) echo "hi" >> file.txt
2> 覆盖写入文件(标准错误) cmd 2> error.log
2>> 追加到文件(标准错误) cmd 2>> error.log
&> 覆盖写入文件(标准输出+标准错误) cmd &> output.log
&>> 追加到文件(标准输出+标准错误) cmd &>> output.log

文件描述符操作 Bash 用数字n表示文件描述符(File Descriptor, FD):

符号 作用 示例
n> 覆盖写入到文件描述符 n cmd 3> custom_fd.log
n>> 追加到文件描述符 n cmd 3>> custom_fd.log
n>&m 将文件描述符 n 重定向到 m cmd 2>&1(错误合并到输出)
n>&- 关闭文件描述符 n exec 3>&-
&>file>&file 合并标准输出和错误到文件(兼容性写法) cmd &> all.log

示例

bash
# 将标准错误重定向到文件描述符3,再重定向到文件
exec 3>&1  # 备份标准输出到3
cmd 2>&1 >output.log | tee -a error.log  # 错误流单独处理
exec 1>&3  # 恢复标准输出

特殊设备重定向

目标文件 作用 示例
/dev/null 丢弃输出(黑洞设备) cmd > /dev/null 2>&1
/dev/stdout 显式指向标准输出 echo "x" > /dev/stdout
/dev/stderr 显式指向标准错误 echo "error" > /dev/stderr
/dev/fd/n 重定向到文件描述符 n cmd > /dev/fd/3

示例

bash
# 静默执行命令(忽略所有输出)
cmd > /dev/null 2>&1

####输入重定向

符号 作用 示例
< 从文件读取输入(标准输入) sort < data.txt
<< Here Document(多行输入) 见前文
<<< Here String(字符串输入) grep "x" <<< "abc"

<:标准输入重定向

作用:将文件内容作为命令的标准输入。
语法command < file
特点

示例

bash
# 将 file.txt 的内容作为 wc 命令的输入
wc -l < file.txt

结果:统计 file.txt 的行数。

<<:Here Document(文档内嵌输入)

作用:将多行文本(称为 “Here Document”)作为命令的标准输入,直到遇到指定的结束标记。
语法

bash
command << DELIMITER
多行文本...
DELIMITER

特点

示例

bash
# 将多行文本传递给 cat 命令
cat << EOF
第一行
第二行
变量值: $HOME
EOF

结果

TEXT
第一行
第二行
变量值: /home/username

<<<:Here String(字符串内嵌输入) 作用:将单个字符串作为命令的标准输入。
语法command <<< "string"
特点

示例

bash
# 将字符串传递给 grep 命令
grep "hello" <<< "hello world"

结果:输出 hello world(因为匹配成功)。

对比总结

操作符 名称 输入源 典型用途 是否解析变量
< 输入重定向 文件 从文件读取数据
<< Here Document 多行文本(脚本内嵌) 传递多行输入(如生成配置文件) 是(可禁用)
<<< Here String 单个字符串 快速传递变量或简单字符串

关键区别

  1. 输入来源不同

    • < 从文件读取
    • << 从脚本内嵌的多行文本读取
    • <<< 从单个字符串读取
  2. 性能差异

    • <<<echo "str" | command 更高效(避免管道)
    • < 适合处理大文件(逐行读取,不占用内存)
  3. 变量解析

    • <<<<< 默认解析变量,但 << 可通过 <<'DELIMITER' 禁用解析
    • < 由命令决定是否解析(如 cat 不解析,eval 会解析)

使用场景示例 < 的典型用途

bash
# 从配置文件读取输入
while read line; do
    echo "配置行: $line"
done < config.txt

<< 的典型用途

bash
# 生成多行配置文件
cat > app.conf << EOF
server_ip=192.168.1.1
port=8080
EOF

<<< 的典型用途

bash
# 快速检查字符串是否包含子串
grep "error" <<< "$log_content"

注意事项

  1. << 的结束标记

    • 结束标记必须单独一行且顶格书写(不能有缩进):
      bash
      # 错误示例(缩进会导致语法错误)
      cat << EOF
      内容...
        EOF  # 缩进了,无法识别为结束标记
  2. <<< 的字符串引用

    • 如果字符串包含空格或特殊字符,需用引号包围:
      bash
      # 安全写法
      wc -w <<< "hello world"
  3. 性能选择

    • 处理大量数据时优先用 <(文件)或 <<(多行文本)
    • 简单字符串操作用 <<< 更高效

####重定向组合

(1) 同时重定向输入和输出

bash
# 从input.txt读取,结果写入output.txt
command < input.txt > output.txt

(2) 分离标准输出和错误

bash
# 标准输出到out.log,错误到err.log
command > out.log 2> err.log

(3) 合并输出和错误到同一文件

bash
# 方式1(推荐)
command &> combined.log

# 方式2(传统写法)
command > combined.log 2>&1

####进程替换(Process Substitution)

>(...)<(...) 将命令输出/输入视为临时文件:

符号 作用 示例
<(cmd) 将命令输出作为文件输入 diff <(ls dir1) <(ls dir2)
>(cmd) 将命令输入作为文件输出 tee >(gzip > out.gz)

示例

bash
# 比较两个目录的文件列表差异
diff <(ls /path/to/dir1) <(ls /path/to/dir2)

# 将输出同时写入文件和压缩
echo "data" | tee >(gzip > output.gz) > original.txt

####综合示例

** 日志记录脚本**

bash
#!/bin/bash
exec 3>&1  # 备份标准输出到描述符3
exec > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)  # 分离输出和错误

echo "正常消息"  # 写入stdout.log
ls /nonexistent  # 错误写入stderr.log

exec 1>&3  # 恢复标准输出
echo "回到终端显示"

复杂重定向

bash
{
    echo "标准输出1"
    echo "标准错误1" >&2
    echo "标准输出2"
    echo "标准错误2" >&2
} > >(grep "标准输出" > out.txt) 2> >(grep "标准错误" > err.txt)

注意事项

  1. 顺序敏感
    2>&1 >file>file 2>&1 完全不同!后者才是合并输出到文件。

    bash
    # 错误:错误流不会进入文件
    cmd 2>&1 > file
    
    # 正确:合并输出和错误到文件
    cmd > file 2>&1
  2. 文件描述符范围
    Bash 默认支持 0-9,更高数值需用 exec 显式分配。

  3. 管道与重定向优先级
    管道 | 优先级高于重定向,必要时用 { } 分组:

    bash
    { cmd1 | cmd2; } > output.txt
  4. Here Document 的变体

    • <<-:忽略结束标记前的制表符(Tab)
      bash
      cat <<- EOF
          Indented text
      EOF  # 可以用Tab缩进

###pipe

##control

###if

bash
if [ ... ]
then
  # if-code
else
  # else-code
fi

or

bash
if [ ... ] ; then
  # if-code
else
  # else-code
fi

conditions:

examples:

bash
#!/bin/sh
if [ "$X" -lt "0" ]
then
  echo "X is less than zero"
fi
if [ "$X" -gt "0" ]; then
  echo "X is more than zero"
fi
[ "$X" -le "0" ] && \
      echo "X is less than or equal to  zero"
[ "$X" -ge "0" ] && \
      echo "X is more than or equal to zero"
[ "$X" = "0" ] && \
      echo "X is the string or number \"0\""
[ "$X" = "hello" ] && \
      echo "X matches the string \"hello\""
[ "$X" != "hello" ] && \
      echo "X is not the string \"hello\""
[ -n "$X" ] && \
      echo "X is of nonzero length"
[ -f "$X" ] && \
      echo "X is the path of a real file" || \
      echo "No such file: $X"
[ -x "$X" ] && \
      echo "X is the path of an executable file"
[ "$X" -nt "/etc/passwd" ] && \
      echo "X is a file which is newer than /etc/passwd"


read a
read b
read c

if [ $a == $b -a $b == $c -a $a == $c ] # -a means
then
echo EQUILATERAL

elif [ $a == $b -o $b == $c -o $a == $c ] # -o means or
then
echo ISOSCELES
else
echo SCALENE

fi

###loop

bash

for i in {1..5}
do
    echo $i
done

for X in cyan magenta yellow
do
    echo $X
done
bash
i=1
while [[ $i -le 10 ]] ; do
   echo "$i"
  (( i += 1 ))
done
bash
# 定义数组(空格分隔元素,可含特殊字符)
fruits=("Apple" "Banana" "Orange" "Grape" "Watermelon")
for fruit in "${fruits[@]}"; do
    echo "Fruit: $fruit"
done
for i in "${!fruits[@]}"; do
    echo "Index $i: ${fruits[i]}"
done
bash
# 定义仓库目录列表(支持绝对路径或相对路径)
repos=(
    "/path/to/repo1"
    "/path/to/repo2"
    "/path/to/repo3"
    "/path/to/repo4"
)

# 遍历所有仓库
for i in "${repos[@]}"; do
    echo "Updating repo: $i"
    cd "$i" || { echo "Failed to enter $i"; continue; }

    # 执行 git pull
    git pull origin main  # 假设分支是 main,根据实际情况修改

    # 检查 git pull 是否成功
    if [ $? -eq 0 ]; then
        echo "Successfully updated: $i"
    else
        echo "Failed to update: $i"
    fi

    echo "----------------------------------"
done

echo "All repositories updated!"

##function


fork boom!

TEXT
:(){:|:;};:

##Advance

###get-opts

example:

sh
#!/bin/sh
docs="Usage: \
\n\tbash $0 [options] \
\nOptions: \
\n\t-N NAME: your name, required\
\n\t-a AGE: your age, required\
\n\t-v: verbose mode, optional\
\nExample: \
\n\t bash $0 -N balabala -a 24 "

usage() {
    echo -e $docs >&2
    exit 1
}

if [ $# -eq 0 ] || [ $1 == -h ]; then usage; fi

## 设置变量的默认值
verbose=n

## 检查变量参数是否缺失
check() {
    opt=$1
    arg=$2

    if [[ $arg =~ ^- ]] || [ ! $arg ]
    then
        echo "ERROR: -$opt expects an corresponding argument" >&2
        usage
    fi
}

## 循环解析脚本的所有positional parameters
while getopts :vN:a: opt
do
    case $opt in
    v)
        verbose=y
        ;;
    N)
        check N $OPTARG
        name=$OPTARG
        ;;
    a)
        check a $OPTARG
        age=$OPTARG
        ;;
    :)
        echo "ERROR: -$OPTARG expects an corresponding argument" >&2
        usage
        ;;
    \?) # shell里'?'具有匹配一位任意字符的作用,因此需要转义为普通字符
        echo "ERROR: unkown option -$OPTARG" >&2
        usage
        ;;
    esac
done

## 如果用户没有提供-N选项
if [ ! $name ]
then
    echo ERROR: option -N is required >&2
    usage
fi

## 如果用户没有提供-a选项
if [ ! $age ]
then
    echo ERROR: option -a is required >&2
    usage
fi

if [ $verbose == n ]
then
    echo "Merry Christmas! $name"
else
    echo "Hello, your name is $name, you are $age years old!"
    echo "Merry Christmas!!"
fi

:vN:a:中,第一个: 表示静默错误模式(silent error mode)。如果没有这个:,当遇到无效选项时,getopts 会打印错误消息。
选项后的 :(如 N:)表示该选项需要一个参数。
如果没有 :,则表示该选项不需要参数。

###cron

Cron is a job scheduling utility present in Unix like systems. You can schedule jobs to execute daily, weekly, monthly or in a specific time of the day. Automation in Linux heavily relies on cron jobs.

Below is the syntax to schedule crons:

bash
# Cron job example
* * * * * sh /path/to/script.sh

Here, * represents minute(s) hour(s) day(s) month(s) weekday(s), respectively.

Below are some examples of scheduling cron jobs.

SCHEDULE SCHEDULED VALUE
5 0 * 8 * At 00:05 in August.
5 4 * * 6 At 04:05 on Saturday.
0 22 * * 1-5 At 22:00 on every day-of-week from Monday through Friday.

crontab -l lists the already scheduled scripts for a particular user.