-1

I want to write a bash script that:

  1. Runs the command bux
    • a) If the output of bux contains have, do nothing
    • b) If the output of bux contains X, run the command Y
    • c) If the output of bux contains Z, run the command A
  • It will only contain one of these things, not multiple x
retep
  • 317
  • 2
  • 13
  • 2
    Have you tried anything yet? SO isn't a script writing service, though we're very willing to help with problems along the way. You might also want to look at http://stackoverflow.com/a/16263820/4687135 – Eric Renouf Jan 06 '16 at 13:19

2 Answers2

1

Here is a script that should do what you want (provided that bux, Y and A are bash scripts) :

#!/bin/bash
OUTPUT=`source bux`
if [[ "$OUTPUT" =~ have ]]; then
   :
elif [[ "$OUTPUT" =~ X ]]; then
   source Y  
elif [[ "$OUTPUT" =~ Z ]]; then
   source A
fi

If you want to execute programs instead (provided that bux, Y and A are in the path) :

#!/bin/bash
OUTPUT=`bux`
if [[ "$OUTPUT" =~ have ]]; then
    :
elif [[ "$OUTPUT" =~ X ]]; then
    Y
elif [[ "$OUTPUT" =~ Z ]]; then
    A
fi  
Bruno STEUX
  • 121
  • 4
1

glob patterns in a case statement like this:

case $(bux) in
    *have*)
        echo 'do nothing?'
        ;;
    *X*)
        Y
        ;;
    *Z*)
        A
        ;;
    *)
        echo 'default case.'  # display an error ...?
        ;;
esac

Obviously the patterns could be more complex if you like, but this seems to cover your requirements.

tripleee
  • 175,061
  • 34
  • 275
  • 318
zM_
  • 21
  • 4
  • It should be `${bux}` or `$bux` (or, even better, `"$bux"`), but not `$(bux)`. Also, the patterns aren't regular expressions, just the "normal" pattern language of Bash, i.e., `*X*` instead of `.*X.*` – Benjamin W. Jan 06 '16 at 14:55
  • @BenjaminW. The OP needs to run the *command* `bux` and inspect its output so `$(bux)` is correct. – Etan Reisner Jan 06 '16 at 15:12
  • @EtanReisner Ooops, brain fart! The pattern point stands, though ;) – Benjamin W. Jan 06 '16 at 15:47
  • I took the liberty to fix the answer in accordance with the comments above. Feel free to revert if you feel this is too intrusive; but the original answer didn't solve the OP's problem. – tripleee Jan 07 '16 at 09:31
  • Oh ! You're (obviously) right for the regex pattern, i'm too used to Perl style. Thanks for correcting that ;) – zM_ Jan 07 '16 at 16:48