Shell的输入和输出(echo,read,cat,管道,tee,重定向等)


从echo的变量开始说起
如:echo 命令输出转义符以及变量。

以下是代码:
  1. # echo -e "/007your home is $HOME , you are connected on `tty`"
  2. your home is /root , you are connected on /dev/pts/1
  3. # echo -e "/ayour home is $HOME , you are connected on `tty`"
  4. your home is /root , you are connected on /dev/pts/1




本例中
/007或/a你可以让终端铃响一声
显示出$HOME目录,
并且可以让系统执行tty命令(注意,该命令用键盘左上角的符号,法语中的抑音符引起来,不是单引号 )。

在 echo 命令输出之后附加换行,可以使用/ n选项:

以下是代码:
  1. $ cat echod
  2. #!/bin/sh
  3. echo -e "this echo's 3 new lines/n/n/n"
  4. echo "OK"


编辑一个新echod,如上内容,然后运行输出如下:

$ ./echod
this echo's 3 new lines



OK
$


在e c h o语句中使用跳格符,记住别忘了加反斜杠/:

以下是代码:
  1. $ echo -e "here is a tab/there are two tabs/t/tok"
  2. here is a tab   here are two tabs               ok
  3. $



把一个字符串输出到文件中,使用重定向符号 >
在下面的例子中一个字符串被重定向到一个名为m y f i l e的文件中:

以下是代码:
  1. $ echo "The log files have all been done"> myfile



或者可以追加到一个文件的末尾,这意味着不覆盖原有的内容:

以下是代码:
  1. $ echo "$LOGNAME carried them out at `date`">>myfile


现在让我们看一下m y f i l e文件中的内容:

The log files have all been done
sam carried them out at 六 11月 13 12:54:32 CST 2004


引号是一个特殊字符,所以必须要使用反斜杠/来使 shell 忽略它的特殊含义。
假设你希望使用echo命令输出这样的字符串:“/ d e v / r m t 0”,那么我们只要在引号前面加上反斜杠/即可:

以下是代码:
  1. $ echo "/"/dev/rmt0"/"
  2. "/dev/rmt0"
  3. $
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值