close
Shell Scripts(4) - 函式函式中可以使用local關鍵字來宣告區域變數,那這個變數只在函式之內有效,且如果和全域變數同名,就會蓋過它(只在函式內)。下面是語法:function_name(){   statements}範例:#!/bin/sh# This 當鋪is the fourth shell scripttext="global variable"func(){    local text="local variable"    echo "func::$text"}yes_or_no(){    echo 澎湖民宿"Is your name $* ?"    while true    do        echo -n "Enter yes or no: "        read 節能燈具x        case "$x" in            y | yes ) return 0;;            n | no )  支票借款return 1;;            * )          echo "Answer yes or no"        esac    591done}echo ------- scope of variable ------- funcecho $textecho ------- return from function -------if [ -z "$1" ]then    echo "Please input the parameter after 吳哥窟./fourth.sh"    exit 1fi if yes_or_no "$1"then    echo "Hi $1, nice name"else    echo "Never mind"fiexit 0執行:建構一個sh名為房屋二胎fourth.sh,然後執行 ./fourth.sh[root@hughes ~]# ./fourth.sh myname------- scope of variable -------func::local variableglobal variable------- return from function -------Is your name myname ?Enter yes or no: 婚禮顧問yHi myname, nice name說明:先定義一個全域變數text,再定義兩個函式func、yes_or_no。在func裡又定義一個區域變數text,由func的執行結果可看出,local關鍵字宣告的text,只在func裡有效。在第二段程式裡,先檢查外部shell是裝潢否有傳進來參數,有的話再把它傳給函式yes_or_no,yes_or_no接收到參數後印出來,接著判斷使用者輸入回傳1或0。然後第二段程式依據回傳值顯示結果。


.msgcontent .wsharing ul li { text-indent: 0; }



分享
負債整合
Facebook
Plurk
YAHOO!

arrow
arrow
    全站熱搜

    re61recfmn 發表在 痞客邦 留言(0) 人氣()