syntax:ccmd:case

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
syntax:ccmd:case [2012/05/17 12:18]
ormaaj Misc cleanup. Add example.
syntax:ccmd:case [2013/02/01 13:27]
ormaaj [Examples] A practical example to show off my video power function.
Line 36: Line 36:
 Another one of my stupid examples... Another one of my stupid examples...
 <​code>​ <​code>​
-read -rp "Which fruit do you like most? " ​fruit+printf '%s ' 'Which fruit do you like most?
 +read -${BASH_VERSION+e}r ​fruit
  
-case "$fruit" ​in +case $fruit in 
-  apple) ​             echo "Mmmmh... I like those!" +    apple) 
-                      ;; +        ​echo 'Mmmmh... I like those!' 
-  banana) ​            ​echo "Hm, a bit awry, no?" +        ;; 
-                      ;; +    banana) 
-  orange|tangerine) ​  ​echo "Eeeks! I don't like those!+        ​echo 'Hm, a bit awry, no?' 
-                      echo "​Go ​away!" +        ;; 
-                      exit 1 +    orange|tangerine) 
-                      ;; +        ​echo $'Eeeks! I don\'t like those!\nGo away!' 
-  *)                  echo "​Unknown fruit - sure it isn't toxic?"​ +        exit 1 
-                      ;;+        ;; 
 +    *) 
 +        ​echo "​Unknown fruit - sure it isn't toxic?"​
 esac esac
 +</​code>​
 +
 +Here's a practical example showing a common pattern involving a ''​case''​ statement. If the first argument is one of a valid set of alternatives,​ then perform some sysfs operations under Linux to control a video card's power profile. Otherwise, show a usage synopsis, and print the current power profile and GPU temperature.
 +<code bash>
 +# Set radeon power management
 +function clk {
 + typeset base=/​sys/​class/​drm/​card0/​device
 + [[ -r ${base}/​hwmon/​hwmon0/​temp1_input && -r ${base}/​power_profile ]] || return 1
 +
 + case $1 in
 + low|high|default)
 + printf '​%s\n'​ "temp: $(<​${base}/​hwmon/​hwmon0/​temp1_input)C"​ "old profile: $(<​${base}/​power_profile)"​
 + echo "​$1"​ >​${base}/​power_profile
 + echo "new profile: $(<​${base}/​power_profile)"​
 + ;;
 + *)
 + echo "​Usage:​ $FUNCNAME [ low | high | default ]"
 + printf '​%s\n'​ "temp: $(<​${base}/​hwmon/​hwmon0/​temp1_input)C"​ "​current profile: $(<​${base}/​power_profile)"​
 + esac
 +}
 </​code>​ </​code>​
  
 A template for experiments with ''​case''​ logic, showing shared code between blocks using '';&'',​ and the non-short-circuiting '';;&''​ operator: A template for experiments with ''​case''​ logic, showing shared code between blocks using '';&'',​ and the non-short-circuiting '';;&''​ operator:
-<​code>​+<​code ​bash>
 #​!/​usr/​bin/​env bash #​!/​usr/​bin/​env bash
  
Line 89: Line 112:
 ===== Portability considerations ===== ===== Portability considerations =====
  
-  * Only the '';;''​ delimiter is specified by POSIX+  * Only the '';;''​ delimiter is specified by POSIX.
   * zsh and mksh use the '';​|''​ control operator instead of Bash's '';;&''​. Mksh has '';;&''​ for Bash compatability (undocumented).   * zsh and mksh use the '';​|''​ control operator instead of Bash's '';;&''​. Mksh has '';;&''​ for Bash compatability (undocumented).
-  * ksh93 has the '';&''​ operator, but not '';;&''​. +  * ksh93 has the '';&''​ operator, but no '';;&'' ​or equivalent
 +  * ksh93, mksh, zsh, and posh support a historical syntax where open and close braces may be used in place of ''​in''​ and ''​esac'':​ ''​case word { x) ...; };''​. This is similar to the alternate form Bash supports for its [[syntax/​ccmd/​classic_for | for loops]], but Bash doesn'​t support this syntax for ''​case..esac''​.
 ===== See also ===== ===== See also =====
-  * [[POSIX case conditional construct|http://​pubs.opengroup.org/​onlinepubs/​9699919799/​utilities/​V3_chap02.html#​tag_18_09_04_05]]+  * [[http://​pubs.opengroup.org/​onlinepubs/​9699919799/​utilities/​V3_chap02.html#​tag_18_09_04_05|POSIX case conditional construct]]
  
  • syntax/ccmd/case.txt
  • Last modified: 2017/02/08 15:04
  • by fgrose