shell中函數的應用_linux shell

來源:互聯網
上載者:User

To turn the functions in this chapter into a library for use in other scripts, extract all the functions and concatenate them into one big file. If we call this file library.sh, a test script that accesses all of the functions might look like this:

#!/bin/sh# Library test script. library.shinitializeANSIechon "First off, do you have echo in your path? (1=yes, 2=no) "read answerwhile ! validint $answer 1 2 ; do echon "${boldon}Try again${boldoff}. Do you have echo " echon "in your path? (1=yes, 2=no) " read answerdoneif ! checkForCmdInPath "echo" ; then echo "Nope, can't find the echo command."else echo "The echo command is in the PATH."fiecho ""echon "Enter a year you think might be a leap year: "read yearwhile ! validint $year 1 9999 ; do echon "Please enter a year in the ${boldon}correct${boldoff} format: " read yeardoneif isLeapYear $year ; then echo "${greenf}You're right! $year was a leap year.${reset}"else echo "${redf}Nope, that's not a leap year.${reset}"fiexit 0

應用函數,我們就可以複用我們的指令碼。

值得注意的是 $ . tinyscript.sh ,就是在當前shell下執行指令碼,不加"."或source

則會在子shell下執行指令碼,可能會有不同的情況發生,值得注意。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.