简体   繁体   English

如何将Linux管道用于用户定义的Shell脚本

[英]How to use Linux pipe for Userdefined Shell Scripts

I have two user defined shell scripts: 我有两个用户定义的外壳脚本:

First is Add 首先是添加

if [ $# -eq 3 ]
then
    sum=`expr  $1 + $2 `
    echo $sum
else
    echo "usage :$0 num1 num2"
    echo "num1 and num2 are two numbers"
    exit 1
fi

The next is Square 接下来是广场

echo `expr $1 \* $1`

Can any one pleae tell me how to use Linux pipe for these shell scripts. 谁能告诉我如何对这些shell脚本使用Linux管道。 I tried something like: 我尝试了类似的东西:

add 10 20 | square                         

But it is giving me the list of files in that directory. 但这给了我该目录中文件的列表。

Using a pipe will pass the output from the first command to the stdin of the second command. 使用管道会将第一个命令的输出传递给第二个命令的标准输入。 You want the output to be used as arguments for the second command instead. 您希望将输出用作第二个命令的参数。 Try xargs: 试试xargs:

add 10 20 | xargs square

Of course you have to make sure that the output of the first command is just "10" in this case. 当然,在这种情况下,您必须确保第一个命令的输出仅为“ 10”。

A little more explanation: a pipe will take the output of the first command and redirect it into the standard input stream of the second command. 进一步说明:管道将获取第一个命令的输出,并将其重定向到第二个命令的标准输入流。 That means you will have to use a command like "read" (as some of the other answers do) to use the information from the input stream. 这意味着您将必须使用“读取”之类的命令(就像其他答案一样)才能使用输入流中的信息。

But your square script doesn't read anything from the standard input: it takes an argument instead. 但是,您的方形脚本不会从标准输入中读取任何内容:它需要一个参数。 So we want to take the output of your first command (10) and use it as the argument for your second command. 因此,我们要获取第一个命令(10)的输出并将其用作第二个命令的参数。 The "xargs" utility does exactly that: the standard input it receives will be passed as arguments to the square command. “ xargs”实用程序正是这样做的:它将收到的标准输入作为参数传递给square命令。 See https://en.wikipedia.org/wiki/Xargs . 参见https://en.wikipedia.org/wiki/Xargs

By the way, command substitution has the same effect: 顺便说一句,命令替换具有相同的效果:

square $(add 10 20)

The syntax $(add 10 20) will run the add script and replace the expression with its output. 语法$(add 10 20)将运行add脚本并将表达式替换为其输出。 So after running the add script the line looks like this: 因此,运行添加脚本后,该行如下所示:

square 30

And, in effect, we have again turned the output from add into an argument for square. 实际上,我们再次将输出从add转换为square的参数。

As written, you want to use command substitution, not a pipe (since square takes command line arguments, rather than reading from standard input): 如所写,您要使用命令替换,而不是管道(因为square命令行参数,而不是从标准输入中读取):

square $(add 10 20)

To modify square so that add 10 20 | square 修改square使add 10 20 | square add 10 20 | square works, use the read builtin: add 10 20 | square作品,请使用内置的read

#!/bin/bash

read input
echo $(( $input * $input ))   # No need for the external expr command

add should also write any error messages to standard error, not standard output: add还应该将任何错误消息写入标准错误,而不是标准输出:

if [ $# -eq 2 ]
then
    sum=$(( $1 + $2 ))
    echo $sum
else
    echo "usage :$0 num1 num2" >&2
    echo "num1 and num2 are two numbers" >&2
    exit 1
fi

You can use the "read" command to read the value from STDIN, if no parameters are specified: 如果未指定任何参数,则可以使用“ read”命令从STDIN读取值:

val="$1"
test -z "$1" && read val
echo `expr $val \* $val`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM