linux的shell腳本實驗報告_第1頁
linux的shell腳本實驗報告_第2頁
linux的shell腳本實驗報告_第3頁
linux的shell腳本實驗報告_第4頁
linux的shell腳本實驗報告_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)第二次實驗內(nèi)容一、實驗名稱:Linux下shell編程二、實驗類型:設(shè)計三、實驗?zāi)康模菏煜inux的shell幾種變量使用熟練掌握Linux的shell編程幾種結(jié)構(gòu)熟練掌握Linux下shell腳本的編寫四、實驗準備參考教材,課件第7章內(nèi)容及筆記。要求實驗內(nèi)容全部寫到實驗報告上(B5紙)。五、實驗內(nèi)容練習(xí)使用shell四種變量,參考課件例題。用戶自定義變量,環(huán)境變量,位置變量,特殊變量這四種變量類型的使用,書中有例題。調(diào)試課件所有shell腳本的例題。編寫如下腳本:編

2、寫腳本if1,測試其功能。 echo -n word 1: read word1echo -n word 2: read word2if test $word1 = $word2 then echo Matchfiecho End of program.編寫腳本chkargs,測試其功能if test $# -eq 0 then echo You must supply at least one argument. exit 1fiecho Program running.編寫腳本if2,測試其功能if test $# -eq 0 then echo You must supply at le

3、ast one argument. exit 1fiif test -f $1 then echo $1 is a regular file in the working directory else echo $1 is NOT a regular file in the working directoryfi編寫腳本if3,測試其功能echo -n word 1: read word1echo -n word 2: read word2echo -n word 3: read word3if $word1 = $word2 -a $word2 = $word3 then echo Matc

4、h: words 1, 2, & 3 elif $word1 = $word2 then echo Match: words 1 & 2 elif $word1 = $word3 then echo Match: words 1 & 3 elif $word2 = $word3 then echo Match: words 2 & 3 else echo No matchfi編寫smartzip 腳本,測試其功能#!/bin/bashftype=file $1case $ftype in$1: Zip archive*)unzip $1 ;$1: gzip compressed*)gunzip

5、 $1 ;$1: bzip2 compressed*)bunzip2 $1 ;*) echo File $1 can not be uncompressed with smartzip;esac編寫腳本dirfiles,測試其功能。for i in *do if -d $i then echo $i fidone編寫腳本until1,測試其功能。用while改寫之。secretname=jennyname=nonameecho Try to guess the secret name!echountil $name = $secretname /while改寫位 while “$name” !

6、= “$secretname” ,其他地方不變do echo -n Your guess: read namedoneecho Very good.編寫腳本brk,測試其功能。for index in 1 2 3 4 5 6 7 8 9 10 do if $index -le 3 ; then echo continue continue fi# echo $index# if $index -ge 8 ; then echo break break fidone編寫腳本command_menu,測試其功能。echo -e n COMMAND MENUnecho a. Current date

7、 and timeecho b. Users currently logged inecho c. Name of the working directoryecho -e d. Contents of the working directorynecho -n Enter a, b, c, or d: read answerechocase $answer in a) date; b) who; c) pwd; d) ls; *) echo There is no selection: $answer;esac編寫腳本demo_shift,測試其功能。echo arg1= $1 arg2=

8、$2 arg3= $3shiftecho arg1= $1 arg2= $2 arg3= $3shiftecho arg1= $1 arg2= $2 arg3= $3shiftecho arg1= $1 arg2= $2 arg3= $3shift編寫shell腳本sum1,求命令行上整數(shù)和。即:$./sum1 5 12 4 6,給出和的結(jié)果。sum=0for i in $*do let sum=sum+idoneecho “和是:$sum”編寫腳本filetest,判斷當前目錄下所有文件類型,如果是普通文件,顯示文件內(nèi)容;如果是目錄文件,顯示目錄列表;如果是大小為0的文件,刪除它;否則,顯示

9、“sorry, The file is not recognized!”for i in *do if -d $i then ls $ielif -f $i then if -s $i then cat $ielse rm $i fielse echo n “sorry,the file cant be recognized”fidone編寫shell腳本user,判斷當前登錄用戶是否為“學(xué)號命名”的用戶,是,提示:hello “學(xué)號用戶”,welcome!,否,提示“you should login using your username! ”Read nameIf $USER = $nam

10、e Then echo “hello $USER”else echo “you should login using your username!”fi編寫shell腳本menu,使用shell編寫一個菜單,分別實現(xiàn)列出以下內(nèi)容:(1)顯示目錄內(nèi)容、(2)切換目錄、(3)創(chuàng)建文件、(4)編輯文件、(5)刪除文件的功能。在此例中將用到循環(huán)語句、分支語句和輸入輸出語句。Echo “a.display the directory”Echo “b.change the directory”Echo “c.create a file”Echo “d.delete the file”Echo “if yo

11、u input nothing,you will exit”Read itemUntil -z $item Do Case $item in Echo “input the directory”Read direLs $dire;Echo “input the directory you want go into”Read direCd $dire;Echo “input the file you want to create”Read fTouch $f;Echo “input the file you want to delete”Read fRm $f;EsacEcho “a.displ

12、ay the directory”Echo “b.change the directory”Echo “c.create a file”Echo “d.delete the file”Echo “if you input nothing,you will exit”Read itemdone編寫腳本,實現(xiàn)一個簡單計算器。+ addition- subtractionx multiplication/ division腳本執(zhí)行形式:$ ./cal.sh 21 / 3Let l=$1 /最簡單的一種形式,而且還特別高效Echo $1;第二種方式:Re=”+ - * /”For var in $reDo If $var = “*” ThenNum2=$1#* Num1=$1%* Else Num2=$1#*$var Num1=$1

溫馨提示

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

評論

0/150

提交評論