Bash Scripting Basics: Parameter Expansion

๐Ÿ’ก Bash Scripting Basics: Parameter Expansion โ€” Executive Summary

Technical architectural analysis, practical configuration manifests, and deployment instructions for Bash Scripting Basics: Parameter Expansion.

References and Guidelines (NOTE)
Example
bash
#!/bin/bash
# My First Script Program
 
printf "I love UNIX !\n"
pwd

exit 0
Script component
  1. ! (Magic number)

  2. Located at the beginning of the script file
  3. Specify the program to run the script
  4. Can be used in commands other than the shell. ex) #!/bin/more
  5. # (comment)
  6. Shell command
  7. Can contain commands executable in the shell.
  8. Shell program โ†’ described below
Shell Program syntax elements
  1. Shell variables
  2. Specification method: varname = value
  3. Variable expression

    format function Result simulation
    $name Replace with the value of name
    $(name) Replace with the value of name. Used when a variable name is adjacent to another statement
    $(name:-word) If name is defined, use that value. Or use word if name==null { null }
    if name==undefined { โ€˜wordโ€™ }
    if hasVal(name) { name }
    $(name:=word) If name is undefined or null, substitute word and use its value. if name==null { โ€˜wordโ€™ }
    if name==undefined { โ€˜wordโ€™ }
    if hasVal(name) { name }
    $(name:+word) If name is defined and its value is not null, word is used. if name==null { null }
    if name==undefined { undefined }
    if hasVal(name) { โ€˜wordโ€™ }
    $(name:?word) If name is defined and the value is not null, use that value. Otherwise, print word and end. if name==null
    { print(โ€˜wordโ€™) and exit() }
    if name==undefined
    { print(โ€˜wordโ€™) and exit() }
    if hasVal(name)
    { name }
  4. Variable string processing โ€“ If the value is a string, an expression that finds a pattern within the string and removes part of it.

    format function
    ${variable%pattern} Find and remove the first part that matches the pattern from the end of the value in the variable
    ${variable%%pattern} Find and remove the largest part that matches the pattern from the end of the value in the variable
    ${variable#pattern} Find and remove the first part matching the pattern from the beginning of the value in the variable
    ${variable##pattern} Find and remove the largest part that matches the pattern from the beginning of the value in the variable
  5. Parameter processing
  6. Values and parameters given as arguments when running a script
  7. Positional parameters

    command line arguments meaning
    $0 name of shell script
    $1 - $9 The first to ninth arguments given when executing a command.
    $(10) 10th argument
    $# Total number of arguments
    $ All arguments โ†’ \$1 \$2 \$3
    $@ Same as \$*
    โ€œ$โ€ All arguments โ†’ โ€œ\$1 \$2 \$3โ€
    โ€œ$@โ€ All arguments โ†’ โ€œ\$1โ€ โ€œ\$2โ€ โ€œ\$3โ€
    $? Exit value of the most recently executed command
  8. Executive syntax
  9. Special character handling

    quotation marks function How to use
    Single quote (โ€™ โ€˜) Prevents all special characters from being interpreted echo โ€˜$testโ€™
    $test
    Double quotes (โ€ โ€œ) Only substitution of variables or commands is allowed echo โ€œ$testโ€
    100
    Backslash (\) Prevents single characters from being interpreted echo \$test
    $test
  10. Returns the command execution result as a value (string)
    | symbol | How to use |
    | โ€” | โ€” |
    | back quarter (` `) | echo `date` |
    | \$(command) | echo \$(date) |
  11. Input Stream
  12. Static input โ€“ input from a terminal or file, single line
    1. Format

      format Use
      read x Receives one line from standard input and stores it in variable \$x
      read first last Receives a line from standard input and stores the first word in the variable \$first and all the rest in the variable \$last.
      read -p prompt Prints a prompt and waits for input. The entered value is stored in variable \$REPLY
    2. Example of use
bash
#!/bin/bash
         # ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ์ฒ˜๋ฆฌ๋ฅผ ํ…Œ์ŠคํŠธ ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ 
          
         read   x                   # ์•„๋ฌด ๋ฉ”์‹œ์ง€ ์—†์ด ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๊ธฐ๋‹ค๋ฆผ 
         echo  "x : $x"             # ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜์˜ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ์ถœ๋ ฅ 
          
         read  x   y                # ์ฒซ ๋‹จ์–ด๋Š” x, ๋‚˜๋จธ์ง€๋Š” y์— ์ €์žฅ
         echo "x is $x  y is $y"    # x, y ๊ฐ’ ์ถœ๋ ฅ 
          
         read  -p โ€œInput : โ€œ        # Input : ์„ ์ถœ๋ ฅํ•œ ํ›„ ์ž…๋ ฅ ๊ธฐ๋‹ค๋ฆผ
         echo "input : $REPLY"      # $REPLY์— ์ž๋™ ์ €์žฅ๋œ ์ž…๋ ฅ๊ฐ’ ์ถœ๋ ฅ
  1. Dynamic input โ€“ receiving input as a statement, multiple lines
    1. Format
      command << TERMINATOR
      Input contents
      TERMINATOR

      TERMINATOR is a name chosen arbitrarily and EOF (End Of File) is often used.
    2. Example of use
bash
#!/bin/bash
          
         # ์ฃผ์„
         << EOF
         AAAAAA
         BBBBBB
         CCCCCC
         EOF

         # here ๋ฌธ์„œ ํ…Œ์ŠคํŠธ. user1์—๊ฒŒ ๋ฉ”์ผ์„ ๋ณด๋ƒ„ 
         mail user1 << END 
         This is a test mail for here document 
         END
  1. Operator

    operator meaning Example usage
    - negative number -x
    ! Logical negation (NOT) (( ! x < y ))
    ~ Bit inversion (NOT) ~y
    * / % Multiplication, division, remainder let y=3 * 5
    + - addition, subtraction let x=x+1
    << >> Bit left shift, bit right shift (( y = x << 3 ))
    <= >= < > == != comparison operation (( x < y ))
    & ^ | Bitwise AND, XOR, OR let โ€œz = x ^ yโ€
    && || Logical AND, OR (( x
    = Specify variable value let z=1
    *= /= %= += == <<= >>= &= ^= |= shortcut operation let z+=1
  2. Control statements

  3. Conditional Test – String Operator
    | string operators | Action |
    | — | — |
    | str = pattern
    str == pattern
    | str matches pattern. Space on both sides of the = operator |
    | str != pattern | str does not match pattern |
    | str | str is not null |
    | -z str | str length is 0 |
    | -n str | str length is not zero |
    | -l str | Returns the length of str (not a conditional expression) |
  4. Condition Test – Flags

    test flag action
    -a file file exists
    -e file file exists
    -L file symbolic link file
    -O file User is the owner of the file
    -G file Group ID in file is same as group ID in script
    -S file socket file
    -r file readable
    -w file writable
    -x file executable
    -b file block device special file
    -c file character device special file
    -d file directory file
    -p file pipe file
    -u file setuid authorization file
    -g file setgid authorization file
    -k file sticky bit access permission file
    -s file Size of file is not zero
    1. conditional statement – if
      bash
      #!/bin/bash
         #  test_elif: if-elif ๋ฌธ ํ…Œ์ŠคํŠธ 
              
         echo  โ€œInput Score : โ€œ
         read score
          
         if (( $score >= 90 )); then 
            echo  โ€œYour score is great. โ€œ
         elif (( $score >= 80 )); then 
            echo  โ€œYour score is good. โ€œ
         else 
            echo โ€œYour score is not good. โ€œ 
         fi
  5. Conditional statement – case

bash
#!/bin/bash
      # case ํ…Œ์ŠคํŠธ ์Šคํฌ๋ฆฝํŠธ
      : <<EOF
      ์ฃผ์–ด์ง„ ๋ณ€์ˆ˜์˜ ๊ฐ’์— ๋”ฐ๋ผ ์‹คํ–‰ํ•  ๋ช…๋ น ๋”ฐ๋กœ ์ง€์ •
      ๋ณ€์ˆ˜์˜ ๊ฐ’์ด value1 ์ด๋ฉด value1๋ถ€ํ„ฐ ;;์„ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ๋ช…๋ น ์‹คํ–‰
      ๊ฐ’์˜ ์ง€์ •์— ํŠน์ˆ˜๊ธฐํ˜ธ, | (or ์—ฐ์‚ฐ์ž) ์‚ฌ์šฉ ๊ฐ€๋Šฅ
      ์ผ์น˜ํ•˜๋Š” ๊ฐ’์ด ์—†์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์ธ * ๋ถ€ํ„ฐ ์‹คํ–‰
      EOF
       
      echo "Select command :"
      read  cmd
       
      case $cmd in
      [0-9])                # 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ž„์˜์˜ ์ˆซ์ž
         date
         ;;
      "cd" | "CD")        # cd ๋˜๋Š” CD
         echo $HOME
         ;;
      [aA-C]*)             # ์†Œ๋ฌธ์ž a, ๋Œ€๋ฌธ์ž A,B,C๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ž„์˜์˜ ๋ฌธ์ž์—ด
         pwd
         ;;
      *)
         echo" Usage : select command"
         ;;
      esac
  1. Repeat statement – for
