




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
bash随笔1. 2&1重定向关于重定向,在bash的man中有下面的描述: Note that the order of redirections is significant. For example, the command ls dirlist 2&1 directs both standard output and standard error to the file dirlist, while the command ls 2&1 dirlist directs only the standard output to file dirlist, because the standard error was duplicated as standard output before the standard output was redirected to dirlist.根据上面的描述,我们可以知道两种形式的差异了:形式2首先duplicated一个stdout的句柄放到stderr,然后再将dirlist文件句柄放置到stdout,因此在形式2中仅能收集到stdout的输出,而stderr的输出仍然走标准的stdout句柄。形式1首先将dirlist文件句柄放置到stdout,然后再将stdout的句柄放到stderr,这样stdout和stderr中都是dirlist的文件句柄,因此可以收集stdout和stderr的输出。2. 遍历目录中所有的文件代码片断:#process each file in $css_pathfor myfile in *;do #skip directory if test -d $myfile; then continue; fi; echo process file: $myfile .;done3. 置换(Substitution)n $parameter:-word使用缺省值。如果parameter没有值或是null,使用word部分的表达式被提交;否则parameter部分的表达式被提交。n $parameter:=word赋缺省值。如果parameter没有值或是null,word部分的表达式被赋给parameter,最后,parameter部分的表达式被提交。Positional parameters and special parameters may not be assigned to in this way.n $parameter:?word如果parameter没有值或是null 则提示错误。如果parameter没有值或是null,word部分的表达式(如果word部分不存在则是一个消息)被发送到standard error或shell(如果是非交互模式),然后退出。否则,parameter的值被提交。n $parameter:+word使用其它值。如果parameter没有值或是null,什么都不提交,否则word部分的表达式被提交。n $parameter:offsetn $parameter:offset:length获得表达式的子串。offset以0为基开始索引。如果offset小于0,从字符串的尾部计算索引。如果length部分省略,长度自动计算到字符串表达式的尾部。n $parameter%wordn $parameter%word如果parameter的尾部和word部分最短匹配(the % case)或最长匹配(the % case),匹配的部分被删除。n $parameter/pattern/stringn $parameter/pattern/string将parameter中和pattern匹配的部分替换成string表达式的内容。”/pattern/string”完成一次替换;”/pattern/string”完成全局替换。4. 特殊参数shell对一些参数做了特殊的处理,这些参数只能引用不能改变。* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, $* is equivalent to $1c$2c., where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators. Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, $ is equivalent to $1 $2 . When there are no positional parameters, $ and $ expand to nothing (i.e., they are removed).# Expands to the number of positional parameters in decimal.? Expands to the status of the most recently executed foreground pipeline.- Expands to the current option flags as specified upon invocation, by the set builtin command, or That is, $ is equivalent to $1 $2 . When there are no positional parameters, $ and $ expand to nothing (i.e., they are removed).$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the cur- rent shell, not the subshell.! Expands to the process ID of the most recently exe- cuted background (asynchronous) command.0 Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Oth- erwise, it is set to the file name used to invoke bash, as given by argument zero._ At shell startup, set to the absolute file name of the shell or shell script being executed as passed in the argument list. Subsequently, expands to the last argument to the previous command, after expan- sion. Also set to the full file name of each com- mand executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file currently being checked.5. 流程控制语句5.1. 判断文件是否存在if ! -f /etc/sysconfig/network ; then exit 0fiif -f /etc/sysconfig/pcmcia ; then. /etc/sysconfig/pcmciafi比较完整的if语句,用于引入函数库:# Source function libraryif -f /etc/init.d/functions ; then . /etc/init.d/functionselif -f /etc/rc.d/init.d/functions ; then . /etc/rc.d/init.d/functionselse exit 0fi5.2. 短路算法注意,相等性通过单=号比较。# Check that networking is up. $NETWORKING = no & exit 0# if the ip configuration utility isnt around we cant function. -x /sbin/ip | exit 15.3. Case语句结束一个筛选通过;。case $IPX in yes|true) /sbin/ipx_configure -auto_primary=$IPXAUTOPRIMARY -auto_interface=$IPXAUTOFRAME if $IPXINTERNALNETNUM != 0 ; then /sbin/ipx_internal_net add $IPXINTERNALNETNUM $IPXINTERNALNODENUM fi ;esac完整的Case语句:# See how we were called.case $1 in start) start ; stop) stop ; status) dostatus ; restart|reload) restart ; condrestart) condrestart ; *) echo Usage: apmd.init start|stop|status|restart|reload|condrestart exit 1esac5.4. 函数定义判断字符串的包涵关系。# returns OK if $1 contains $2strstr() $1#*$2* = $1 & return 1 return 0对strstr函数的用法如下:# Confirm whether we really want to run this serviceconfirm() -x /usr/bin/rhgb-client & /usr/bin/rhgb-client -details=yes while : ; do echo -n $Start service $1 (Y)es/(N)o/(C)ontinue? Y read answer if strstr $yY $answer | $answer = ; then return 0 elif strstr $cC $answer ; then rm -f /var/run/confirm -x /usr/bin/rhgb-client & /usr/bin/rhgb-client -details=no return 2 elif strstr $nN $answer ; then return 1 fi done函数的返回值用return返回;参数通过$N访问。5.5. 命令的返回值最近执行的命令的返回值,通过$?访问。echo -n $Starting up APM daemon: test -r $CONFIG & . $CONFIGdaemon /usr/sbin/apmd -p $LOGPERCENTCHANGE -w $WARNPERCENT $ADDPARAMS -P /etc/sysconfig/apm-scripts/apmscriptRETVAL=$? $RETVAL -eq 0 & touch /va
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五版知识产权许可合同创新保护
- 二零二五年度洞渣运输工程与道路绿化养护合同
- 潜水运动基础知识培训课件
- 山西工会基本知识培训课件
- 大额贷款咨询知识培训课件
- 大面值人民币课件
- 大队劳资员专业知识培训课件
- 人文医师考试题及答案
- 农业养殖面试题及答案
- 职高语文考试试题及答案
- DL∕T 1396-2014 水电建设项目文件收集与档案整 理规范
- DB15-T 3516-2024 野生动物救护站建设规范
- 《多彩的超轻粘土》校本课程教案(共10课)
- 上海市嘉定区2023-2024学年三年级下学期期末数学试卷
- 室间隔缺损教学查房
- MBA《会计学》课件-(济南)
- 传奇辅助脚本
- 四川省普通高中2024届高三上学期学业水平考试数学试题(解析版)
- 孕产妇健康知识宣传栏
- 部委管理制度
- 动车组随车机械师-动车组随车机械师作业标准
评论
0/150
提交评论