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 $@ do echo Hi $name done echo "== using \$@ ==" for name in $@ # same as for name in $* do echo Hi $name done echo '== using "$*" ==' for name in "$*" do echo Hi $name done echo '== using "$@" ==' for name in "$@" 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 029k红软基地

展开

同类推荐

热门PPT

相关PPT