简体   繁体   English

单个bash行中的嵌套命令

[英]Nested commands in single bash line

Using bash, how do I send the output file from one command to be used as a variable in the second command, in one line? 使用bash,如何在一行中将一个命令的输出文件发送为第二个命令的变量?

I'm trying to use streamer and mutt to take a picture using my webcam and then email it to an email address. 我正在尝试使用流光和杂色使用网络摄像头拍照,然后将其通过电子邮件发送到电子邮件地址。 I've got as far as the below, but not sure how to tell mutt to use the file created in the previous (streamer) command. 我已经到了以下内容,但是不确定如何告诉mutt使用上一个(streamer)命令中创建的文件。

streamer -o image.jpg && echo "email body" | mutt -a (file from previous command) -s Subject -- recipient@email.com

AFAIK there is no such way. AFAIK没有这种方式。

What you could do is the following 您可以做的是以下几点

OUTPUT_FILE="image.jpg"; streamer -o ${OUTPUT_FILE} && echo "email body" | mutt -a ${OUTPUT_FILE} -s Subject -- recipient@email.com

In your example the output file name is even static, so you could just hardcode it. 在您的示例中,输出文件名甚至是静态的,因此您可以对其进行硬编码。

If I understand how streamer works, you can use process substitution: 如果我了解streamer工作原理,则可以使用流程替换:

echo "email body" | mail -a <( streamer )

The shell provides a file name for the -a option which mail can then read to access the standard output of streamer . 该外壳程序为-a选项提供了一个文件名,然后可以读取mail以访问streamer的标准输出。

Otherwise, just use the name as suggested by mbratch , since the pipeline with mail will not run until streamer completes successfully: 否则,只需使用mbratch所建议的mbratch ,因为带有邮件的管道将在streamer成功完成之前不会运行:

streamer -o image.jpg && echo "email body" | mail -a image.jpg

or, using some knowledge about the command line to avoid typing image.jpg twice, use 或者,使用一些有关命令行的知识以避免两次输入image.jpg ,请使用

streamer -o image.jpg && echo "email body" | mail -a !#:2

to reuse the 3rd word (counting from 0) of the current command line to grab the name of the file streamer uses for output. 重用当前命令行的第三个单词(从0开始计数)以获取streamer用于输出的文件名。

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

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