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,136
  • 6
  • 29
  • 45
  • 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
  • 713,503
  • 59
  • 514
  • 593
  • 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
  • 216,930
  • 22
  • 195
  • 267
1
case "$1" in
  -u|--update)
      echo update
      ;;
  *)
      echo no update
      ;;
esac
Cyrus
  • 77,979
  • 13
  • 71
  • 125