Bash forwarding combined with dual channel designation

Is it possible to combine redirection of output to a file and pipes with ||? (Not sure what it's called)

Example:

(wget -qO- example.com/duff || exit) | some_processing >> outfile.txt 

If wget fails, I would like to exit and not run some_processing or create an empty file.

+4
source share
5 answers
 #!/bin/bash RESULT=`wget -qO- example.com/duff` if [ $? -eq 0 ]; then echo $RESULT | some_processing >> outfile.txt fi 
+3
source

|| is logical or. Without creating outfile.txt , a much more complex syntax is required; as you wrote it (in the usual way), outfile.txt is created before wget starts.

I canโ€™t honestly think about how to make the phone not happen at all if the left command does not work. I would just write a Perl script if I were you.

+3
source

([$? -eq 0] & some_processing โ†’ outfile.txt) <<$ (wget -qO- example.com/duff)

+1
source

I think you pretty much answer how to do this.

If wget fails, I would like to exit and not run some_processing or create an empty file.


wget wget -qO- example.com/duff

if [[$? -ne 0]] // $? means return value, on well-written programs it should be zero

then

 some_processing >> outfile.txt 

c


I think this will accomplish what you want.

0
source

Please note that it may not be enough to check the return code. it would also be nice to check the status of the HTTP return, e.g. 404 ..... etc.

0
source

All Articles