1

Is there a way to actually execute results from a shell command, instead of using them as arguments to another command?

For instance, I'd like to run '--version' on all executables in a folder, something like:

ls /usr/bin/ | --version

I've found a way using find/exec:

find /usr/ -name valgrind -exec {} --version \;

But I'd like to do it with ls. I've search for over 45 minutes and can't find any help.

2

Try doing this :

printf '%s\n' /usr/bin/* | while IFS= read -r cmd; do "$cmd" --version; done 
  • I'm getting a permission denied error, even when I run it as root :( – Jex_and_Izzy Oct 23 '16 at 23:00
  • @Jex_and_Izzy Then you have a file in /usr/bin that isn't executable, or that is a directory. – Gilles 'SO- stop being evil' Oct 24 '16 at 0:07
  • You are right Gilles, was wrong on my end. Thanks – Jex_and_Izzy Oct 24 '16 at 15:44
  • 1
    This is not using ls as requested. – user1133275 Oct 24 '16 at 17:32
  • @user1133275 and that is a good thing. Granted, probably won't be an issue in /usr/bin but you never know and, in any case, ls isn't the right too for the job. – terdon Dec 8 '16 at 23:38
2

If you're trying to execute every executable file in a certain directory with a --version option, try this one-liner (using /usr/bin as the example directory):

for f in /usr/bin/*; do [ -x "$f" ] && $f --version; done
  • This worked also. Thank you. I'd up-vote it but I don't have enough rep points :( – Jex_and_Izzy Oct 24 '16 at 16:11
1

perl and bash;

ls -1 | perl -pe 's/\n/ --version;\n/g' | bash

or xargs;

ls -1 | xargs -I {} bash {} --version;
  • $ ls -1 | xargs -I{} {} --version output is : xargs: {}: No such file or directory – Gilles Quenot Oct 23 '16 at 21:39
  • No, same thing hapenned – Gilles Quenot Oct 23 '16 at 21:41
  • The first suggestion works, although I receive a "bash: line 2: --version: command not found" error after each output – Jex_and_Izzy Oct 23 '16 at 23:02
  • @user1133275 First works, second still doesn't – Jex_and_Izzy Oct 24 '16 at 16:17
  • @Jex_and_Izzy I'd need a more specific example to reproduce and correct the issue you are having with the second one. – user1133275 Oct 24 '16 at 17:32
0

to reevaluate shell output as shell input, use eval.

eval "$(your command here)"
  • 1
    We should note that eval must be used with care, at least sanity check the eval input first (And welcome back @mikeserv). – cuonglm Oct 24 '16 at 2:14
  • @cuonglm - just stopping by. I have less time even than before for this stuff lately. I've been homeless since my house burnt down last month – mikeserv Oct 24 '16 at 10:03
  • 2
    Ops, I'm sorry about that, hope you will be better soon. – cuonglm Oct 24 '16 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.