排行榜 -

您的位置:首頁 > ppt下載 > PPT課件 > 課件PPT > shell腳本編程ppt

shell腳本編程ppt下載

素材預覽

shell腳本編程ppt

這是shell腳本編程ppt,包括了Shell腳本,Shell腳本舉例,條件測試,測試表達式的值,邏輯測試,檢查空值,for循環語句等內容,歡迎點擊下載。

shell腳本編程ppt是由紅軟PPT免費下載網推薦的一款課件PPT類型的PowerPoint.

Linux 操作系統 Shell 腳本編程 主要內容和學習要求 Shell 腳本 如果有一系列你經常使用的Linux命令,你可以把它們存儲在一個文件里,shell可以讀取這個文件并順序執行其中的命令,這樣的文件被稱為腳本文件。shell 腳本按行解釋。 Shell 腳本 Shell 腳本舉例 echo命令 功能說明:顯示文字。 語 法:echo [-ne][字符串] 或 echo [--help][--version] 補充說明:echo會將輸入的字符串送往標準輸出。輸出的字符串間以空白字符隔開, 并在最后加上換行號。 -n 不進行換行 -e 若字符串中出現以下字符,則特別加以處理,而不會將它當成一般文字輸出 \n 換行 \b 空格... 參 數: -n 不要在最后自動換行 -e 若字符串中出現以下字符,則特別加以處理,而不會將它當成一般文字輸出: \a 發出警告聲; \b 刪除前一個字符; \c 最后不加上換行符號; \f 換行但光標仍舊停留在原來的位置; \n 換行且光標移至行首; \r 光標移至行首,但不換行; \t 插入tab; \v 與\f相同; \\ 插入\字符; \nnn 插入nnn(八進制)所代表的ASCII字符; --help 顯示幫助 --version 顯示版本信息 Shell 腳本舉例 read命令 read variable #讀取變量給variable read x y #可同時讀取多個變量 read #自動讀給REPLY read –p “Please input: ” #自動讀給REPLY 條件測試 測試表達式的值 測試表達式的值 字符串測試 整數測試 整數測試 邏輯測試 邏輯測試 文件測試 檢查空值 if 條件語句 幾點說明 ex4if.sh #!/bin/bash # scriptname: ex4if.sh # echo -n "Please input x,y: " read x y echo "x=$x, y=$y" if (( x > y )); then echo "x is larger than y" elif (( x == y)); then echo "x is equal to y" else echo "x is less than y" fi chkperm.sh #!/bin/bash # Using the old style test command: [ ] # filename: perm_check.sh # file=testing if [ -d $file ] then echo "$file is a directory" elif [ -f $file ] then if [ -r $file -a -w $file -a -x $file ] then # nested if command echo "You have read,write,and execute permission on $file." fi else echo "$file is neither a file nor a directory. " fi chkperm2.sh #!/bin/bash # Using the new style test command: [[ ]] # filename: perm_check2.sh # file=./testing if [[ -d $file ]] then echo "$file is a directory" elif [[ -f $file ]] then if [[ -r $file && -w $file && -x $file ]] then # nested if command echo "You have read,write,and execute permission on $file." fi else echo "$file is neither a file nor a directory. " fi name_grep #!/bin/bash # filename: name_grep # name=Tom if grep "$name" /etc/passwd >& /dev/null then : else echo "$name not found in /etc/passwd" exit 2 fi tellme #!/bin/bash echo -n "How old are you? " read age if [ $age -lt 0 -o $age -gt 120 ] then echo "Welcome to our planet! " exit 1 fi if [ $age -ge 0 -a $age -le 12 ] then echo "Children is the flowers of the country" elif [ $age -gt 12 -a $age -le 19 ] then echo "Rebel without a cause" elif [ $age -gt 19 -a $age -le 29 ] then echo "You got the world by the tail!!" elif [ $age -ge 30 -a $age -le 39 ] then echo "Thirty something..." else echo "Sorry I asked" fi tellme2 #!/bin/bash echo -n "How old are you? " read age if (( age < 0 || age > 120 )) then echo "Welcome to our planet! " exit 1 fi if ((age >= 0 && age <= 12)) then echo "Children is the flowers of the country" elif ((age >= 13 && age <= 19 )) then echo "Rebel without a cause" elif (( age >= 19 && age <= 29 )) then echo "You got the world by the tail!!" elif (( age >= 30 && age <= 39 )) then echo "Thirty something..." else echo "Sorry I asked" fi idcheck.sh #!/bin/bash # Scriptname: idcheck.sh # purpose: check user id to see if user is root. # Only root has a uid of 0. # Format for id output: uid=501(tt) gid=501(tt) groups=501(tt) # root’s uid=0 : uid=0(root) gid=0(root) groups=0(root)… # id=`id | awk -F'[=(]' '{print $2}'` # get user id echo "your user id is: $id" if (( id == 0 )) # [ $id -eq 0 ] then echo "you are superuser." else echo "you are not superuser." fi case 選擇語句 幾點說明 yes_no.sh #!/bin/bash # test case # scriptname: yes_no.sh # echo -n "Do you wish to proceed [y/n]: " read ans case $ans in y|Y|yes|Yes) echo "yes is selected" ;; n|N|no|No) echo "no is selected" ;; *) echo "`basename $0`: Unknown response" exit 1 ;; esac for 循環語句 for 循環執行過程 forloop.sh #!/bin/bash # Scriptname: forloop.sh for name in Tom Dick Harry Joe do echo "Hi $name" done echo "out of loop" forloop2.sh #!/bin/bash # Scriptname: forloop2.sh for name in `cat namelist` do echo "Hi $name" done echo "out of loop" mybackup.sh #!/bin/bash # Scriptname: mybackup.sh # Purpose: Create backup files and store # them in a backup directory. # backup_dir=backup mkdir $backup_dir for file in *.sh do if [ -f $file ] then cp $file $backup_dir/${file}.bak echo "$file is backed up in $backup_dir" fi done greet.sh #!/bin/bash # Scriptname: greet.sh # usage: greet.sh Tom John Anndy echo "== using \$* ==" for name in $* # same as for name in [email protected] do echo Hi $name done echo "== using \[email protected] ==" for name in [email protected] # same as for name in $* do echo Hi $name done echo '== using "$*" ==' for name in "$*" do echo Hi $name done echo '== using "[email protected]" ==' for name in "[email protected]" do echo Hi $name done permx.sh #!/bin/bash # Scriptname: permx.sh # for file # Empty wordlist do if [[ -f $file && ! -x $file ]] then chmod +x $file echo " == $file now has execute permission" fi done while 循環語句 until 循環語句 break 和 continue months.sh #!/bin/bash # Scriptname: months.sh for month in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec do for week in 1 2 3 4 do echo -n "Processing the month of $month. OK? " read ans if [ "$ans" = n -o -z "$ans" ] then continue 2 else echo -n "Process week $week of $month? " read ans if [ "$ans" = n -o -z "$ans" ] then continue else echo "Now processing week $week of $month." sleep 1 # Commands go here echo "Done processing..." fi fi done done exit 和 sleep select 循環與菜單 runit.sh #!/bin/bash # Scriptname: runit.sh PS3="Select a program to execute: " select program in 'ls -F' pwd date do $program done select 與 case goodboy.sh #!/bin/bash # Scriptname: goodboys.sh PS3="Please choose one of the three boys : " select choice in tom dan guy #select choice do case $choice in tom) echo Tom is a cool dude! break;; # break out of the select loop dan | guy ) echo Dan and Guy are both wonderful. break;; *) echo "$REPLY is not one of your choices" echo "Try again." ;; esac done 循環控制 shift 命令 doit.sh #!/bin/bash # Name: doit.sh # Purpose: shift through command line arguments # Usage: doit.sh [args] while (( $# > 0 )) # or [ $# -gt 0 ] do echo $* shift done shft.sh #!/bin/bash # Using 'shift' to step through all the positional parameters. until [ -z "$1" ] # Until all parameters used up... do echo "$1" shift done echo # Extra line feed. exit 0 隨機數和 expr 命令 字符串操作 ex4str #!/bin/bash dirname="/usr/bin/local/bin"; echo "dirname=$dirname" echo -n '${#dirname}='; sleep 4;echo "${#dirname}" echo echo -n '${dirname:4}='; sleep 4;echo "${dirname:4}" echo echo -n '${dirname:8:6}='; sleep 4; echo ${dirname:8:6} echo echo -n '${dirname#*bin}='; sleep 4; echo ${dirname#*bin} echo echo -n '${dirname##*bin}='; sleep 4;echo ${dirname##*bin} echo echo -n '${dirname%bin}='; sleep 4;echo ${dirname%bin} echo echo -n '${dirname%%bin}='; sleep 4;echo ${dirname%%bin} echo echo -n '${dirname%bin*}='; sleep 4;echo ${dirname%bin*} echo echo -n '${dirname%%bin*}='; echo ${dirname%%bin*} echo echo -n '${dirname/bin/sbin}='; echo ${dirname/bin/sbin} echo echo -n '${dirname//bin/lib}='; echo ${dirname//bin/lib} echo echo -n '${dirname/bin*/lib}='; echo ${dirname/bin*/lib} 腳本調試 編程小結:變量 編程小結:輸入輸出 編程小結:條件測試 編程小結:條件測試 編程小結:條件測試 編程小結:條件測試 編程小結:控制結構 函數 函數舉例 函數的調用 ex4fun2.sh #!/bin/bash JUST_A_SECOND=1 fun () { # A somewhat more complex function i=0 REPEATS=5 echo echo "And now the fun really begins." echo sleep $JUST_A_SECOND # Hey, wait a second! while [ $i -lt $REPEATS ] do echo "----------FUNCTIONS---------->" echo "<------------ARE-------------" echo "<------------FUN------------>" echo let "i+=1" done } # Now, call the functions. fun exit 0 ex4fun3.sh # f1 # Will give an error message, since function "f1" not yet defined. # declare -f f1 # This doesn't help either. # f1 # Still an error message. # However... f1 () { echo "Calling function \"f2\" from within function \"f1\"." f2 } f2 () { echo "Function \"f2\"." } # f1 # Function "f2" is not actually called until this point # although it is referenced before its definition. # This is permissible. 函數的調用 向函數傳遞參數 例:ex4fun4.sh #!/bin/bash # Functions and parameters DEFAULT=default # Default param value. func2 () { if [ -z "$1" ] # Is parameter #1 zero length? then echo "-Parameter #1 is zero length -" else echo "-Param #1 is \"$1\" -" fi variable=${1:-$DEFAULT} echo "variable = $variable" if [ -n "$2" ] then echo "- Parameter #2 is \"$2\" -" fi return 0 } echo echo "Nothing passed" func2 # Called with no params echo echo "One parameter passed." func2 first # Called with one param echo echo "Two parameters passed." func2 first second # Called with two params echo echo "\"\" \"second\" passed." func2 "" second # The first parameter is of zero?length echo exit 0 # End of script 函數與命令行參數 例:ex4fun5.sh #!/bin/bash # function and command line arguments # Call this script with a command line argument, # something like $0 arg1. func () { echo "$1" } echo "First call to function: no arg passed." echo "See if command-line arg is seen." Func # No! Command-line arg not seen. echo "==================================" echo echo "Second call to function: command-line arg passed explicitly." func $1 # Now it's seen! exit 0 return 與 exit 例:ex4fun6.sh #!/bin/bash # purpose: Maximum of two integers. max2 () # Returns larger of two numbers. {if [ -z $2 ] then echo "Need to pass two parameters to the function." exit 1 fi if [[ $1 == $2 ]] # [ $1 -eq $2 ] then echo "The two numbers are equal." exit 0 else if [ $1 -gt $2 ] then return $1 else return $2 fi fi } read num1 num2 echo "num1=$num1, num2=$num2" max2 $num1 $num2 return_val=$? echo "The larger of the two numbers is: $return_val." exit 0

c單片機編程ppt:這是c單片機編程ppt,包括了C51與標準C的差異,C語言與匯編語言,存儲模式,使用C51擴展關鍵字等內容,歡迎點擊下載。

mfc編程ppt:這是mfc編程ppt,包括了MFC ODBC數據庫概述,MFC ODBC編程,MFC ODBC向導過程,ODBC應用編程等內容,歡迎點擊下載。

可編程序控制器ppt:這是可編程序控制器ppt,包括了可編程控制器的結構和基本工作原理,PLC的內部寄存器及I/O配置,PLC的編程語言概述,OMRON公司可編程控制器的程序等內容,歡迎點擊下載。

推薦PPT

PPT分類Classification

Copyright:2009-2015 rsdown.cn Corporation,All Rights Reserved 紅軟PPT免費下載網版權所有

粵ICP備14102101號

湖北十一选五走势图app下载 广西11选5开奖结果查询 北京11选5任五号码推荐 广西11选5走势 广东快乐十分新规则 那只股票最近会大涨 今天湖北30选5开奖结果 洋河股份股票分析 贵州快三开奖结果查询今天500 配资被骗 不立案 一定牛陕西十一选五