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

来源:互联网 发布:数据散修txt全集下载 编辑:程序博客网 时间:2024/06/04 23:25

从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. $
 
原创粉丝点击