加载中...

Bash


其中 Y=bash

源代码下载: LearnBash-cn.sh

Bash 是一个为 GNU 计划编写的 Unix shell,是 Linux 和 Mac OS X 下的默认 shell。 以下大多数例子可以作为脚本的一部分运行,也可直接在 shell 下交互执行。

更多信息

  1. #!/bin/bash
  2. # 脚本的第一行叫 shebang,用来告知系统如何执行该脚本:
  3. # 参见: http://en.wikipedia.org/wiki/Shebang_(Unix)
  4. # 如你所见,注释以 # 开头,shebang 也是注释。
  5. # 显示 “Hello world!”
  6. echo Hello world!
  7. # 每一句指令以换行或分号隔开:
  8. echo 'This is the first line'; echo 'This is the second line'
  9. # 声明一个变量:
  10. Variable="Some string"
  11. # 下面是错误的做法:
  12. Variable = "Some string"
  13. # Bash 会把 Variable 当做一个指令,由于找不到该指令,因此这里会报错。
  14. # 也不可以这样:
  15. Variable= 'Some string'
  16. # Bash 会认为 'Some string' 是一条指令,由于找不到该指令,这里再次报错。
  17. # (这个例子中 'Variable=' 这部分会被当作仅对 'Some string' 起作用的赋值。)
  18. # 使用变量:
  19. echo $Variable
  20. echo "$Variable"
  21. echo '$Variable'
  22. # 当你赋值 (assign) 、导出 (export),或者以其他方式使用变量时,变量名前不加 $。
  23. # 如果要使用变量的值, 则要加 $。
  24. # 注意: ' (单引号) 不会展开变量(即会屏蔽掉变量)。
  25. # 在变量内部进行字符串代换
  26. echo ${Variable/Some/A}
  27. # 会把 Variable 中首次出现的 "some" 替换成 “A”。
  28. # 变量的截取
  29. Length=7
  30. echo ${Variable:0:Length}
  31. # 这样会仅返回变量值的前7个字符
  32. # 变量的默认值
  33. echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
  34. # 对 null (Foo=) 和空串 (Foo="") 起作用; 零(Foo=0)时返回0
  35. # 注意这仅返回默认值而不是改变变量的值
  36. # 内置变量:
  37. # 下面的内置变量很有用
  38. echo "Last program return value: $?"
  39. echo "Script's PID: $$"
  40. echo "Number of arguments: $#"
  41. echo "Scripts arguments: $@"
  42. echo "Scripts arguments separated in different variables: $1 $2..."
  43. # 读取输入:
  44. echo "What's your name?"
  45. read Name # 这里不需要声明新变量
  46. echo Hello, $Name!
  47. # 通常的 if 结构看起来像这样:
  48. # 'man test' 可查看更多的信息
  49. if [ $Name -ne $USER ]
  50. then
  51. echo "Your name isn't your username"
  52. else
  53. echo "Your name is your username"
  54. fi
  55. # 根据上一个指令执行结果决定是否执行下一个指令
  56. echo "Always executed" || echo "Only executed if first command fails"
  57. echo "Always executed" && echo "Only executed if first command does NOT fail"
  58. # 在 if 语句中使用 && 和 || 需要多对方括号
  59. if [ $Name == "Steve" ] && [ $Age -eq 15 ]
  60. then
  61. echo "This will run if $Name is Steve AND $Age is 15."
  62. fi
  63. if [ $Name == "Daniya" ] || [ $Name == "Zach" ]
  64. then
  65. echo "This will run if $Name is Daniya OR Zach."
  66. fi
  67. # 表达式的格式如下:
  68. echo $(( 10 + 5 ))
  69. # 与其他编程语言不同的是,bash 运行时依赖上下文。比如,使用 ls 时,列出当前目录。
  70. ls
  71. # 指令可以带有选项:
  72. ls -l # 列出文件和目录的详细信息
  73. # 前一个指令的输出可以当作后一个指令的输入。grep 用来匹配字符串。
  74. # 用下面的指令列出当前目录下所有的 txt 文件:
  75. ls -l | grep "\.txt"
  76. # 重定向输入和输出(标准输入,标准输出,标准错误)。
  77. # 以 ^EOF$ 作为结束标记从标准输入读取数据并覆盖 hello.py :
  78. cat > hello.py << EOF
  79. #!/usr/bin/env python
  80. from __future__ import print_function
  81. import sys
  82. print("#stdout", file=sys.stdout)
  83. print("#stderr", file=sys.stderr)
  84. for line in sys.stdin:
  85. print(line, file=sys.stdout)
  86. EOF
  87. # 重定向可以到输出,输入和错误输出。
  88. python hello.py < "input.in"
  89. python hello.py > "output.out"
  90. python hello.py 2> "error.err"
  91. python hello.py > "output-and-error.log" 2>&1
  92. python hello.py > /dev/null 2>&1
  93. # > 会覆盖已存在的文件, >> 会以累加的方式输出文件中。
  94. python hello.py >> "output.out" 2>> "error.err"
  95. # 覆盖 output.out , 追加 error.err 并统计行数
  96. info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
  97. wc -l output.out error.err
  98. # 运行指令并打印文件描述符 (比如 /dev/fd/123)
  99. # 具体可查看: man fd
  100. echo <(echo "#helloworld")
  101. # 以 "#helloworld" 覆盖 output.out:
  102. cat > output.out <(echo "#helloworld")
  103. echo "#helloworld" > output.out
  104. echo "#helloworld" | cat > output.out
  105. echo "#helloworld" | tee output.out >/dev/null
  106. # 清理临时文件并显示详情(增加 '-i' 选项启用交互模式)
  107. rm -v output.out error.err output-and-error.log
  108. # 一个指令可用 $( ) 嵌套在另一个指令内部:
  109. # 以下的指令会打印当前目录下的目录和文件总数
  110. echo "There are $(ls | wc -l) items here."
  111. # 反引号 `` 起相同作用,但不允许嵌套
  112. # 优先使用 $( ).
  113. echo "There are `ls | wc -l` items here."
  114. # Bash 的 case 语句与 Java 和 C++ 中的 switch 语句类似:
  115. case "$Variable" in
  116. # 列出需要匹配的字符串
  117. 0) echo "There is a zero.";;
  118. 1) echo "There is a one.";;
  119. *) echo "It is not null.";;
  120. esac
  121. # 循环遍历给定的参数序列:
  122. # 变量$Variable 的值会被打印 3 次。
  123. for Variable in {1..3}
  124. do
  125. echo "$Variable"
  126. done
  127. # 或传统的 “for循环” :
  128. for ((a=1; a <= 3; a++))
  129. do
  130. echo $a
  131. done
  132. # 也可以用于文件
  133. # 用 cat 输出 file1 和 file2 内容
  134. for Variable in file1 file2
  135. do
  136. cat "$Variable"
  137. done
  138. # 或作用于其他命令的输出
  139. # 对 ls 输出的文件执行 cat 指令。
  140. for Output in $(ls)
  141. do
  142. cat "$Output"
  143. done
  144. # while 循环:
  145. while [ true ]
  146. do
  147. echo "loop body here..."
  148. break
  149. done
  150. # 你也可以使用函数
  151. # 定义函数:
  152. function foo ()
  153. {
  154. echo "Arguments work just like script arguments: $@"
  155. echo "And: $1 $2..."
  156. echo "This is a function"
  157. return 0
  158. }
  159. # 更简单的方法
  160. bar ()
  161. {
  162. echo "Another way to declare functions!"
  163. return 0
  164. }
  165. # 调用函数
  166. foo "My name is" $Name
  167. # 有很多有用的指令需要学习:
  168. # 打印 file.txt 的最后 10 行
  169. tail -n 10 file.txt
  170. # 打印 file.txt 的前 10 行
  171. head -n 10 file.txt
  172. # 将 file.txt 按行排序
  173. sort file.txt
  174. # 报告或忽略重复的行,用选项 -d 打印重复的行
  175. uniq -d file.txt
  176. # 打印每行中 ',' 之前内容
  177. cut -d ',' -f 1 file.txt
  178. # 将 file.txt 文件所有 'okay' 替换为 'great', (兼容正则表达式)
  179. sed -i 's/okay/great/g' file.txt
  180. # 将 file.txt 中匹配正则的行打印到标准输出
  181. # 这里打印以 "foo" 开头, "bar" 结尾的行
  182. grep "^foo.*bar$" file.txt
  183. # 使用选项 "-c" 统计行数
  184. grep -c "^foo.*bar$" file.txt
  185. # 如果只是要按字面形式搜索字符串而不是按正则表达式,使用 fgrep (或 grep -F)
  186. fgrep "^foo.*bar$" file.txt
  187. # 以 bash 内建的 'help' 指令阅读 Bash 自带文档:
  188. help
  189. help help
  190. help for
  191. help return
  192. help source
  193. help .
  194. # 用 mam 指令阅读相关的 Bash 手册
  195. apropos bash
  196. man 1 bash
  197. man bash
  198. # 用 info 指令查阅命令的 info 文档 (info 中按 ? 显示帮助信息)
  199. apropos info | grep '^info.*('
  200. man info
  201. info info
  202. info 5 info
  203. # 阅读 Bash 的 info 文档:
  204. info bash
  205. info bash 'Bash Features'
  206. info bash 6
  207. info --apropos bash

还没有评论.