Shell Foundation (6): position parameter and case branch

Keywords: less shell

Position parameter

sign

$0: script file absolute Lujin
 $1-9: position parameter mark less than 10
 ${10..}: position parameter marker greater than 10
 $×: total number of parameters
 $*: expand to a list of location parameters starting at 1. When it's enclosed in double quotes
   When it gets up, it expands into a string enclosed by double quotation marks, containing
   All location parameters, each of which is the first one of the shell variable IFS
   Characters (a space by default) are separated.
$@: expand to a list of location parameters starting at 1. When it's enclosed in double quotes
   When it gets up, it expands each position parameter into a double quotation mark
   Separated string from.

shift: multi parameter list access one by one

usage
Each time the shift command executes, the value of variable $2 moves to variable $1,
The value of variable $3 moves to variable $2, and so on. The value of the variable $#, is also subtracted by 1.
Give an example
#!/bin/bash
# posit-param2: script to display all arguments
count=1
while [[ $# -gt 0 ]]; do
    echo "Argument $count = $1"
    count=$((count + 1))
    shift
done

case bifurcation

Basic grammar
case word in
    [pattern [| pattern]...) commands ;;]...
esac
pattern matching

Give an example
#!/bin/bash
read -p "enter word > "
case $REPLY in
    a|A) echo "is a or A." ;;
    [[:alpha:]]) echo "is a single alphabetic character." ;;
    [ABC][0-9]) echo "is A, B, or C followed by a digit." ;;
    ???) echo "is three characters long." ;;
    *.txt) echo "is a word ending in '.txt'" ;;
    *) echo "is something else." ;;
esac
Match multiple test conditions
#!/bin/bash
# case4-2: test a character
read -n 1 -p "Type a character > "
# ;;; & used to match multiple test conditions and continue to match the next one instead of exiting directly.
case $REPLY in
    [[:upper:]]) echo "'$REPLY' is upper case." ;;&
    [[:lower:]]) echo "'$REPLY' is lower case." ;;&
    [[:alpha:]]) echo "'$REPLY' is alphabetic." ;;&
    [[:digit:]]) echo "'$REPLY' is a digit." ;;&
    [[:graph:]]) echo "'$REPLY' is a visible character." ;;&
    [[:punct:]]) echo "'$REPLY' is a punctuation symbol." ;;&
    [[:space:]]) echo "'$REPLY' is a whitespace character." ;;&
    [[:xdigit:]]) echo "'$REPLY' is a hexadecimal digit." ;;&
esac

Posted by eatadi on Mon, 06 Jan 2020 01:51:07 -0800