第一部分 Shell基础编程——第三章 Shell输入和输出
2013-03-30 20:31
483 查看
笔记
#Shell输入和输出 #echo:显示文本行活变量,或者把字符串输入到文件,默认回车换行 #\c:回车不换行 #\f:禁止 #\t:跳格,相当于按了一个Tab键 #\n:回车换行 #更名 #sudo mv edhod.sh echod #改变权限 #chmod 755 echod #cat echod #!/bin/bash #echod echo -e "This echo's 3 new lines\n\n\n" echo "ok" echo echo "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解析特殊字符) ./echod This echo's 3 new lines ok This echo's 3 new lines\n\n\n #演示回车换行和回车不换行 [root@localhost 0330]# cat mylogfile.txt The 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 readname chmod 755 readname [root@localhost 0330]# ./readname First Name:Chinaitlab Last Name:Shen zhen Your First Name is:Chinaitlab Your Last Name is:Shen zhen #演示以空格作为分隔符 [root@localhost 0330]# cat readname #!/bin/bash #readname echo -n "First Name:" read firstname echo -n "Last Name:" read lastname subname echo -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]# ./readname First Name:chinaitlab Last Name:shen zhen Your First Name is:chinaitlab Your Last Name is:shen Your Subname is:zhen #cat man cat #单独显示myfile内容 cat myfile #显示myfile1 myfile2 myfile3的内容 [root@localhost 0330]# cat myfile1 myfile2 myfile3 this is myfile1 this is myfile2 this is myfile3 #将myfile1 myfile2 myfile3重定向到myfile123 [root@localhost 0330]# cat myfile1 myfile2 myfile3 > myfile123 [root@localhost 0330]# cat myfile123 this is myfile1 this is myfile2 this is myfile3 #dos传输的文本 cat -v dos.txt #不会分页显示 ls -l /bin > ls.txt cat 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.out root pts/0 Mar 30 19:00 (192.168.223.1) [root@localhost 0330]# who | tee who.out [root@localhost 0330]# cat who.out root pts/0 Mar 30 19:00 (192.168.223.1) [root@localhost 0330]# who | tee -a who.out root pts/0 Mar 30 19:00 (192.168.223.1) [root@localhost 0330]# cat who.out root 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/sda1 none .host:/ [root@localhost 0330]# cat partation.txt /dev/sda2 /dev/sda1 none .host:/ #标准输入、输出和错误 #标准输入 0 缺省键盘 #标准输出 1 缺省屏幕 #标准错误 2 缺省屏幕 #文件重定向 #> 重定向新文件 #>> 追加 [root@localhost 0330]# cat file jenny john wendy annie marry jack #将排序内容重定向到sort.out [root@localhost 0330]# cat file | sort 1>sort.out [root@localhost 0330]# cat sort.out annie jack jenny john marry wendy #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 nullfile ls: 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 <file annie jack jenny john marry wendy [root@localhost 0330]# cat name.txt chinaitlab shenzhen #将name.txt的内容作为输入传递给sort并将排序结果输出到name.out [root@localhost 0330]# sort< name.txt >name.out [root@localhost 0330]# cat name.out chinaitlab shenzhen #终止符,输入某个输入终止输入 [root@localhost 0330]# cat term.txt this 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.txt this is a test Hello,there I am using a xterm terminal and my username is root BYE ... #将错误内容重定向到文件 [root@localhost 0330]# grep "trident" missiles grep: missiles: No such file or directory [root@localhost 0330]# ls -al missile 2> err_message.txt [root@localhost 0330]# cat err_message.txt ls: missile: No such file or directory #/dev/null:无底洞 #将错误内容抛出 [root@localhost 0330]# grep "trident" missiles 2>/dev/null cat /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.txt account_old.txt 1>accounts.out 2>accounts.err [root@localhost 0330]# cat account_old.txt cat: account_old.txt: No such file or directory [root@localhost 0330]# cat accounts.out account_old.txt 1>accounts.out 2>accounts.err [root@localhost 0330]# ls -al account_old.txt ls: account_old.txt: No such file or directory [root@localhost 0330]# cat accounts_err cat: account_old.txt: No such file or directory [root@localhost 0330]# cat account.err cat: account.err: No such file or directory #合并标准输出和标准错误 [root@localhost 0330]# grep "standard" standard.txt > grep.out 2>&1 [root@localhost 0330]# cat grep.out grep: standard.txt: No such file or directory vi standard.txt [root@localhost 0330]# cat standard.txt china it lab shen zhen standard shell bash linux shell redhat [root@localhost 0330]# grep "standard" standard.txt > grep.out 2>&1 [root@localhost 0330]# cat grep.out standard shell #exec exec ./helloworld.sh #重启Shell #重新登录并设置环境变量 [root@localhost root]# export CHINAITLAB=ShenZhen [root@localhost root]# echo $CHINAITLAB ShenZhen #再次执行脚本,再次打印刚才设置的环境变量时无内容 exec ./helloworld.sh echo $CHINAITLAB #文件描述符(exec和文件描述符结合使用不会重启Shell) [root@localhost 0330]# vi file_desc [root@localhost 0330]# cat file_desc #!/bin/bash #file_desc exec 3<&0 0<name.txt read line1 read line2 exec 0<&3 echo $line1 echo $line2 [root@localhost 0330]# vi name.txt [root@localhost 0330]# chmod 755 file_desc [root@localhost 0330]# cat name.txt chinaitlab shenzhen [root@localhost 0330]# ./file_desc chinaitlab shenzhen
附图
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客] |
相关文章推荐
- shell基础编程——输出和输入
- Shell基础-Bash基本功能-输入输出重定向
- shell基础五:输入和输出(echo,read,cat,管道,tee,重定向等)
- shell基础五:输入和输出(echo,read,cat,管道,tee,重定向等)
- shell中的输入输出和编程中的变量(shell 03)
- 第一部分 Shell基础编程——第四章 控制流结构
- Linu Shell 编程基础--变量,简单的输出,echo,printf
- Linux学习日记 —— 10.3.3 Shell基础-Bash基本功能-输入输出重定向
- 【Shell 编程基础第一部分】Shell脚本HelloShell及简单的Shell基础 推荐
- shell学习-基础(变量、输入、输出、数组、环境变量)
- 黑马程序员之WinForm编程基础学习笔记:在多行文本框中输入多行"姓名=成绩"格式的数据,要求输出成绩最高的学生的姓名和成绩。
- shell基础-输入输出以及重定向/多命令顺序执行与管道符/通配符与特殊符号
- 第三章 shell输入与输出
- linux shell 编程笔记 - shell的输入与输出 -3- 标准输入-标准输出-标准错误
- 第一部分 Shell基础编程——第五章 文本过滤
- Shell编程—根据用户输入来输出符号正方形
- 第一部分 Shell基础编程——第六章 Shell函数
- shell基础五:输入和输出(echo,read,cat,管道,tee,重定向等)
- 【Shell 编程基础第一部分】第一个Shell脚本HelloShell及一些简单的Shell基础书写与概念;
- 【Shell 编程基础第一部分】第一个Shell脚本HelloShell及一些简单的Shell基础书写与概念