拓展阅读

linux Shell 命令行-00-intro 入门介绍

linux Shell 命令行-02-var 变量

linux Shell 命令行-03-array 数组

linux Shell 命令行-04-operator 操作符

linux Shell 命令行-05-test 验证是否符合条件

linux Shell 命令行-06-flow control 流程控制

linux Shell 命令行-07-func 函数

linux Shell 命令行-08-file include 文件包含

linux Shell 命令行-09-redirect 重定向

基本输出

echo

  [plaintext]
1
echo [-e] 字符串

printf

类似于 C 的 printf() 函数

  [plaintext]
1
printf 格式化字符串 [参数...]

test 测试条件是否成立

数字

  • test_num.sh
  [bash]
1
2
3
4
5
6
7
8
9
10
#!/bin/bash num1=100 num2=100 if test $[num1] -eq $[num2] then echo '两个数相等!' else echo '两个数不相等!' fi

运行

  [bash]
1
2
houbinbindeMacBook-Pro:shell houbinbin$ /bin/sh test_num.sh 两个数相等!

字符串

  • test_str.sh
  [bash]
1
2
3
4
5
6
7
8
9
10
#!/bin/bash num1="runoob" num2="runoob" if test num1=num2 then echo '两个字符串相等!' else echo '两个字符串不相等!' fi

运行

  [bash]
1
2
houbinbindeMacBook-Pro:shell houbinbin$ /bin/sh test_str.sh 两个字符串相等!

文件

  • test_file.sh
  [bash]
1
2
3
4
5
6
7
8
#!/bin/bash if test -e /bin/bash then echo '文件已存在!' else echo '文件不存在!' fi

运行

  [bash]
1
2
houbinbindeMacBook-Pro:shell houbinbin$ /bin/sh test_file.sh 文件已存在!

参考资料

https://www.runoob.com/linux/linux-shell.html