0

I haven't done much bash scripting in the past (this is probably only my fourth or fifth script I have created). The current script i'm making will repeat a command (the script's argument) until it completes without failing. But before it starts repeating the command I want to check that it's an actual command. How could I check this? Edit: Here is the current code I have written (not much)

    #!/bin/bash

    while true; do #This while loop keeps repeating the first argument (which should be a command) until it finishes without failing
    if ($1); then
      echo "success"
    break;
    else
      echo "fail"
      echo;
    fi;

done;
Sol33t303
  • 105
  • 1
  • 7
  • 1
    What exactly are these commands? External programs? Bash functions / builtins / aliases? Shell expressions you want to `eval`? A concrete example (or the code you've written so far) would be helpful. – dimo414 Jul 23 '18 at 07:25
  • @dimo414 I added my code to the question. I was planning on probably using mostly for external programs, but it would be good to be able to to use it with bash builtins and aliases as well. Here is an example of how it might be used (the script is called repeat) repeat 'youtube-dl "(some-url)"' (that command might not look right because of the markdown code syntax the markdown code syntax) – Sol33t303 Jul 23 '18 at 10:47
  • If your question isn't answered by any of the answers in the duplicate, you need to clarify how your case is different from those cases. – tripleee Jul 23 '18 at 10:49
  • @tripleee Yeah I know, I haven't looked at the duplicate yet because I figured I would reply to dimo414 first :) – Sol33t303 Jul 23 '18 at 10:51
  • Your current code should probably be simplified to `while ! "$@"; do : nothing; done`. Notice in particular how `"$@"` includes an arbitrary number of arguments and preserves their quoting. – tripleee Jul 23 '18 at 10:51
  • As an aside, diagnostic messages should usually go to standard error; including the name of the command which produced the message is a useful convention which facilitates troubleshooting especially once you start building complex scripts on top of simpler scripts. `echo "$0: success" >&2` – tripleee Jul 23 '18 at 10:55
  • Thanks for the advice, I'm currently on my phone but I will change the script when I get back to my pc. – Sol33t303 Jul 23 '18 at 11:01

0 Answers0