您的位置:首页 > 运维架构 > Shell

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

2007-02-09 09:31 639 查看
[align=right][/align]
从echo的变量开始说起
如:echo 命令输出转义符以及变量。

以下是代码:

# echo -e "/007your home is $HOME , you are connected on `tty`"

your home is /root , you are connected on /dev/pts/1

# echo -e "/ayour home is $HOME , you are connected on `tty`"

your home is /root , you are connected on /dev/pts/1

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

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

以下是代码:

$ cat echod

#!/bin/sh

echo -e "this echo's 3 new lines/n/n/n"

echo "OK"

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

$ ./echod
this echo's 3 new lines

OK
$

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

以下是代码:

$ echo -e "here is a tab/there are two tabs/t/tok"

here is a tab here are two tabs ok

$

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

以下是代码:

$ echo "The log files have all been done"> myfile

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

以下是代码:

$ 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”,那么我们只要在引号前面加上反斜杠/即可:

以下是代码:

$ echo "/"/dev/rmt0"/"

"/dev/rmt0"

$

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: