linux command read the content,shell基础:输入和输出(echo,read,cat,管道,tee,重定向等)

本文以Redhat 9.0和Redhat EL AS 4.0为实验环境,详细介绍了Linux中echo、read、cat、tee等常用命令的使用方法,包括输出转义符、读取用户输入、显示文件内容等。同时,还讲解了管道和文件重定向的概念及常用组合,如将标准输出重定向到文件等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验环境:Redhat 9.0,Redhat EL AS 4.0

echo:

-------------------------------------------------------------------------

在LINUX中,要使转义符生效,需加参数-e从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

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

$

把一个字符串输出到文件中,使用重定向符号>。

在下面的例子中一个字符串被重定向到一个名为myfile的文件中:

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

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

$ echo "$LOGNAME carried them out at `date`">>myfile

现在让我们看一下myfile文件中的内容:

The log files have all been done

sam carried them out at 六 11月 13 12:54:32 CST 2004

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

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

"/dev/rmt0"

$

read:

------------------------------------------------------------------------------

[sam@chenwy sam]$ vi var_test

#!/bin/sh

#var_test

echo -e "First Name :\c"

read first

echo -e "Middle Name :\c"

read middle

echo -e "Last name :\c"

read last

echo $first $middle $last

[root@first ~]# read a b c d

1 2 3 4

[root@first ~]# echo $a

1

[root@first ~]# echo $b

2

[root@first ~]# echo $c

3

[root@first ~]# echo $d

4

[root@first ~]# echo $a $b $c $d

1 2 3 4

这是在"man bash"中的一段:

read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...]

One  line  is  read  from  the  standard input, or from the file descriptor fd supplied as an argument to the -u option, and  the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their  intervening  separators  assigned  to the last name.  If there are fewer words read from the input stream than names, the remaining names are  assigned  empty  values.  The characters in IFS are used to split the line into words.  The backslash character (\)  may  be used  to  remove any special meaning for the next character read and for line continuation.  Options, if supplied, have the  following meanings:

-a aname

The words are assigned to sequential indices of the array variable aname, starting at 0.  aname is unset before any new  values  are  assigned.   Other  name  arguments  are ignored.

-d delim

The first character of delim is  used  to  terminate  the input line, rather than newline.

-e     If the standard input is coming from a terminal, readline (see READLINE above) is used to obtain the line.

-n nchars

read returns after reading nchars characters rather  than waiting for a complete line of input.

-p prompt

Display prompt on standard error, without a trailing newline, before attempting to read any input.  The prompt is displayed only if input is coming from a terminal.

-r     Backslash does not act as an escape character.  The backslash is considered to be part of the line.  In  particular,  a  backslash-newline pair may not be used as a line continuation.

-s     Silent mode.  If input is coming from a terminal, characters are not echoed.

-t timeout

Cause  read  to time out and return failure if a complete line of input is not read within timeout  seconds.  This option  has  no  effect if read is not reading input from the terminal or a pipe.

-u fd  Read input from file descriptor fd.

If no names are supplied, the line read is assigned to the ariable  REPLY.   The  return  code  is zero, unless end-of-file is ncountered, read times out, or an invalid  file  descriptor  is supplied as the argument to -u.

read -p "how old r u? " age

echo $age

read -p "some words? " -a words

echo ${words[*]}

read -p "Password: " -s passwd

echo $passwd

read -t 5 auth

echo $auth

read -n 1 key

read -dq -p "input something end with q: " menu

read -e file #在这试试命令历史和补齐功能

cat:

--------------------------------------------------------------------

cat:显示文件内容,创建文件,还可以用它来显示控制字符。注意:在文件分页符处不会停下来;会一下显示完整个文件。因此,可以使用more命令或把pg命令的输出通过管道传递到另外一个具有分页功能的命令中,使用命令less file可实现相同的功能。

如下形式

$ cat myfile | more 或 $ cat myfile | pg

cat命令的一般形式为:

cat [options] filename1 ... filename2 ...

1、显示名为myfile的文件:

$ cat myfile

2、显示myfile1、myfile2、myfile3这三个文件,可以用:

$ cat myfile1 myfile2 myfile3

3、创建一个包含上述三个文件的内容,名为bigfile的文件,可以用输出重定向到新文件中:

