《10-3shell编程.ppt》由会员分享,可在线阅读,更多相关《10-3shell编程.ppt(30页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Linux Shell编程编程处理用户输入处理用户输入命令行参数处理#!/bin/basha=1for(b=1;b$n),而变量$1则被丢弃,$0这个程序名称没变#!/bin/bashcount=1while-n$1doechoparameter#$count=$1count=$count+1#shiftaparmshiftdone#bashtest.shlinuxidcemclinuxracparameter#1=linuxidcparameter#2=emcparameter#3=linuxparameter#4=rac移动多个参数#!/bin/bashechotheoriginalpar
2、ameter:$*#shift3paramshift3echothenewshiftparameteris:$1#bashtest.shaabbccddeetheoriginalparameter:aabbccddeethenewshiftparameteris:dd一些运行选项选项描述-a实现所有对象-c生成计数-d指定目录-e展开对象-f指定读取数据的文件-h显示命令的帮助信息-i忽略大小写-l生成长格式的输出选项描述-n使用非交互式(批量)模式-o指定一个输出文件来重定向输出-q以quite模式退出-r递归处理目录和文件-s以silent模式执行-v生成verbose模式-x排除和拒绝-
3、y设置所有提问回答为yes#!/bin/bashwhile-n$1docase$1in-a)echothe-aoptionexists;-b)echothe-boptionexists;-c)echothe-coptionexists;*)echothe$1isnotanoption;esacshiftdone#bashtest.sh-a-b-c-d-ethe-aoptionexiststhe-boptionexiststhe-coptionexiststhe-disnotanoptionthe-eisnotanoption#!/bin/bashwhile-n$1docase$1in-a)ec
4、hothe-aoptionexists;-b)echothe-boptionexists;-c)echothe-coptionexists;-)shiftbreak;*)echothe$1isnotanoption;esacshiftdonecount=1forparamin$doechoparameter#$count:$paramcount=$count+1done#bashtest.sh-a-c-f-btestthe-aoptionexiststhe-coptionexiststhe-fisnotanoptionparameter#1:-bparameter#2:test#bashtes
5、t.sh-a-c-f-btestthe-aoptionexiststhe-coptionexiststhe-fisnotanoptionthe-boptionexiststhetestisnotanoption获取用户输入#!/bin/bashecho-npleaseinputyourname:readnameechohello$name,welcometokmust#bashtest.shpleaseinputyourname:sgfhellosgf,welcometokmust#!/bin/bashread-ppleaseinputyourname:nameechohello$name,w
6、elcometokmust#bashtest.shpleaseinputyourname:sgfhellosgf,welcometokmust环境变量REPLY#readhellosgf#echo$REPLYhellosgf#!/bin/bashread-ppleaseinputyourname:echohello$REPLY,welcometokmust#bashtest.shpleaseinputyourname:sgfhellosgf,welcometokmust计时#!/bin/bashifread-t5-ppleaseinputyourname:thenechohello$REPLY
7、,welcometocomebackhereelseechosorry,youaretooslowfi说明:-t5是的输入超过5秒则跳到else的判断结果,-p必须放置在-t的后面,否则报错#bashtest.shpleaseinputyourname:sorry,youaretooslow#!/bin/bashread-n1-pdoyouwanttocontinueY/N?case$REPLYinY|y)echoechofine,continueon.;N|n)echoechoOK,goodbye.;esac-n后面是数字1,表示read命令接收到一个字符就退出,所以输入Y/N后不用回车就马上执行下去了密码输入#!/bin/bashread-s-ppleaseinputyourpassword:echoechoyourpasswordis$REPLY-s选项,使得输入的数据颜色跟背景颜色一致,实现不显示数据的效果read命令读取文件#!/bin/bashcount=1cattest.txt|whilereadlinedoechoLINE$count:$linecount=$count+1done每调用一次read命令,都会去读取一行文本作业练习各种输入输出的各种处理方法
限制150内