Sheel Script Programming Functions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.