bash
#!/bin/bash
      #  test_for: for ํ…Œ์ŠคํŠธ ์Šคํฌ๋ฆฝํŠธ 
      : <<EOF
      list ๋‚ด ๋ชจ๋“  ์—˜๋ ˆ๋จผํŠธ๋ฅผ ๋ณ€์ˆ˜๋กœ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ๋ฐ˜๋ณต (forEach)
      list ๋Œ€์‹  $(<file)์„ ์‚ฌ์šฉํ•˜๋ฉด ์™ธ๋ถ€ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.
      EOF
       
      for num in 0 1 2 
      do 
         echo "Number is $num" 
      done
  1. Repeat statement – while
bash
#!/bin/bash 
      # test_while: while์„ ์ด์šฉํ•ด 1๋ถ€ํ„ฐ 10๊นŒ์ง€ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ 
      : <<EOF
      ์กฐ๊ฑด์ด true์ธ ๋™์•ˆ ๋ช…๋ น ๋ฐ˜๋ณต
      EOF

      count=1                    
      sum=0 
       
      while (( count <= 10 ))     
      do 
           (( sum += count ))       
           let  count+=1             
      done 
       
      echo "Sum(1~10) : $sum"
  1. Repeat statement – until
bash
#!/bin/bash 
      # test_until: ์ง€์ •ํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธํ•˜๋ฉด ์•Œ๋ฆฌ๋Š” ์Šคํฌ๋ฆฝํŠธ 
      : <<EOF
      #์กฐ๊ฑด์ด true๊ฐ€ ๋ ๋•Œ๊นŒ์ง€ ๋ช…๋ น ๋ฐ˜๋ณต
      EOF

      echo  "Input name :"   # ์ž…๋ ฅ ์•ˆ๋‚ด๋ฌธ ์ถœ๋ ฅ
      read  person                  # ์œ ์ € ์ด๋ฆ„์„ person์— ์ €์žฅ 
       
      until  who | grep $person   # > /dev/null 
      do 
          sleep  5                 # ์œ ์ €๊ฐ€ ์ ‘์† ์ค‘์ด ์•„๋‹ˆ๋ฉด 5์ดˆ ์‰ผ 
      done
  1. Repeat statement – select
