function test() { if [ $1 -le $2 ]; then echo "$1 is less than or equal to $2" else echo "$1 is greater than $2" fi }
test 1 2 test 3 1
重定向
在 linux 中 每个进程都有三个打开的默认描述符
0 表示标准输入
1 表示标准输出
2 表示标准错误输出
将一个 sh 结果输出到文件中
以追加的方式输出到文件中
在 test.sh 中写入
1 2 3 4 5
#!/bin/bash
echo "is a shell"
然后命令行执行
1 2 3 4 5 6 7 8 9 10
➜ Desktop bash test.sh > file.txt ➜ Desktop cat file.txt is a shell ➜ Desktop bash test.sh >> file.txt ➜ Desktop bash test.sh >> file.txt ➜ Desktop cat file.txt is a shell is a shell is a shell ➜ Desktop
写一个异常
1 2 3 4 5 6
#!/bin/bash
echo "is a shell" a=1 b=0 ((a/b))
然后输出结果
1 2 3 4 5 6 7 8 9
➜ Desktop bash test.sh is a shell test.sh: line 6: ((: a/b: division by 0 (error token is "b") ➜ Desktop bash test.sh 2>> error.log is a shell ➜ Desktop cat error.log test.sh: line 6: ((: a/b: division by 0 (error token is "b") ➜ Desktop
把错误的信息输出到标准输出里面
1 2 3 4 5 6
➜ Desktop bash test.sh > file.txt 2>&1 ➜ Desktop cat file.txt is a shell test.sh: line 6: ((: a/b: division by 0 (error token is "b") ➜ Desktop