鼎鼎知识库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2021.4.20学习Linux脚本.md 7.7KB

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. # 跑通第一个在`Ubuntu`上的`Shell Script`
  2. ## 什么是Shell Script?
  3. 通常以`.sh`结尾,该文件中包含了`UNIX`的命令。在`Ubuntu`中,默认的`Shell`是`Bash`。
  4. 可以在文件的开头注明是否以`shell script`运行。
  5. ```
  6. #!/bin/bash 头一行也被称为shebang, 用来告诉Ubuntu kernel使用哪个解释器,这里表示使用Bash
  7. #!/bin/sh 表示使用Dash解释器
  8. ```
  9. 什么是`Bash`?
  10. 默认的命令行解释器。
  11. ```
  12. 查看Bash: echo $SHELL
  13. ```
  14. 什么是`Shell`?
  15. 用来执行命令的宏处理器`macro processor`。当打`Linux`控制台,默认就包含`Shell`。输入一些简单的命令,`Shell`默认借助`Bash`解释器,解释并执行命令。
  16. ```
  17. date
  18. cal
  19. pwd
  20. ls
  21. ```
  22. 什么是`Script`或`Scripting`?
  23. 其实可以理解成批处理命令。用来执行多个命令。
  24. ## 第一个批处理命令脚本
  25. ```
  26. 创建批处理文件:nano task.sh
  27. #!/bin/bash
  28. date
  29. cal
  30. pwd
  31. ls
  32. 给予该文件权限:sudo chmod +x task.sh
  33. 执行批处理:./task.sh
  34. ```
  35. # 文件名和权限
  36. ```
  37. 查看文件类型:file task.sh
  38. 复制脚本文件:cp task.sh 0_xyz
  39. 查看复制文件:file 0_xyz
  40. 查看复制文件内容:nano 0_xyz
  41. ```
  42. # 脚本执行方式
  43. ```
  44. 创建脚本文件:echo date > date.sh
  45. 打印脚本文件内容:cat date.sh
  46. 尝试执行脚本文件:./date.sh(执行不成功)
  47. 尝试另外一种执行脚本文件的方式:bash date.sh(执行成功)
  48. 编辑脚本文件:nano date.sh
  49. #!/bin/bash
  50. date
  51. 更改脚本文件的权限:chmod +x date.sh
  52. 使用第一种方式执行脚本文件:./date.sh
  53. ```
  54. # 相对位置和绝对位置
  55. ```
  56. 来到根目录:cd /
  57. 查看根目录:pwd
  58. 进入home目录:cd home
  59. 返回上一级: cd ..
  60. 查看根目录内容:ls
  61. 目录结构为:
  62. home/lenovo
  63. etc
  64. 进入etc目录:cd etc
  65. 从etc目录进入home/lenovo目录:cd ../home/lenovo
  66. 返回上一个目录:cd -
  67. 再返回上一个目录:cd -
  68. 后退两级返回根目录:cd ../../
  69. 返回linux开机默认目录:cd
  70. ```
  71. # `Hello World`脚本
  72. ```
  73. 创建并打开文件:nano hello-world.sh
  74. #!/bin/bash
  75. echo "Hello World"
  76. 赋予权限:chmod +x hello-world.sh
  77. 执行脚本:bash hello-world.sh
  78. ```
  79. # 查看帮助文件
  80. ```
  81. man ls
  82. q
  83. ls --help
  84. ls -l
  85. ls -l hello-world.sh
  86. ```
  87. # 备份脚本
  88. ```
  89. 创建tmp目录:mkdir tmp
  90. 把home/lenovo目录备份:tar -czf /tmp/myhome_directory.tar.gz /home/lenovo
  91. 列出刚刚创建的备份文件:ls -l /tmp/myhome_directory.tar.gz
  92. 删除备份文件:rm /tmp/myhome_directory.tar.gz
  93. 使用which bash创建批处理文件:which bash > back.sh
  94. 在back.sh文件中输入另外一行内容:echo 'tar -czf /tmp/myhome_directory.tar.gz /home/lenovo/' >> backup.sh
  95. 打开并编辑:nano backup.sh
  96. 赋予权限:chmod +x bakcup.sh
  97. 运行:bash backup.sh
  98. ```
  99. # 变量
  100. ```
  101. 创建脚本文件:nano welcome.sh
  102. #!/bin/bash
  103. greeting="Welcome"
  104. user=$(whoami)
  105. day=$(date +%A)
  106. echo "$greeting back $user! Today is $day, which is the best day of the entire week!"
  107. echo "Your Bash shell version is: $BASH_VERSION. Enjoy!"
  108. 赋予权限:chmod +x welcome.sh
  109. 执行:bash welcome.sh
  110. ```
  111. 备份文件使用变量:
  112. ```
  113. #!/bin/bash
  114. # This bash script is used to backup a user's home directory to /tmp/.
  115. user=$(whoami)
  116. input=/home/$user
  117. output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz
  118. tar -czf $output $input
  119. echo "Backup of $input completed! Details about the output backup file:"
  120. ls -l $output
  121. 运行:bash backup.sh
  122. ```
  123. # 输入、输出、错误导向
  124. ```
  125. 标准错误输出stderr: ls -l foobar
  126. 创建文件:touch foobar
  127. 标准输出stdout:ls -l foobar
  128. 把信息输出到一个文件:ls barfoo > stdout.txt
  129. 把错误信息输出到一个文件:ls barfoo 2> stdout.txt
  130. 把信息和错误信息输出到一个文件:ls barfoo &> stdoutandstderr.txt
  131. 控制台打印文件内容:cat stdout
  132. ```
  133. 使用错误导向忽略错误提示。/dev/null可以看作是一个保存错误信息的容器、
  134. ```
  135. nano backup.sh
  136. #!/bin/bash
  137. # This bash script is used to backup a user's home directory to /tmp/.
  138. user=$(whoami)
  139. input=/home/$user
  140. output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz
  141. tar -czf $output $input 2> /dev/null
  142. echo "Backup of $input completed! Details about the output backup file:"
  143. ls -l $output
  144. 执行脚本:bash backup.sh
  145. ```
  146. 输入文件,从一个文件中获取信息。
  147. ```
  148. 在控制台打开并编辑内容:cat > file1.txt
  149. 按ctrl+d完成编辑
  150. 从file1.txt中读取文件内容:cat < file1.txt
  151. ```
  152. # 功能
  153. ```
  154. 创建脚本:nano function.sh
  155. #!/bin/bash
  156. function user_details {
  157. echo "User Name: $(whoami)"
  158. echo "Home Directory: $HOME"
  159. }
  160. user_details
  161. 赋予权限:chmod +x function.sh
  162. 执行脚本:bash function.sh
  163. ```
  164. 更改备份脚本
  165. ```
  166. #!/bin/bash
  167. # This bash script is used to backup a user's home directory to /tmp/.
  168. user=$(whoami)
  169. input=/home/$user
  170. output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz
  171. # The function total_files reports a total number of files for a given directory.
  172. function total_files {
  173. find $1 -type f | wc -l
  174. }
  175. # The function total_directories reports a total number of directories
  176. # for a given directory.
  177. function total_directories {
  178. find $1 -type d | wc -l
  179. }
  180. tar -czf $output $input 2> /dev/null
  181. echo -n "Files to be included:"
  182. total_files $input
  183. echo -n "Directories to be included:"
  184. total_directories $input
  185. echo "Backup of $input completed!"
  186. echo "Details about the output backup file:"
  187. ls -l $output
  188. ```
  189. # 数字和字符串比较
  190. | 描述 | 数字比较 | 字符串比较 |
  191. | -------- | -------- | ---------- |
  192. | 小于 | -lt | < |
  193. | 大于 | -gt | > |
  194. | 等于 | -eq | = |
  195. | 不等于 | -nq | != |
  196. | 小于等于 | -le | N/A |
  197. | 大于等于 | -ge | N/A |
  198. 比较数字
  199. ```
  200. a=1
  201. b=2
  202. [ $a -lt $b ]
  203. echo $?
  204. ```
  205. 比较字符串
  206. ```
  207. [ "apples" = "oranges" ]
  208. echo $?
  209. ```
  210. 放在脚本中比较
  211. ```
  212. nano comparison.sh
  213. #!/bin/bash
  214. string_a="UNIX"
  215. string_b="GNU"
  216. echo "Are $string_a and $string_b strings equal?"
  217. [ $string_a = $string_b ]
  218. echo $?
  219. num_a=100
  220. num_b=100
  221. echo "Is $num_a equal to $num_b ?"
  222. [ $num_a -eq $num_b ]
  223. echo $?
  224. 赋予权限:chmod +x comparison.sh
  225. 执行脚本:bash comparison.sh
  226. ```
  227. # 条件语句
  228. `if/then/else`语句
  229. ```
  230. 创建脚本:nano if_else.sh
  231. #!/bin/bash
  232. num_a=100
  233. num_b=200
  234. if [ $num_a -lt $num_b ]; then
  235. echo "$num_a is less than $num_b!"
  236. fi
  237. 赋予权限:chmod +x if_else.sh
  238. 执行脚本:bash if_else.sh
  239. 更改脚本:
  240. #!/bin/bash
  241. num_a=400
  242. num_b=200
  243. if [ $num_a -lt $num_b ]; then
  244. echo "$num_a is less than $num_b!"
  245. else
  246. echo "$num_a is greater than $num_b!"
  247. fi
  248. 执行脚本:bash less_than.sh
  249. ```
  250. # 占位符参数
  251. ```
  252. 创建脚本文件:nano param.sh
  253. #!/bin/bash
  254. echo $1 $2 $4
  255. echo $#
  256. echo $*
  257. 赋予权限:chmod +x param.sh
  258. 执行脚本:bash param.sh hello bash scripting world
  259. ```
  260. # 循环
  261. `for`循环
  262. ```
  263. 创建文件:nano items.txt
  264. hello
  265. hi
  266. world
  267. 在控制台打印文件内容:cat items.txt
  268. 创建脚本文件:nano for.sh
  269. #!/bin/bash
  270. for i in $( cat items.txt ); do
  271. echo -n $i | wc -c;
  272. done
  273. 赋予权限:chmod +x for.sh
  274. 执行脚本:bash for.sh
  275. ```
  276. `while`循环
  277. ```
  278. 创建脚本:nano while.sh
  279. #!/bin/bash
  280. counter=0
  281. while [ $counter -lt 3 ]; do
  282. let counter+=1;
  283. echo $counter
  284. done;
  285. 赋予权限:chmod +x while.sh
  286. 执行脚本:bash while.sh
  287. ```
  288. `until`循环
  289. ```
  290. 创建脚本文件:nano until.sh
  291. #!/bin/bash
  292. counter=6
  293. until [ $counter -lt 3 ]; do
  294. let counter-=1
  295. echo $counter
  296. done
  297. 赋予权限:chmod +x until.sh
  298. 运行脚本:bash until.sh
  299. ```
  300. # 算术
  301. ```
  302. a=$(( 12 + 5 ))
  303. echo a
  304. expr 2 + 2
  305. let a=2+2
  306. echo $a
  307. echo '8.5 / 2.3' | bc
  308. ```