Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 操作系统 > Linux > 第三章 shell的输出与输入
【标  题】:第三章 shell的输出与输入
【关键字】:shell
【来  源】:http://www.cublog.cn/u/18861/showart.php?id=115063

第三章 shell的输出与输入

Your Ad Here
第三章  shell的输出与输入
     本章导航;
         3.1 echo
         3.2 read
         3.3 cat
         3.4 管道<|>
         3.5 tee
         3.6 exec
         3.7 文件重定向
         3.8 标准输入与输出,和错误
         3.9 合并标准输出和标准错误
         3.10 使用文件描述符
  3.1 echo讲解
   3.1.1 作用:
         echo命令主要用来显示文件行或变量或把字符串输入到文件,
   3.1.2 格式:
          echo [option] string
   3.1.3 参数:
           -e 解析转义字符
                \n回车不换行
                \f进纸
                \t跳格
                \c回车换行
           -n 回车不换行,linux默认回车换行
   3.1.4  例:
           新建一脚本命名为echo.sh,在里面写入如下代码; 
#!/bin/bash
#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     #存在       
echo.out  log  rpm  shell
[root@server shell]# cat /root/echo.out   #查看内容  
do you known about it?              #正确
[root@server shell]# echo -n "hello my dear"  #自己试下

 
 
3.2 read
    3.2.1 定义   
      read 语句可以从键盘或文件中的某一行文本中读入信息,并将其赋给一个变量; 
    3.2.2 格式;
      read variblel1 variblel2...
       如果只指定了一个变量,那么read将会把所有输入赋给该变量,直至遇到第一个文件结束符或回车,如果给出了多个变量,它们会按顺序分别被赋予不同的变量,shell将用空格作为变量之间的分隔符;如果给出的值超出了变量数量,则会把最后所有的值赋给最后一个变量;
   3.2.3 例
    创建一个名为read.sh的脚本并写入以下内容;

#!/bin/bash
#name:readname.sh
echo -n "Please input your fristname: "
read fristname
echo -n "hello $fristname,Please input your lastname: "
read lastname
echo "Your full name is $fristname $lastname ^_^"

         保存退出;给脚本执行的权限,并运行;
[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]#
     3.2.3.1     这里再讲一下关于多个变量同时赋值的问题;先讲变量多, 值少会出现什么情况;
[root@server shell]# read h j k l  #四个变量
A B C                  #只给三个值
[root@server shell]# echo $h $j $k $l  #打印变量
A B C              #这里只显三个,思考一下变量l
         变量l怎么回事,难道是因为值少就不给赋值了吗? 错,这里提醒给多少个变量要是因为值少的缘故会出现变量为空的现象;可用;
    
[root@server shell]# set | grep "l=" #查看l
l=                     #l赋了空值,
[root@server shell]#
    3.2.3.2   值和变量相等就不多敖述了;
    3.2.3.3   要是值多变量少怎么办呢?看例;
    
[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]#
     不管怎么说,可以用一句话总结;变多值少后变为空,变少值多后变多值;
 3.3 cat
    3.3.1 定义
        cat 是一个简单而通用的命令,可以用它来显示文件字内容,创建文件,还可以来显示控制字符.常和more连用,达到分页的效果;
    3.3.2 格式
        cat [option] filename1 filename2 ....
      参数;
        -n 显示行数
        -b 不给空行标行数
        -s 连续二个空行只显一个空行
        -v 显示控制字符
               更多讯息请参考 man cat
   3.3.3 例:
          显示和多文件显示级多文件显示并保存成一个文件;
[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]#
3.4 管道
  3.4.1 定义:
      可以通过管道把一个命令的输出传递给另一个命令做为输入,管道用"|"表示;
  3.4.2 格式;
       Comman1 | Command2 | ...
  3.4.3 例;
      一个管道符

[root@server shell]# ls -Rl /usr/local |more
/usr/local:
total 48
drwxr-xr-x    2 root     root         4096 Jan 25  2003 bin
drwxr-xr-x    2 root     root         4096 Jan 25  2003 etc
drwxr-xr-x    2 root     root         4096 Jan 25  2003 games
drwxr-xr-x    2 root     root         4096 Jan 25  2003 include
drwxr-xr-x    2 root     root         4096 Jan 25  2003 lib
drwxr-xr-x    2 root     root         4096 Jan 25  2003 libexec
drwxr-xr-x    2 root     root         4096 Jan 25  2003 sbin
drwxr-xr-x    4 root     root         4096 May 12 15:58 share
-rw-r--r--    1 root     root        10627 May 15 11:33 smb.conf
drwxr-xr-x    2 root     root         4096 Jan 25  2003 src

/usr/local/bin:
total 0

