读书人

Shell编程第三章 Shell输入跟输出

发布时间: 2013-04-02 12:35:26 作者: rapoo

Shell编程——第三章 Shell输入和输出
笔记

#Shell输入和输出#echo:显示文本行活变量,或者把字符串输入到文件,默认回车换行#\c:回车不换行#\f:禁止#\t:跳格,相当于按了一个Tab键#\n:回车换行#更名#sudo mv edhod.sh echod#改变权限#chmod 755 echod#cat echod#!/bin/bash#echodecho -e "This echo's 3 new lines\n\n\n"echo "ok"echoecho "This echo's 3 new lines\n\n\n"echo "The log files have all been done">mylogfile.txt#创mylogfile.txt文件sudo touch mylogfile.txt#执行(-e解析特殊字符)./echodThis echo's 3 new linesokThis echo's 3 new lines\n\n\n#演示回车换行和回车不换行[root@localhost 0330]# cat mylogfile.txtThe log files have all been done#回车换行[root@localhost 0330]# echo "The log files have all been done"                  Th log files have all been done#回车不换行[root@localhost 0330]# echo -n "The log files have all been done"The log files have all been done[root@localhost 0330]##read#read语句可以从键盘或者文件的某一行文本中读入信息,并将其赋给一个变量#vi readnamechmod 755 readname[root@localhost 0330]# ./readnameFirst Name:ChinaitlabLast Name:Shen zhenYour First Name is:ChinaitlabYour Last Name is:Shen zhen#演示以空格作为分隔符[root@localhost 0330]#cat  readname#!/bin/bash#readnameecho -n "First Name:"read firstnameecho -n "Last Name:"read lastname subnameecho -e "Your First Name is:${firstname}\n"echo -e "Your Last Name is:${lastname}\n"echo -e "Your Subname is:${subname}\n"[root@localhost 0330]# ./readnameFirst Name:chinaitlabLast Name:shen zhenYour First Name is:chinaitlabYour Last Name is:shenYour Subname is:zhen#catman cat#单独显示myfile内容cat myfile#显示myfile1 myfile2 myfile3的内容[root@localhost 0330]# cat myfile1 myfile2 myfile3this is myfile1this is myfile2this is myfile3#将myfile1 myfile2 myfile3重定向到myfile123[root@localhost 0330]# cat myfile1 myfile2 myfile3 > myfile123[root@localhost 0330]# cat myfile123this is myfile1this is myfile2this is myfile3#dos传输的文本cat -v dos.txt#不会分页显示ls -l /bin > ls.txtcat ls.txt#分页显示方法一cat ls.txt | more#分页显示方法二cat ls.txt | less#分页显示方法三less ls.txt#分页显示方法四more ls.tx#管道(|)#分页显示#cat ls.txt | more#查找myfile#ls -l | grep "myfile"#显示分区信息#df k#打印第一列分区信息#df -k | awk '{print $1}'#过滤掉Filesystem#df -k | awk '{print $1}' | grep -v "Filesystem"#tee#把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中[root@localhost 0330]# who | tee who.outroot     pts/0        Mar 30 19:00 (192.168.223.1)[root@localhost 0330]# who | tee who.out[root@localhost 0330]# cat who.outroot     pts/0        Mar 30 19:00 (192.168.223.1)[root@localhost 0330]# who | tee -a who.outroot     pts/0        Mar 30 19:00 (192.168.223.1)[root@localhost 0330]# cat who.outroot     pts/0        Mar 30 19:00 (192.168.223.1)root     pts/0        Mar 30 19:00 (192.168.223.1)#将分区内容打印到屏幕并写入到文件[root@localhost 0330]# df -k | awk '{print $1}' | grep -v "Filesystem" | tee partation.txt/dev/sda2/dev/sda1none.host:/[root@localhost 0330]# cat partation.txt/dev/sda2/dev/sda1none.host:/#标准输入、输出和错误#标准输入 0 缺省键盘#标准输出 1 缺省屏幕#标准错误 2 缺省屏幕#文件重定向#> 重定向新文件#>> 追加[root@localhost 0330]# cat filejennyjohnwendyanniemarryjack#将排序内容重定向到sort.out[root@localhost 0330]# cat file | sort 1>sort.out[root@localhost 0330]# cat sort.outanniejackjennyjohnmarrywendy #cat file | sort >sort.out等价于cat file | sort 1>sort.out#演示追加[root@localhost 0330]# cat path.out/home/wgb/shell/0330[root@localhost 0330]# pwd>>path.out[root@localhost 0330]# cat path.out/home/wgb/shell/0330/home/wgb/shell/0330#巧妙创建文件[root@localhost 0330]# ls -al nullfilels: nullfile: No such file or directory[root@localhost 0330]# >nullfile[root@localhost 0330]# ls -al nullfile-rw-r--r--    1 root     root            0 Mar 30 19:36 nullfile#[root@localhost 0330]# sort <fileanniejackjennyjohnmarrywendy[root@localhost 0330]# cat name.txtchinaitlabshenzhen#将name.txt的内容作为输入传递给sort并将排序结果输出到name.out[root@localhost 0330]# sort< name.txt >name.out[root@localhost 0330]# cat name.outchinaitlabshenzhen#终止符,输入某个输入终止输入[root@localhost 0330]# cat term.txtthis is a test[root@localhost 0330]# cat >>term.txt <<CHINAITLAB> Hello,there I am using a $TERM terminal> and my username is $LOGNAME> BYE ...> CHINAITLAB[root@localhost 0330]# cat term.txtthis is a testHello,there I am using a xterm terminaland my username is rootBYE ...#将错误内容重定向到文件[root@localhost 0330]# grep "trident" missilesgrep: missiles: No such file or directory[root@localhost 0330]# ls -al missile 2> err_message.txt[root@localhost 0330]# cat err_message.txtls: missile: No such file or directory#/dev/null:无底洞#将错误内容抛出[root@localhost 0330]# grep "trident" missiles 2>/dev/nullcat /dev/null#显示account_new.txt account_old.txt的内容,将标准输入到accounts.out,错误输出到accounts_err[root@localhost 0330]# cat account_new.txt account_old.txt 1>accounts.out 2>accounts_err[root@localhost 0330]# cat account_new.txtaccount_old.txt 1>accounts.out 2>accounts.err[root@localhost 0330]# cat account_old.txtcat: account_old.txt: No such file or directory[root@localhost 0330]# cat accounts.outaccount_old.txt 1>accounts.out 2>accounts.err[root@localhost 0330]# ls -al account_old.txtls: account_old.txt: No such file or directory[root@localhost 0330]# cat accounts_errcat: account_old.txt: No such file or directory[root@localhost 0330]# cat account.errcat: account.err: No such file or directory#合并标准输出和标准错误[root@localhost 0330]# grep "standard" standard.txt > grep.out 2>&1[root@localhost 0330]# cat grep.outgrep: standard.txt: No such file or directoryvi standard.txt[root@localhost 0330]# cat standard.txtchina it labshen zhenstandard shellbashlinux shellredhat[root@localhost 0330]# grep "standard" standard.txt > grep.out 2>&1[root@localhost 0330]# cat grep.outstandard shell#execexec ./helloworld.sh#重启Shell#重新登录并设置环境变量[root@localhost root]# export CHINAITLAB=ShenZhen[root@localhost root]# echo $CHINAITLABShenZhen#再次执行脚本,再次打印刚才设置的环境变量时无内容exec ./helloworld.shecho $CHINAITLAB#文件描述符(exec和文件描述符结合使用不会重启Shell)[root@localhost 0330]# vi file_desc[root@localhost 0330]# cat file_desc#!/bin/bash#file_descexec 3<&0 0<name.txtread line1read line2exec 0<&3echo $line1echo $line2[root@localhost 0330]# vi name.txt[root@localhost 0330]# chmod 755 file_desc[root@localhost 0330]# cat name.txtchinaitlabshenzhen[root@localhost 0330]# ./file_descchinaitlabshenzhen


附图

Shell编程——第三章 Shell输入跟输出

Shell编程——第三章 Shell输入跟输出

Shell编程——第三章 Shell输入跟输出

Shell编程——第三章 Shell输入跟输出

Shell编程——第三章 Shell输入跟输出

Shell编程——第三章 Shell输入跟输出Shell编程——第三章 Shell输入跟输出 Shell编程——第三章 Shell输入跟输出@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]

读书人网 >编程

热点推荐