linux脚本重定向end,Linux Shell 脚本:输入 / 输出重定向

You can use the here document to print multiple lines using your script as follows −

#!/bin/sh

cat << EOF

This is a simple lookup program

for good (and bad) restaurants

in Cape Town.

EOF

Upon execution, you will receive the following result −

This is a simple lookup program

for good (and bad) restaurants

in Cape Town.

The following script runs a session with the vi text editor and saves the input in the file test.txt.

#!/bin/sh

filename=test.txt

vi $filename <

i

This file was created automatically from

a shell script

^[

ZZ

EndOfCommands

If you run this script with vim acting as vi, then you will likely see output like the following −

$ sh test.sh

Vim: Warning: Input is not from a terminal

$

After running the script, you should see the following added to the file test.txt −

$ cat test.txt

This file was created automatically from

a shell script

$

Discard the output

Sometimes you will need to execute a command, but you don't want the output displayed on the screen. In such cases, you can discard the output by redirecting it to the file /dev/null −

$ command > /dev/null

Here command is the name of the command you want to execute. The file /dev/null is a special file that automatically discards all its input.

To discard both output of a command and its error output, use standard redirection to redirect STDERR to STDOUT −

$ command > /dev/null 2>&1

Here 2 represents STDERR and 1 represents STDOUT. You can display a message on to STDERR by redirecting STDOUT into STDERR as follows −

$ echo message 1>&2

我来翻译

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值