bash
#!/bin/bash 
      # test_select: ์‚ฌ์šฉ์ž ์ž…๋ ฅ์— ๋”ฐ๋ผ pwd,date ๋ช…๋ น์‹คํ–‰ 
      : <<EOF
      ๋ฉ”๋‰ด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ณต ์‹คํ–‰๋ฌธ์ด๋‹ค.
      list์— ์ง€์ •ํ•œ ํ•ญ๋ชฉ์„ ์ž๋™์œผ๋กœ ์„ ํƒ ๊ฐ€๋Šฅํ•œ ๋ฉ”๋‰ด๋กœ ๋งŒ๋“ค์–ด ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด์ค€๋‹ค.
      ์‚ฌ์šฉ์ž๋Š” ๊ฐ ํ•ญ๋ชฉ์— ์ž๋™์œผ๋กœ ๋ถ€์—ฌ๋œ ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•œ๋‹ค.
      ์‚ฌ์šฉ์ž ์ž…๋ ฅ์€ select์™€ in ์‚ฌ์ด์— ์ง€์ •๋œ ๋ณ€์ˆ˜์— ์ €์žฅ
      ๋ณดํ†ต case ๋ฌธ๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ์ž…๋ ฅ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
      EOF

      PS3="Input command(1~3) " 
       
      select cmd in pwd date quit 
      do                           
         case $cmd in             
         pwd) 
              pwd ;; 
         date) 
              date;; 
         quit) 
              break;;               
         *) 
              echo "Invalid input. select number" 
              ;; 
         esac 
         REPLY= 
      done
  1. Repeat control statement – continue
