已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 受试者体验与风险最小化的优化策略实践
- 双相抑郁抑郁发作期的药物-心理激活策略
- 医疗护理实践案例分析
- 2026年一级注册建筑师之建筑经济、施工与设计业务管理考试题库300道及参考答案【典型题】
- 卵巢交界性肿瘤生育功能保留的术后心理调适干预策略报告
- 卵巢癌复发患者机器人二次减瘤粘连松解的术后随访策略-1
- 2025河南创作者传媒科技有限公司招聘21名笔试备考试题附答案
- 2025浙江海数科技有限公司招聘9人模拟试卷附答案
- 2026年中级银行从业资格之中级公司信贷考试题库300道含完整答案【夺冠系列】
- 2026年一级注册建筑师之建筑物理与建筑设备考试题库300道附答案【b卷】
- 申论笔试题目及答案
- 指数对数函数高考题
- 雨污合流管网改造工程施工组织设计
- GB 28050-2025食品安全国家标准预包装食品营养标签通则
- 有限空间作业中毒窒息应急处理预案
- DB46T665-2025 乡镇(街道)民政服务站建设和管理规范
- 深信服潜伏威胁探针STA-技术白皮书-20220425
- 山东省济南市2023-2024学年高一上学期1月期末考试数学试题
- 广东省广州市四年级第34届“YMO”青少年数学思维研学交流活动复选(含答案)
- 直线回归课件
- 【MOOC】行政法与行政诉讼法学-西南政法大学 中国大学慕课MOOC答案
评论
0/150
提交评论