10-4.shell編程.ppt_第1頁(yè)
10-4.shell編程.ppt_第2頁(yè)
10-4.shell編程.ppt_第3頁(yè)
10-4.shell編程.ppt_第4頁(yè)
10-4.shell編程.ppt_第5頁(yè)
已閱讀5頁(yè),還剩19頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Linux Shell編程,函數(shù),函數(shù)定義,在shell中還可以定義函數(shù)。函數(shù)實(shí)際上也是由若干條shell命令組成的,因此它與shell程序形式上是相似的,不同的是它不是一個(gè)單獨(dú)的進(jìn)程,而是shell程序的一部分。函數(shù)定義的基本格式為: function fname or fname 若干命令行 ,調(diào)用函數(shù)的格式為: fname param1 param2 shell函數(shù)可以完成某些例行的工作,而且還可以有自己的退出狀態(tài),因此函數(shù)也可以作為if、while等控制結(jié)構(gòu)的條件。 在函數(shù)定義時(shí)不用帶參數(shù)說(shuō)明,但在調(diào)用函數(shù)時(shí)可以帶有參數(shù),此時(shí)shell將把這些參數(shù)分別賦予相應(yīng)的位置參數(shù)$1、$2、.及

2、$*。,#!/bin/bashfunction fun1 echo this is the first functionfun1fun2() echo this is the second functionfun2 #bash test.sh this is the first functionthis is the second function,必須先定義,再調(diào)用,#!/bin/bashfunction fun1 echo this is the first functionfun1fun2fun2() echo this is the second function,函數(shù)名必須唯一,#!

3、/bin/bashfunction fun1 echo this is the first functionfun1fun1() echo this is the second functionfun1#bash test.sh this is the first functionthis is the second function,#!/bin/bash function hello () echo Hello,$1 today is date echo now going to the function hello hello kmust echo back from the funct

4、ion,#!/bin/bashfunction fun1 echo this is the first function ls -l xxfun1echo the exit status is :$? #bash test.sh this is the first functionls: xx: 沒(méi)有那個(gè)文件或目錄the exit status is :2 函數(shù)的退出狀態(tài)為2,因?yàn)樽詈笠粭l命令執(zhí)行出錯(cuò),#!/bin/bashfunction fun1 ls -l xx echo this is the first functionfun1echo the exit status is :$?

5、#bash test.sh ls: xx: 沒(méi)有那個(gè)文件或目錄this is the first functionthe exit status is :0 退出狀態(tài)值卻是0 ,因?yàn)樽詈笠粭l命令執(zhí)行無(wú)錯(cuò),return命令可以使用單個(gè)整數(shù)值來(lái)定義函數(shù)退出狀態(tài),#!/bin/bashfunction fun1 read -p enter a value: value echo doubling the value return $ $value * 2 fun1echo the new value is $? #bash test.sh enter a value:24doubling the v

6、aluethe new value is 48,#!/bin/bashfname () read -p “please input a value:” valueecho $ $value * 2 result=fname#反引號(hào)echo the result is : $result#bash test.sh please input a value:13the result is : 26,函數(shù)參數(shù),#!/bin/bashfunction fname if $# -eq 0 | $# -gt 2 then echo -1elif $# -eq 1 then echo $ $1 + $1 e

7、lse echo $ $1 * $2 fi,echo #1#echo -n adding 10 and 15 :value= fname 10 15echo $valueecho #2#echo -n adding just one num of 10:value= fname 10echo $valueecho #3#echo -n adding no num:value= fname echo $valueecho #4#echo -n adding three num 10 20 30 :value= fname 10 20 30echo $value,# bash test.sh #1

8、#adding 10 and 15 :150#2#adding just one num of 10:20#3#adding no num:-1#4#adding three num 10 20 30 :-1,#!/bin/bashfname () echo $ $1 + $2 if $# -eq 2 then value=fname #value=bad $1 $2 echo the result is : $valueelse echo please input two parametersfi #bash test.sh please input two parameters#bash

9、test.sh 1 2,全局變量,#!/bin/bashfun () value=$ $value * 2 read -p please input the value: value fun echo the new value is : $value,local-局部變量,#!/bin/bashfunction fun local temp=$ $value + 5 result=$ $temp + 2 temp=8value=10 fun echo the result is $resultif $temp -gt $value thenecho temp is largerelseech

10、o temp is smallerfi,創(chuàng)建庫(kù),funLib.sh function fun1 echo $ $1 + $2 function fun2 echo $ $1 * $2 test2.sh #!/bin/bash./funLib.shresult=fun1 12 24echo the result is $result,select,select 表達(dá)式是一種bash的擴(kuò)展應(yīng)用,尤其擅長(zhǎng)于交互式使用。用戶可以從一組不同的值中進(jìn)行選擇。 select var in . ; do break done,#!/bin/sh echo What is your favourite OS?

11、select var in Linux Gnu Hurd Free BSD Other; do break done echo You have selected $var,下面是該腳本運(yùn)行的結(jié)果: What is your favourite OS? 1) Linux 2) Gnu Hurd 3) Free BSD 4) Other #1 You have selected Linux,#!/bin/bash ftype=file $1 case $ftype in $1: Zip archive*) unzip $1 ; $1: gzip compressed*) gunzip $1 ; $1: bzip2 compressed*) bunzip2 $1 ; *) error File $1 can not be uncompressed with smartzip; esac,#!/bin/bashPrintHelp() echo FORMAT: command arg echo

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論