Skip to main content
improve legibility
Source Link
Stéphane Chazelas
  • 591.8k
  • 97
  • 1.1k
  • 1.7k

As in bash -c 'case a in (a) echo a;;& (b) echo b; esac' orbash's

case a in
  (a) echo a;;&
  (b) echo b
esac

Or zsh's zsh/mksh -c 'case a in (a) echo a;| (b) echo b; esac' won't/ mksh's

case a in
  (a) echo a;|
  (b) echo b
esac

Won't output both a and b like a switch('a'){case 'a': puts("a"); case 'b': puts("b");}

switch('a') {
  case 'a': puts("a"); /* fall through */
  case 'b': puts("b");
}

would in C.

As in bash -c 'case a in (a) echo a;;& (b) echo b; esac' or zsh/mksh -c 'case a in (a) echo a;| (b) echo b; esac' won't output both a and b like a switch('a'){case 'a': puts("a"); case 'b': puts("b");} would in C.

As in bash's

case a in
  (a) echo a;;&
  (b) echo b
esac

Or zsh's / mksh's

case a in
  (a) echo a;|
  (b) echo b
esac

Won't output both a and b like a

switch('a') {
  case 'a': puts("a"); /* fall through */
  case 'b': puts("b");
}

would in C.

added 1187 characters in body
Source Link
Stéphane Chazelas
  • 591.8k
  • 97
  • 1.1k
  • 1.7k

With the use-case you've now added:

case $5 in
  (C | C++)
     export CXXFLAGS="${CXXFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
esac
case $5 in
  (C) export CFLAGS="${CFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}";;
  (C++) ;;
  (*) export FLAGS_USER="${FLAGS_USER} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
esac

Or:

c_or_cxx=false
case $5 in
  (C) export CFLAGS="${CFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
      c_or_cxx=true;;
  (C++) c_or_cxx=true;;
  (*) export FLAGS_USER="${FLAGS_USER} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
esac
if "$c_or_cxx"; then
   export CXXFLAGS="${CXXFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
fi

You can also include the common code in a function or variable to eval (variable may be preferable here as the code includes references to $1):

for_c_and_cxx='
  export CXXFLAGS="${CXXFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
'
case $5 in
  (C)
    export CFLAGS="${CFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
    eval "$for_c_and_cxx";;
  (C++)
    eval "$for_c_and_cxx";;
  (*)
    export FLAGS_USER="${FLAGS_USER} {1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
esac

Note that it's only the Bourne shell that required you omitted the opening (s (the Bourne shell also did not support export var=value and in any case was not a POSIX compliant shell). In modern/standard sh implementations, you can include it which IMO makes for more readable code and lets you use your editors ability to check for matching parenthesis for instance.

With the use-case you've now added:

case $5 in
  (C | C++)
     export CXXFLAGS="${CXXFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
esac
case $5 in
  (C) export CFLAGS="${CFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}";;
  (C++) ;;
  (*) export FLAGS_USER="${FLAGS_USER} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
esac

Or:

c_or_cxx=false
case $5 in
  (C) export CFLAGS="${CFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
      c_or_cxx=true;;
  (C++) c_or_cxx=true;;
  (*) export FLAGS_USER="${FLAGS_USER} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
esac
if "$c_or_cxx"; then
   export CXXFLAGS="${CXXFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
fi

You can also include the common code in a function or variable to eval (variable may be preferable here as the code includes references to $1):

for_c_and_cxx='
  export CXXFLAGS="${CXXFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
'
case $5 in
  (C)
    export CFLAGS="${CFLAGS} ${1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
    eval "$for_c_and_cxx";;
  (C++)
    eval "$for_c_and_cxx";;
  (*)
    export FLAGS_USER="${FLAGS_USER} {1}${SUSUWU_DEPENDENCY_INCLUDE_PATH}"
esac

Note that it's only the Bourne shell that required you omitted the opening (s (the Bourne shell also did not support export var=value and in any case was not a POSIX compliant shell). In modern/standard sh implementations, you can include it which IMO makes for more readable code and lets you use your editors ability to check for matching parenthesis for instance.

added 95 characters in body
Source Link
Stéphane Chazelas
  • 591.8k
  • 97
  • 1.1k
  • 1.7k

For that, you'd need ;& instead (itself from ksh93 and found in all of bash, zsh and mkshmksh; while ;; and the whole case...esac structure is from the Bourne shell from the late 70s).

For that, you'd need ;& instead (itself from ksh93 and found in all of bash, zsh and mksh).

For that, you'd need ;& instead (itself from ksh93 and found in all of bash, zsh and mksh; while ;; and the whole case...esac structure is from the Bourne shell from the late 70s).

added 612 characters in body
Source Link
Stéphane Chazelas
  • 591.8k
  • 97
  • 1.1k
  • 1.7k
Loading
edited body
Source Link
Stéphane Chazelas
  • 591.8k
  • 97
  • 1.1k
  • 1.7k
Loading
Source Link
Stéphane Chazelas
  • 591.8k
  • 97
  • 1.1k
  • 1.7k
Loading