b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

shell脚本编程(完结版).pdf(3)

电脑杂谈  发布时间:2019-07-29 07:11:04  来源:网络整理

最 #终$*所#代表的全部命令行参数将为空,循环结束。 8. weekday # 输入数字,输出该数字对应的是星期几#!/bin/bashif [ $# -lt 1 ]thenecho Usage:$0 数字exitficase $1 in1)echo monday;;2)echo tuesday;;3)echo wednesday;;4)echo thursday;;5)echo friday;;6)echo saturday;;7)echo sunday;;*)echo invalidate;esac 9. greetings #!/bin/bash # This is the first Bash shell program # ScriptName: greetingsechoecho –e "Hello $LOGNAME, \c"如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论16Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦echo"it's nice talking to you."echo"Your present working directory is:"pwd # Show the name of present directoryechoecho –e "The time is `date +%T`!. \nBye"echo 10. ex4read # !/bin/bash # This script is to test the usage of read # Scriptname: ex4readecho "=== examples for testing read ==="echo -e "What is your name? \c"read nameecho "Hello $name"echoecho -n "Where do you work? "readecho "I guess $REPLY keeps you busy!"echoread -p "Enter your job title: "# 自动读给REPLYecho "I thought you might be an $REPLY."echoecho "=== End of the script ===" # read variable : 读取变量给variable # read x y : 可同时读取多个变量 # read : 自动读给REPLY # read –p “Please input: ” 自动读给REPLY 11. ex4if #!/bin/bash # scriptname: ex4ifecho -n "Please input x,y: "read x yecho "x=$x, y=$y"if (( x > y )); thenecho "x is larger than y"elif (( x == y)); thenecho "x is equal to y"elseecho "x is less than y"fi如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论17Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦 12. chkperm #!/bin/bash # Using the old style test command: [ ] # filename: chkpermfile=testingif [ -d $file ]thenecho "$file is a directory"elif [ -f $file ]thenif [ -r $file -a -w $file -a -x $file ]then# nested if commandecho "You have read,write,and execute permission on $file."fielseecho "$file is neither a file nor a directory. "fi 13. chkperm2 #!/bin/bash # Using the new style test command: [[ ]] # filename: chkperm2file=./testingif [[ -d $file ]]thenecho "$file is a directory"elif [[ -f $file ]]thenif [[ -r $file && -w $file && -x $file ]]then# nested if commandecho "You have read,write,and execute permission on $file."fielseecho "$file is neither a file nor a directory. "fi 14. name_grep #!/bin/bash # filename: name_grepname=Tomif grep "$name" /etc/passwd >& /dev/null如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论18Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦then:elseecho "$name not found in /etc/passwd"exit 2fi 15. tellme #!/bin/bashecho -n "How old are you? "read ageif [ $age -lt 0 -o $age -gt 120 ]thenecho "Welcome to our planet! "exit 1fiif [ $age -ge 0 -a $age -le 12 ]thenecho "Children is the flowers of the country"elif [ $age -gt 12 -a $age -le 19 ]thenecho "Rebel without a cause"elif [ $age -gt 19 -a$age -le 29 ]thenecho "You got the world by the tail!!"elif [ $age -ge30 -a $age -le 39 ]thenecho "Thirty something..."elseecho "Sorry I asked"fi 16. tellme2 #!/bin/bashecho -n "How old are you? "read ageif (( age < 0 || age > 120 ))thenecho "Welcome to our planet! "exit 1fiif ((age >= 0 && age <= 12))then如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论19Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦echo "Children is the flowers of the country"elif ((age >= 13 && age <= 19 ))thenecho "Rebel without a cause"elif (( age >= 19 &&age <=29 ))thenecho "You got the world by the tail!!"elif (( age >=30 &&age <= 39 ))thenecho "Thirty something..."elseecho "Sorry I asked"fi 17. 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 idecho "your user id is: $id"if (( id == 0 ))# [ $id -eq 0 ]thenecho "you are superuser."elseecho "you are not superuser."fi 18. yes_no.sh #!/bin/bash # test case # scriptname: yes_no.shecho -n "Do you wish to proceed [y/n]: "read anscase $ans iny|Y|yes|Yes)echo "yes is selected";;n|N|no|No)echo "no is selected"如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论20Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦;;*)echo "`basename $0`: Unknown response"exit 1;;esac 19. forloop.sh #!/bin/bash # Scriptname: forloop.shfor name in Tom Dick Harry Joedoecho "Hi $name"doneecho "out of loop" 20. forloop2.sh #!/bin/bash # Scriptname: forloop2.shfor name in `cat namelist`doecho "Hi $name"doneecho "out of loop" 21. mybackup.sh #!/bin/bash # Scriptname: mybackup.sh # Purpose: Create backup files and store # them in a backup directory. #backup_dir=backupmkdir $backup_dirfor file in *.shdoif [ -f $file ]thencp $file $backup_dir/${file}.bakecho "$file is backed up in $backup_dir"fidone如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论21Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦 22. greet.sh #!/bin/bash # Scriptname: greet.sh # usage: greet.sh Tom John Anndyecho "== using \$* =="for name in $* # same as for name in $@doecho Hi $namedoneecho "== using \$@ =="for name in $@# same as for name in $*doecho Hi $namedoneecho '== using "$*" =='for name in "$*"doecho Hi $namedoneecho '== using "$@" =='for name in "$@"doecho Hi $namedone 23. permx.sh #!/bin/bash # Scriptname: permx.sh #for file # Empty wordlistdoif [[ -f $file && ! -x $file ]]thenchmod +x $fileecho " == $file now has execute permission"fidone如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论22Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦 24. months.sh #!/bin/bash # Scriptname: months.shfor month in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Decdofor week in 1 2 3 4doecho -n "Processing the month of $month. OK? "read ansif [ "$ans" = n -o -z "$ans" ]thencontinue 2elseecho -n "Process week $week of $month? "read ansif [ "$ans" = n -o -z "$ans" ]thencontinueelseecho "Now processing week $week of $month."sleep 1 # Commands go hereecho "Done processing..."fifidonedone 25. runit.sh #!/bin/bash # Scriptname: runit.shPS3="Select a program to execute: "select programin 'ls -F' pwd datedo$programdone 26. goodboy.sh #!/bin/bash # Scriptname: goodboys.shPS3="Please choose one of the three boys : "如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论23Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦select choice in tom dan guy #select choicedocase $choice intom)echo Tom is a cool dude!break;;# break out of the select loopdan | guy )echo Dan and Guy are both wonderful.break;;*)echo "$REPLY is not one of your choices"echo "Try again.";;esacdone 27. doit.sh #!/bin/bash # Name: doit.sh # Purpose: shift through command line arguments # Usage: doit.sh [args]while (( $# > 0 )) # or [ $# -gt 0 ]doecho $*shiftdone 28. shft.sh #!/bin/bash # Using 'shift' to step through all the positional parameters.until [ -z "$1" ] # Until all parameters used up...doecho "$1"shiftdoneecho# Extra line feed.exit 0如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论24Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦 29. ex4str #!/bin/bashdirname="/usr/bin/local/bin";echo "dirname=$dirname"echo -n '${#dirname}='; sleep 4;echo "${#dirname}"echoecho -n '${dirname:4}='; sleep 4;echo "${dirname:4}"echoecho -n '${dirname:8:6}='; sleep 4; echo ${dirname:8:6}echoecho -n '${dirname#*bin}='; sleep 4; echo ${dirname#*bin}echoecho -n '${dirname##*bin}='; sleep 4;echo ${dirname##*bin}echoecho -n '${dirname%bin}='; sleep 4;echo ${dirname%bin}echoecho -n '${dirname%%bin}='; sleep 4;echo ${dirname%%bin}echoecho -n '${dirname%bin*}='; sleep 4;echo ${dirname%bin*}echoecho -n '${dirname%%bin*}='; echo ${dirname%%bin*}echoecho -n '${dirname/bin/sbin}='; echo ${dirname/bin/sbin}echoecho -n '${dirname//bin/lib}='; echo ${dirname//bin/lib}echoecho -n '${dirname/bin*/lib}='; echo ${dirname/bin*/lib} 30. ex4fun2.sh #!/bin/bashJUST_A_SECOND=1fun (){# A somewhat more complex functioni=0REPEATS=5echoecho "And now the fun really begins."echosleep $JUST_A_SECOND# Hey, wait a second!while [ $i -lt $REPEATS ]doecho "FUNCTIONS>"如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论25Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦echo "<ARE"echo "<FUN>"echolet "i+=1"done}# Now, call the functions.funexit 0 31. 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. 32. ex4fun4.sh #!/bin/bash # Functions and parametersDEFAULT=default # Default param value.func2 () {if [ -z "$1" ]# Is parameter #1 zero length?thenecho "-Parameter#1 is zero length -"elseecho "-Param#1 is \"$1\" -"如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论26Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦fivariable=${1:-$DEFAULT}echo "variable = $variable"if [ -n "$2" ]thenecho "- Parameter#2 is \"$2\" -"fireturn 0}echoecho "Nothing passed"func2# Called with no paramsechoecho "One parameter passed."func2 first# Called with one paramechoecho "Two parameters passed."func2 first second# Called with two paramsechoecho "\"\" \"second\" passed."func2 "" second# The first parameter is of zero?lengthechoexit 0# End of script 33. 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如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论27Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦echo "Second call to function: command-line arg passed explicitly."func $1# Now it's seen!exit 0 34. ex4fun6.sh #!/bin/bash # purpose: Maximum of two integers.max2 ()# Returns larger of two numbers.{if [ -z $2 ]thenecho "Need to pass two parameters to the function."exit 1fiif [[ $1 == $2 ]]# [ $1 -eq $2 ]thenecho "The two numbers are equal."exit 0elseif [ $1 -gt $2 ]thenreturn $1elsereturn $2fifi}read num1 num2echo "num1=$num1, num2=$num2"max2 $num1 $num2return_val=$?echo "The larger of the two numbers is : $return_val."exit 0如有任何疑问,请联系作者,作者 QQ:1028150787,或者到韦东山群共同讨论28Shell 脚本编程学习笔记2013 年 5 月 2 日 追风~忆梦 习题实训 1、 编写一个 shell 脚本,判断用户输入的字母,如 A~D 。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-116634-3.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    每日福利
    热点图片
    拼命载入中...