简体   繁体   中英

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?

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.

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

AFAIK there is no such way.

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:

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 .

Otherwise, just use the name as suggested by mbratch , since the pipeline with mail will not run until streamer completes successfully:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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