Say I have a list.txt
of files I want to check.
cat list.txt
and for each file, I want to perform an action
cat list.txt | xargs -I {} touch {}
But depending on the outcome of the action, I want to react accordingly:
cat list.txt | xargs -I {} touch {} | if [-z - ]; then echo "blank file"; fi;
This doesn't work properly, and I'm not sure why not...
So, How can I properly test the value of stdout
-?-
- and, as a bonus- how could I pass the contents of {}
to the final portion? To the effect of:
cat list.txt | xargs -I {} touch {} | if [-z - ]; then echo "{} is a blank file"; fi;
Perhaps this should be a complete bash script, but I would prefer if I could learn to do this in one line, since the applications for this could be useful in many ways --
Here is a more practical example:
time john -incremental -stdout | grep 'clientsWeakPassword' | if [ ! -z - ]; then exit 0;
So, this script would randomly generate passwords, and stop when it hits the password specified, displaying the amount of time it took to 'crack' that password...
And ideally, it would also output something like "'clientsWeakPassword' took Xminutes to crack using random guessing" --
But, this chain could also be used to check for a certain thing in log files, etc, etc, etc -
At any rate, I hope someone can point me in the right direction.
Many thanks -
cat list.txt | xargs ...
, but it requires one or more file accesses to findcat
, another to open a pipe, then possibly swapping out a group of pages to free memory forcat
, reading through the entire file to copy it into memory for execution, and the overheads of closing the process and pipe afterwards, and eventually swapping back into memory the pages thatcat
displaced.xargs <list.txt ...
.cat list.txt | ...
, in order to isolate the recipient program from your input file. And I take your point that if you are doing a one-off command, efficiency doesn't matter (though in my case, the amount of typing does!).cat something > some_file
-- so, it is quite helpful to know -- that is a habit I should break if I wish to improve in bash - so Thank you -!