bash
#!/bin/bash
      #  test_cont: continue ํ…Œ์ŠคํŠธ 
      : << EOF
      ๋ฃจํ”„ ์•ˆ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค.
      ์ดํ›„ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ๋ฃจํ”„์˜ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ€๋Š” ๋ช…๋ น
      ์ˆซ์ž๋ฅผ ์ง€์ •ํ•˜๋ฉด ์ค‘์ฒฉ๋œ ๋ฃจํ”„ ์•ˆ์—์„œ ํŠน์ • ๋ฃจํ”„์˜ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.
      EOF
       
      for person in $(< list)          # `cat list`์™€ ๋™์ผ 
      do 
          if [[ $person == user2 ]] then 
             continue                    # user2 ์ด๋ฉด ๊ฑด๋„ˆ๋œ€
          fi 
          echo โ€œHello, $personโ€
      done
  1. Function
  2. Declaration
bash
# typeset -f ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์‹œ์Šคํ…œ์— ๋“ฑ๋ก๋œ ํ•จ์ˆ˜๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

      function {{ ํ•จ์ˆ˜๋ช… }}
      {
      	...
      }
  1. End function – return
bash
#!/bin/bash
      #  ํ•จ์ˆ˜ ๋ฆฌํ„ด๊ฐ’ ํ…Œ์ŠคํŠธ 
       
      function sum { 
         typeset  sum 
       
         (( sum= $1 + $2 ))
         return $sum 
      } 
       
      sum $1 $2 
      echo "$1 + $2 = $?"

      # ํ•จ์ˆ˜ ๋ณธ๋ฌธ ์•ˆ์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์žฅ ์‹คํ–‰
      # return ๋ฌธ ์‹คํ–‰
      # +) ์ง€์ •ํ•œ ๊ฐ’์ด ํ•จ์ˆ˜์˜ ์ข…๋ฃŒ๊ฐ’์œผ๋กœ $?์— ์ €์žฅ๋œ๋‹ค.

      # ๊ฒฐ๊ณผ : 
      # bash {{ filename }} 3 4
      # 3 + 4 = 7
  1. Delete function – unset
bash
unset -f { ํ•จ์ˆ˜๋ช… }
  1. Debugging
    1. bash -x
      bash
      # ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์˜ค๋ฅ˜ ์ˆ˜์ •๋ฐฉ๋ฒ•์ด๋‹ค.
         # ์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ ํ–‰์ด ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ํ™”๋ฉด์— ์ถœ๋ ฅ๋œ๋‹ค.
         bash -x { ํ•จ์ˆ˜๋ช… }
  1. trap
    bash
    #!/bin/bash 
       # test_trap: trap ํ…Œ์ŠคํŠธ ์Šคํฌ๋ฆฝํŠธ 
        
       trap  'echo "$LINENO : count=$count " '  DEBUG
        
       count=1                    
       sum=0 
        
       while (( count <= 10 ))     
       do 
            (( sum += count ))       
            let  count+=1             
       done 
        
       echo "Sum(1~10) : $sum "
    
       # * ์ง€์ •ํ•œ ์‹œ๊ทธ๋„์ด ์Šคํฌ๋ฆฝํŠธ๋กœ ์ „๋‹ฌ๋  ๋•Œ๋งˆ๋‹ค ์ง€์ •ํ•œ ๋ช…๋ น ์‹คํ–‰
       # * ์Šคํฌ๋ฆฝํŠธ์˜ ๋ช…๋ น์ด ํ•œ ์ค„์”ฉ ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค DEBUG ์‹œ๊ทธ๋„์ด ์Šคํฌ๋ฆฝํŠธ๋กœ ์ „๋‹ฌ๋œ๋‹ค.
       # * DEBUG ์‹œ๊ทธ๋„์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ์›ํ•˜๋Š” ๋ณ€์ˆ˜ ๊ฐ’ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.   
       # => ์ฆ‰. ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋„์ค‘ ๋ณ€์ˆ˜๊ฐ’ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *