Sheel Script Programming Functions
First, let's take a look at how to obtain the return values of the two functions:
#! /Bin/bash # function return value acquisition test. Function fun_test () {# No other print statement is allowed in the function. Echo 100} val = $ (fun_test) echo $ val
Another type:
#! /Bin/bash # function return value acquisition test. Function fun_test () {returned 100} fun_testval = $? Echo $ val
#! /Bin/bash # create multiple non-existing directories under the current directory. Function create_dir () {mkdir. /$1>/dev/null} while [$ #-gt 0]; do a = 'ls | grep "$1" 'if ["$ a" = "$1"]; then echo "has exit !!! "Exit 0 else create_dir $1 fi shift 1 done
#/Bin/bash # recursive solution 1 +... 100sum = 0 function fun_test () {if [$1-gt 0]; then set-x let sum + = $1 set + x fun_test $($1-1 )) else echo $ sum fi} val = $ (fun_test 100) echo $ val
#! /Bin/bash # print all directory files in the specified directory. Function is_dir () {local dir = $1 if [-d $ {dir}]; then return 0 else return 1 fi} function ls_dir () {local dir = $1 arr = ('ls $ {dir} ') # create an array. Cd $ dir # Jump to the directory. For (I = 0; I <$ {# arr [@]}; I ++) {# traverse the array, which is important here. If [-d $ {arr [I]}]; then echo "this is a dir:" $ {arr [I]} else echo "this is't a dir: "$ {arr [I]} fi} cd-# Return to the main process directory .} Proc = $ (basename $0) proc = 'basename $ 0' function usage () {printf "% s \ n" "usage :"". /$ {proc} dir "# If the parameter is not input, the error message is returned to the user .} If [$ #-ne 1]; then usage exit 1fils_dir $1
#! /Bin/bash # print a dynamic progress bar without changing the color. Arr = ("|" "/" "-" "\") function pro () {local I = 0 str = ''while [$ I-le 100] do let index = I % 4 printf" [% s] [% d %] [% c] \ r "" $ str "$ I" $ {arr [index]} "sleep 0.2 let I ++ str =$ {str} # done} pro