$ cat myfile1 myfile2 myfile3 > bigfile

4、如果cat的命令行中没有参数,输入的每一行都立刻被cat命令输出到屏幕上,输入完毕后按结束

$ cat

Hello world

Hello world

$

5、新建文件

$cat >myfile

This is great

$cat myfile

This is great

cat:参数选项使用方式:

cat [-AbeEnstTuv] [--help] [--version] fileName

说明:把档案串连接后传到基本输出(萤幕或加 > fileName 到另一个档案)

-n 或 --number 由 1 开始对所有输出的行数编号

-b 或 --number-nonblank 和 -n 相似,只不过对于空白行不编号

-s 或 --squeeze-blank 当遇到有连续两行以上的空白行,就代换为一行的空白行

-v 或 --show-nonprinting 显示非打印字符

cat 还可以在您查看包含如制表符这样的非打印字符的文件时起帮助作用。您可以用以下选项来显示制表符:

* -T 将制表符显示为 ^I

* -v 显示非打印字符,除了换行符和制表符,它们使用各自效果相当的“控制序列”。例如,当您处理一个在 Windows 系统中生成的文件时,这个文件将使用 Control-M(^M)来标记行的结束。对于代码大于 127 的字符,它们的前面将会被加上 M-(表示“meta”),这与其它系统中在字符前面加上 Alt- 相当。

* -E 在每一行的结束处添加美元符($)。

tee:

------------------------------------------------------------------

tee:读取标准输入的数据,并将其内容输出成文件。

语   法:tee [-ai][--help][--version][文件…]

补充说明:tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。我们可利用tee把管道导入的数据存成文件,甚至一次保存数份文件。

参   数:-a 附加到既有文件的面,而非覆盖它。如果给予tee指令的文件名称已经存在,预设会覆盖该文件的内容。加上此参数,数据会新增在该文件内容的最面,而不会删除原先之内容。

-i 忽略中断信号

--help 在线帮助

--version 显示版本信息

列出文本文件slayers.story的内容,同时复制3份副本,文件名称分别为ss-copy1、ss-copy2、ss-copy3:

$ cat slayers.story |tee ss-copy1 ss-copy2 ss-copy3

把列出当前目录,并把结果结到myfile里

$ ls -l |tee myfile

管道:可以通过管道把一个命令的输出传递给另一个命令作为输入。管道用竖杠|表示。它的一般形式为

命令1 |命令2

其中|是管道符号。

标准输入、输出和错误当我们在shell中执行命令的时候,每个进程都和三个打开的文件相联系,并使用文件描述符来引用这些文件。由于文件描述符不容易记忆,shell同时也给出了相应的文件名。

下面就是这些文件描述符及它们通常所对应的文件名:

文件文件描述符

输入文件—标准输入0:它是命令的输入,缺省是键盘,也可以是文件或其他命令的输出。

输出文件—标准输出1:它是命令的输出,缺省是屏幕,也可以是文件。

错误输出文件—标准错误2:这是命令错误的输出,缺省是屏幕,同样也可以是文件。

在执行命令时,可以指定命令的标准输入、输出和错误,要实现这一点就需要使用文件重定向。下面列出了最常用的重定向组合,并给出了相应的文件描述符。

在对标准错误进行重定向时,必须要使用文件描述符,但是对于标准输入和输出来说,这不是必需的。

常用文件重定向命令:

command > filename 把标准输出重定向到一个新文件中

command >> filename 把标准输出重定向到一个文件中(追加)

command 1 > fielname 把标准输出重定向到一个文件中

command > filename 2>&1 把标准输出和标准错误一起重定向到一个文件中

command 2 > filename 把标准错误重定向到一个文件中

command 2 >> filename 把标准输出重定向到一个文件中(追加)

command >> filename 2>&1 把标准输出和标准错误一起重定向到一个文件中(追加)

command < filename >filename2 把command命令以filename文件作为标准输入,以filename 2文件

作为标准输出

command < filename 把command命令以filename文件作为标准输入

command << delimiter 把从标准输入中读入,直至遇到d e l i m i t e r分界符

command command >&m 把把标准输出重定向到文件描述符m中

command

阅读(1695) | 评论(0) | 转发(0) |

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值