shell基础编程——输出和输入

[plain]  view plain  copy
  1. #Shell输入和输出  
  2.   
  3. #echo:显示文本行活变量,或者把字符串输入到文件,默认回车换行  
  4. #\c:回车不换行  
  5. #\f:禁止  
  6. #\t:跳格,相当于按了一个Tab键  
  7. #\n:回车换行  
  8.   
  9. #更名  
  10. #sudo mv edhod.sh echod  
  11. #改变权限  
  12. #chmod 755 echod  
  13.   
  14. #cat echod  
  15. #!/bin/bash  
  16. #echod  
  17. echo -e "This echo's 3 new lines\n\n\n"  
  18. echo "ok"  
  19. echo  
  20. echo "This echo's 3 new lines\n\n\n"  
  21. echo "The log files have all been done">mylogfile.txt  
  22.   
  23. #创mylogfile.txt文件  
  24. sudo touch mylogfile.txt  
  25.   
  26. #执行(-e解析特殊字符)  
  27. ./echod  
  28. This echo's 3 new lines  
  29.   
  30.   
  31.   
  32. ok  
  33.   
  34. This echo's 3 new lines\n\n\n  
  35.   
  36. #演示回车换行和回车不换行  
  37. [root@localhost 0330]# cat mylogfile.txt  
  38. The log files have all been done  
  39.   
  40. #回车换行  
  41. [root@localhost 0330]# echo "The log files have all been done"                  Th log files have all been done  
  42.   
  43. #回车不换行  
  44. [root@localhost 0330]# echo -n "The log files have all been done"  
  45. The log files have all been done[root@localhost 0330]#  
  46.   
  47. #read  
  48. #read语句可以从键盘或者文件的某一行文本中读入信息,并将其赋给一个变量  
  49. #vi readname  
  50. chmod 755 readname  
  51. [root@localhost 0330]# ./readname  
  52. First Name:Chinaitlab  
  53. Last Name:Shen zhen  
  54. Your First Name is:Chinaitlab  
  55.   
  56. Your Last Name is:Shen zhen  
  57.   
  58. #演示以空格作为分隔符  
  59. [root@localhost 0330]#  
  60. cat  readname  
  61. #!/bin/bash  
  62. #readname  
  63. echo -n "First Name:"  
  64. read firstname  
  65. echo -n "Last Name:"  
  66. read lastname subname  
  67. echo -e "Your First Name is:${firstname}\n"  
  68. echo -e "Your Last Name is:${lastname}\n"  
  69. echo -e "Your Subname is:${subname}\n"  
  70. [root@localhost 0330]# ./readname  
  71. First Name:chinaitlab  
  72. Last Name:shen zhen  
  73. Your First Name is:chinaitlab  
  74.   
  75. Your Last Name is:shen  
  76.   
  77. Your Subname is:zhen  
  78.   
  79. #cat  
  80. man cat  
  81. #单独显示myfile内容  
  82. cat myfile  
  83.   
  84. #显示myfile1 myfile2 myfile3的内容  
  85. [root@localhost 0330]# cat myfile1 myfile2 myfile3  
  86. this is myfile1  
  87. this is myfile2  
  88. this is myfile3  
  89.   
  90. #将myfile1 myfile2 myfile3重定向到myfile123  
  91. [root@localhost 0330]# cat myfile1 myfile2 myfile3 > myfile123  
  92. [root@localhost 0330]# cat myfile123  
  93. this is myfile1  
  94. this is myfile2  
  95. this is myfile3  
  96.   
  97. #dos传输的文本  
  98. cat -v dos.txt  
  99.   
  100. #不会分页显示  
  101. ls -l /bin > ls.txt  
  102. cat ls.txt  
  103.   
  104. #分页显示方法一  
  105. cat ls.txt | more  
  106.   
  107. #分页显示方法二  
  108. cat ls.txt | less  
  109.   
  110. #分页显示方法三  
  111. less ls.txt  
  112.   
  113. #分页显示方法四  
  114. more ls.tx  
  115.   
  116. #管道(|)  
  117.   
  118. #分页显示  
  119. #cat ls.txt | more  
  120.   
  121. #查找myfile  
  122. #ls -l | grep "myfile"  
  123.   
  124. #显示分区信息  
  125. #df –k  
  126.   
  127. #打印第一列分区信息  
  128. #df -k | awk '{print $1}'  
  129.   
  130. #过滤掉Filesystem  
  131. #df -k | awk '{print $1}' | grep -v "Filesystem"  
  132.   
  133. #tee  
  134. #把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中  
  135. [root@localhost 0330]# who | tee who.out  
  136. root     pts/0        Mar 30 19:00 (192.168.223.1)  
  137. [root@localhost 0330]# who | tee who.out  
  138. [root@localhost 0330]# cat who.out  
  139. root     pts/0        Mar 30 19:00 (192.168.223.1)  
  140. [root@localhost 0330]# who | tee -a who.out  
  141. root     pts/0        Mar 30 19:00 (192.168.223.1)  
  142. [root@localhost 0330]# cat who.out  
  143. root     pts/0        Mar 30 19:00 (192.168.223.1)  
  144. root     pts/0        Mar 30 19:00 (192.168.223.1)  
  145.   
  146. #将分区内容打印到屏幕并写入到文件  
  147. [root@localhost 0330]# df -k | awk '{print $1}' | grep -v "Filesystem" | tee partation.txt  
  148. /dev/sda2  
  149. /dev/sda1  
  150. none  
  151. .host:/  
  152. [root@localhost 0330]# cat partation.txt  
  153. /dev/sda2  
  154. /dev/sda1  
  155. none  
  156. .host:/  
  157.   
  158. #标准输入、输出和错误  
  159. #标准输入 0 缺省键盘  
  160. #标准输出 1 缺省屏幕  
  161. #标准错误 2 缺省屏幕  
  162.   
  163. #文件重定向  
  164. #> 重定向新文件  
  165. #>> 追加  
  166.   
  167. [root@localhost 0330]# cat file  
  168. jenny  
  169. john  
  170. wendy  
  171. annie  
  172. marry  
  173. jack  
  174.   
  175. #将排序内容重定向到sort.out  
  176. [root@localhost 0330]# cat file | sort 1>sort.out  
  177. [root@localhost 0330]# cat sort.out  
  178. annie  
  179. jack  
  180. jenny  
  181. john  
  182. marry  
  183. wendy   
  184.   
  185. #cat file | sort >sort.out等价于cat file | sort 1>sort.out  
  186.   
  187. #演示追加  
  188. [root@localhost 0330]# cat path.out  
  189. /home/wgb/shell/0330  
  190. [root@localhost 0330]# pwd>>path.out  
  191. [root@localhost 0330]# cat path.out  
  192. /home/wgb/shell/0330  
  193. /home/wgb/shell/0330  
  194. #巧妙创建文件  
  195. [root@localhost 0330]# ls -al nullfile  
  196. ls: nullfile: No such file or directory  
  197. [root@localhost 0330]# >nullfile  
  198. [root@localhost 0330]# ls -al nullfile  
  199. -rw-r--r--    1 root     root            0 Mar 30 19:36 nullfile  
  200.   
  201. #  
  202. [root@localhost 0330]# sort <file  
  203. annie  
  204. jack  
  205. jenny  
  206. john  
  207. marry  
  208. wendy  
  209. [root@localhost 0330]# cat name.txt  
  210. chinaitlab  
  211. shenzhen  
  212. #将name.txt的内容作为输入传递给sort并将排序结果输出到name.out  
  213. [root@localhost 0330]# sort< name.txt >name.out  
  214. [root@localhost 0330]# cat name.out  
  215. chinaitlab  
  216. shenzhen  
  217.   
  218. #终止符,输入某个输入终止输入  
  219. [root@localhost 0330]# cat term.txt  
  220. this is a test  
  221. [root@localhost 0330]# cat >>term.txt <<CHINAITLAB  
  222. > Hello,there I am using a $TERM terminal  
  223. > and my username is $LOGNAME  
  224. > BYE ...  
  225. > CHINAITLAB  
  226. [root@localhost 0330]# cat term.txt  
  227. this is a test  
  228. Hello,there I am using a xterm terminal  
  229. and my username is root  
  230. BYE ...  
  231.   
  232. #将错误内容重定向到文件  
  233. [root@localhost 0330]# grep "trident" missiles  
  234. grep: missiles: No such file or directory  
  235. [root@localhost 0330]# ls -al missile 2> err_message.txt  
  236. [root@localhost 0330]# cat err_message.txt  
  237. ls: missile: No such file or directory  
  238. #/dev/null:无底洞  
  239.   
  240. #将错误内容抛出  
  241. [root@localhost 0330]# grep "trident" missiles 2>/dev/null  
  242. cat /dev/null  
  243.   
  244. #显示account_new.txt account_old.txt的内容,将标准输入到accounts.out,错误输出到accounts_err  
  245. [root@localhost 0330]# cat account_new.txt account_old.txt 1>accounts.out 2>accounts_err  
  246. [root@localhost 0330]# cat account_new.txt  
  247. account_old.txt 1>accounts.out 2>accounts.err  
  248. [root@localhost 0330]# cat account_old.txt  
  249. cat: account_old.txt: No such file or directory  
  250. [root@localhost 0330]# cat accounts.out  
  251. account_old.txt 1>accounts.out 2>accounts.err  
  252. [root@localhost 0330]# ls -al account_old.txt  
  253. ls: account_old.txt: No such file or directory  
  254. [root@localhost 0330]# cat accounts_err  
  255. cat: account_old.txt: No such file or directory  
  256. [root@localhost 0330]# cat account.err  
  257. cat: account.err: No such file or directory  
  258.   
  259.   
  260. #合并标准输出和标准错误  
  261. [root@localhost 0330]# grep "standard" standard.txt > grep.out 2>&1  
  262. [root@localhost 0330]# cat grep.out  
  263. grep: standard.txt: No such file or directory  
  264. vi standard.txt  
  265. [root@localhost 0330]# cat standard.txt  
  266. china it lab  
  267. shen zhen  
  268. standard shell  
  269. bash  
  270. linux shell  
  271. redhat  
  272. [root@localhost 0330]# grep "standard" standard.txt > grep.out 2>&1  
  273. [root@localhost 0330]# cat grep.out  
  274. standard shell  
  275.   
  276.   
  277. #exec  
  278. exec ./helloworld.sh  
  279. #重启Shell  
  280.   
  281. #重新登录并设置环境变量  
  282. [root@localhost root]# export CHINAITLAB=ShenZhen  
  283. [root@localhost root]# echo $CHINAITLAB  
  284. ShenZhen  
  285.   
  286. #再次执行脚本,再次打印刚才设置的环境变量时无内容  
  287. exec ./helloworld.sh  
  288. echo $CHINAITLAB  
  289.   
  290. #文件描述符(exec和文件描述符结合使用不会重启Shell)  
  291. [root@localhost 0330]# vi file_desc  
  292. [root@localhost 0330]# cat file_desc  
  293. #!/bin/bash  
  294. #file_desc  
  295. exec 3<&0 0<name.txt  
  296. read line1  
  297. read line2  
  298. exec 0<&3  
  299. echo $line1  
  300. echo $line2  
  301. [root@localhost 0330]# vi name.txt  
  302. [root@localhost 0330]# chmod 755 file_desc  
  303. [root@localhost 0330]# cat name.txt  
  304. chinaitlab  
  305. shenzhen  
  306. [root@localhost 0330]# ./file_desc  
  307. chinaitlab  
  308. shenzhen  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值