/usr/local/etc:
total 0

/usr/local/games:
total 0
--MORE--

   二个管道符;
[root@server shell]# df -T | awk '{print $1}'|grep -v "Filesystem"
/dev/sda2
/dev/sda1
none
[root@server shell]#
    三个或更多个,,就不多演示了;
 3.5 tee
  3.5.1 定义;
     tee命令把输出的一个副本输送到标准输出,另一个副本保存到相应的文件里;常与管道连用;
   3.5.2 格式
       ls -l |tee ls.out
   3.5.3  用途
        如果希望看到输出的同时,也想把相应的内容保存到一个文件,以便日后查看,那这个命令很适合哦,一般用在管道之后;
   3.5.4 例;
      
[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)#容
   相信这样就很明了呢,加-a就是代表追加的意思,而不加呢就是代表覆盖喽,最好每次使用还是带-a安全些;
3.6 标准输入,输出,错误,
  3.6.1 定义
     在shell中执行命令时,每个进程都要和三个打开的文件相联系,并使用文件描述符来引用这些文件,由于文件描述符并不容易记忆,shell同时也给出了相应的文件名;

文件

 文件描述符
 标准输入  0键盘,也可以是文件
 标准输出  1.屏幕,也可以是文件
 错误输出  2.同上
   系统中实际上有12个文件描述符;可任意使用文件描述符3~9;
3.7  文件重定向
   定义;
    改变程序的输入来源和输出地点;
 comman > filname  把标准输出重定向一个新文件中
 comman >> filename  把标准输出重定向到一个旧文件中
 comman 1 > filename  把标准输出重定抽到一个文件中
comman > filename 2>&1  把标准输出和标准错误一起重定向到一个文
 comman 2 > filename  把标准错误重定向到一个文件中
 command 2 >> filename  把标准错误重定向到一个老文件中
comman>>filename 2>&1  把标准输出和标准错误重定向到老文件中
 cmd<file>file1  cmd以file为输入以file2为输出
 cmd < filename  cmd以file文件作为标准输入
 cmd << delimiter  从标准输入中读入,遇到delimiter分界
 cmd<&m  把文件描述符作为标准输入
 cmd>&m  把标准输出重定到文件描述符m中
 cmd<&-  关闭标准输入
 
3.8 和3.9请就3.7多多练习,再讲一下exec
  exec
      定义;
       exec命令可以用来替代当前shell,换句话就是没有启动子shell,使用这一命令时任何现有环境都会被清除,并重新启动一个shell
   格式;
      exec command
           其中的command通常是一个shee脚本
    适用范围;
       对文件描述符进行操作的时候<也只有在这用>,它不会覆盖你当前的shell.
     例;

[root@server shell]# zhongshanlu=Xiamen   #设置变量
[root@server shell]# export zhongshanlu   #升为环境变量
[root@server shell]# exec ./
echo.sh    error.out  myfile1    myfile123  myfile2    myfile3    read.sh
[root@server shell]# exec ./echo.sh    #运行一个脚本

May     day
May     day
This echo's 3 new lines\n\n\n
This is echo's 3 new lines

 

OK                  #按回车就会重新启动一个shell

Last login: Tue May 16 03:34:19 2006 from 192.168.1.98
[root@server root]# echo $zhongshanlu  #再次查看

              #已被清空喽

[root@server root]#

 3.10 文件描述符
    exec与文件描术符的结合使用,
[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,懒人模式;

    这一章到此结束!

一个配置好的MAIL服务器除基本功能外应该具有的其它功能::【上一篇】
检查点调优: 案例研究:【下一篇】
【相关文章】
  • Unix编程/应用问答中文版 ---20.shell script问题
  • SHELL十三问之六:exec 跟 source 差在哪?
  • SHELL十三问之七:( ) 与 { } 差在哪?
  • 一个自动重新启动无故退出程序的shell脚本
  • 给eshell设置环境变量
  • Gmail网络磁盘(Gmail Drive Shell Extension1.0.10)
  • Shell基本命令集
  • 写shell程序时需要注意的几个地方
  • Linux主要shell命令详解
  • [精品推荐]shell命令[转帖]
  • 【随机文章】
  • Default layout 你说的应该是控制面板
  • 今天早上公交车上想到的系统的实现模式
  • RCCD and RCCDPlus 算法分析
  • redhat 8.0汉化 (版本0.4)
  • 在 Windows2000 环境下实现动态 DNS 的安全考虑
  • doskey
  • asp中生成xbm格式的验证码
  • 用JSP/ASP创建WAP应用
  • 我-linux-自己的os(演示版)-(类似linux 0.01)
  • Photoshop让漂亮美女逃离画框
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.