0

Here is some pseudo-code for what I'm trying to achieve with a bash program.

This program will be called with either:

filesplit -u OR filesplit --update

filesplit

 if -u (or --update) is set:

   echo "Update"

 else:

   echo "No Update

How can this split be achieved in a bash script?

datavoredan
  • 3,536
  • 9
  • 32
  • 48
  • possible duplicate of [Script parameters in Bash](http://stackoverflow.com/questions/18003370/script-parameters-in-bash) – LinkBerest Jul 25 '15 at 15:34

3 Answers3

2

You can check for both values using @(val1|val2) syntax inside [[...]]:

filesplit() {
    [[ "${1?needs an argument}" == -@(u|-update) ]] && echo "Update" || echo "No Update"
}

Testing:

filesplit -b
No Update
filesplit -u
Update
filesplit --update
Update
filesplit --update1
No Update
filesplit
-bash: 1: needs an argument
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    `extglob` is only needed for using extended patterns when doing filename generation. Extended patterns are assumed inside `[[...]]`. – chepner Jul 25 '15 at 16:56
2

Just use logical or || in a [[ ... ]] condition:

if [[ $1 == -u || $1 == --update ]] ; then
    echo Update
else
    echo No Update
fi
choroba
  • 231,213
  • 25
  • 204
  • 289
1
case "$1" in
  -u|--update)
      echo update
      ;;
  *)
      echo no update
      ;;
esac
Cyrus
  • 84,225
  • 14
  • 89
  • 153