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    房屋貸款done}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
    全站熱搜

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