第三章 shell的输出与输入
时间:2006-05-19 来源:wumu888
#name:echod
echo -e "May\tday\nMay\tday" #解析转义字符
echo "This echo's 3 new lines\n\n\n" #无解析转义字符
echo -e "This is echo's 3 new lines \n\n\n" #解析转义字符
echo "OK" #echo基本功能
echo "" #会出现什么情况?
echo "do you known about it?">/root/echo.out #echo重定向功能 [root@server shell]# chmod u+x echo.sh #给脚本以执行权限;
[root@server shell]# ./echo.sh #运行脚本
May day
May day
This echo's 3 new lines\n\n\n
This is echo's 3 new lines OK
具体内容请参见上面内容; 这里讲一下刚才那个echo的重定向功能,它会把最后一行的显示保存在/root/下的echo.out里,这里查看;
[root@server shell]# ls /root #存在 |
#!/bin/bash |
[root@server shell]# chmod u+x read.sh #加X权限 [root@server shell]# ./read.sh #执行 Please input your fristname: he # 提示输入 hello he,Please input your lastname: senlin Your full name is he senlin ^_^ [root@server shell]# |
[root@server shell]# read h j k l #四个变量 A B C #只给三个值 [root@server shell]# echo $h $j $k $l #打印变量 A B C #这里只显三个,思考一下变量l |
[root@server shell]# set | grep "l=" #查看l l= #l赋了空值, [root@server shell]# |
[root@server shell]# read a b c d 1 2 3 4 5 6 [root@server shell]# echo $a 1 [root@server shell]# echo $b 2 [root@server shell]# echo $c 3 [root@server shell]# echo $d 4 5 6 [root@server shell]# |
[root@server shell]# cat myfile1 #单文件显示 this my file1 #内容 [root@server shell]# cat myfile2 #单文件显示 this my file2 #内容 [root@server shell]# cat myfile3 #单文件显示 this is my file3 #内容 [root@server shell]# cat myfile1 myfile2 myfile3#多文件显示 this my file1 this my file2 #内容 this is my file3 [root@server shell]# cat myfile1 myfile2 myfile3 > myfile123 #重定向 [root@server shell]# cat myfile123 #显示其内容; this my file1 this my file2 #内容信息; this is my file3 [root@server shell]# |
[root@server shell]# ls -Rl /usr/local |more
/usr/local/bin:
/usr/local/etc:
/usr/local/games: |
[root@server shell]# df -T | awk '{print $1}'|grep -v "Filesystem" /dev/sda2 /dev/sda1 none [root@server shell]# |
[root@server shell]# who #只查看不保存 root tty1 May 16 03:24 #内 root pts/0 May 16 03:34 (192.168.1.98) #容 [root@server shell]# who | tee who.out #查看且保存 root tty1 May 16 03:24 #内 root pts/0 May 16 03:34 (192.168.1.98) #容 [root@server shell]# ls #保存是否成功 echo.sh myfile1 myfile123 myfile2 myfile3 read.sh who.out #保存成功 [root@server shell]# cat who.out #查看保存信息 root tty1 May 16 03:24 #内 root pts/0 May 16 03:34 (192.168.1.98) #容 [root@server shell]# |
[root@server shell]# who |tee who.out #不加-a root tty1 May 16 03:24 root pts/0 May 16 03:34 (192.168.1.98) [root@server shell]# cat who.out #查看保存信息 root tty1 May 16 03:24 #内 root pts/0 May 16 03:34 (192.168.1.98) #容 [root@server shell]# who |tee -a who.out #加-a root tty1 May 16 03:24 root pts/0 May 16 03:34 (192.168.1.98) [root@server shell]# cat who.out #查看保存讯息 root tty1 May 16 03:24 root pts/0 May 16 03:34 (192.168.1.98)#内 root tty1 May 16 03:24 root pts/0 May 16 03:34 (192.168.1.98)#容 |
文件 |
文件描述符 |
标准输入 | 0键盘,也可以是文件 |
标准输出 | 1.屏幕,也可以是文件 |
错误输出 | 2.同上 |
|
[root@server shell]# zhongshanlu=Xiamen #设置变量
May day
OK #按回车就会重新启动一个shell
Last login: Tue May 16 03:34:19 2006 from 192.168.1.98 #已被清空喽
[root@server root]# |
[root@server shell]# cat name.txt #编辑并保存 此文档 Xia men zhong shan lu [root@server shell]# cat exec3.9.sh #编辑并保存此脚本 #!/bin/bash #name:desc exec 9<&0 0<name.txt #内 read line1 read line2 exec 0<&9 echo $line1 echo $line2 #容 [root@server shell]# chmod u+x exec3.9.sh #赋执行权限 [root@server shell]# ./exec3.9.sh #运行脚本 Xia men zhong shan lu [root@server shell]# |
使用上面的好处就是不再开启新shell,懒人模式;
这一章到此结束!