bash in case of usage

We see some very practical script, due to the accumulation before the bash scripts are more discrete, not a comprehensive understanding about the record here:

The case statement usage bash

Grammar rules case statement is:

Case $ variable name in 
mode 1) command sequence ;; mode 2) command sequence 2 ;; *) default command sequence ;; esac

Note that, case comparison is the pattern, then since it is a wildcard, then:

  1. Remember Wildcard itself can not enclosed in quotation marks.
  2. As for whether the variable VAR can be enclosed in double quotes.
  3. Also keep in mind the difference between a wildcard (pattern) and a regular expression (regular expression) of.
  4. However, the use of matching pattern square brackets denote a continuous range, such as [0-9]; using the vertical bar "|" symbol represents or. 
    The last "*)" indicates the default mode, when using the previous models were unable to match the various variables, execute the command sequence "*)" after.

Examples of the case statement: a character from the keyboard input by the user, and determines whether the character is a letter, numbers or other characters, and outputs the appropriate message.

Copy the code
#!/bin/bash
read -p "press some key ,then press return :" KEY
case $KEY in
[a-z]|[A-Z])
echo "It's a letter."
;;
[0-9]) 
echo "It's a digit."
;;
*)
echo "It's function keys、Spacebar or other ksys."
esac
 

Word in Case [pattern [| pattern] ...) List ;;] ... esac
 Case / esac Standard usage is as follows: 
 Case $ Arg in 
     pattern | the Sample) # pattern or Arg in the Sample 
     ;; 
     pattern1) # Arg pattern1 in 
     ;; 
     *) #default 
     ;; 
 esac 
 arg is a parameter that you introduced, if arg pattern consistent with the content item, then it will execute the following code pattern, while the snippet places two semicolon "; ; "do at the end. 
 It may be noted "case" and "esac" is symmetric, if not remember the words, the "case" can be reversed. 

 

case statement is a wildcard matching, if not by double quotes after the wildcard, but by text processing.

After use double quotes wildcard entry into force is no longer just treated as ordinary characters.

Guess you like

Origin www.cnblogs.com/J1ac/p/10988210.html