您的位置:首页 > 运维架构 > Shell

shell 脚本学习及troubleshooting

2013-05-24 13:56 323 查看
shell 脚本学习及troubleshooting
Shell问题一:
$ FILENAME="My Document" 含有空格的文件名$ ls $FILENAME 列出来试试ls: My: No such file or directory 糟了! ls 见到两个参数ls: Document: No such file or directory$ ls -l "$FILENAME"这样才对My Documentls 只见到一个参数
====================================================
Shell问题二
如果变量名称与另一个字符串紧接在一起,则必须以一对花括号界定,以免发生意料外的情况:$ HAT="fedora"$ echo "The plural of $HAT is $HATs"The plural of fedora is 糟了!没“HATs”这个变量$ echo "The plural of $HAT is ${HAT}s"The plural of fedora is fedoras 这才是我们要的结果
====================================================
grep匹配多个字符串时有一下两个种方法:
(1)egrep –i ‘read|enter’ read.sh(2)vi string.txt
readentergrep –f string.txt read.sh
以上两种方法是grep满足其中一个字符串都匹配,那能不能同时满足多个字符串时才匹配呢?看下例子:
grep –i hostname /etc/*| grep –i “loongson.cn”
只有一行里同时满足存在hostname 和 loongson.cn 才匹配。
find 和 grep的配合使用,防止对多余的文件类型的检索。如下;
find /etc –type f |xargs grep –i addr | grep –i “10.2.5.200”
====================================================
echo 'What would you like to do?'read answercase "$answer" in受测对象为answer 变量eat)echo "OK, have a hamburger";;sleep)echo "Good night then";;*)echo "I'm not sure what you want to do"echo "I guess I'll see you tomorrow";;esaccase 语句的标准语法是:case string inexpr1)body1;;expr2)body2;;...exprN)bodyN;;*)bodyelse;;esac
====================================================
$ cat myscript#!/bin/bashecho "My name is $1 and I come from $2"echo "Your info : $@"$ ./myscript Johnson WisconsinMy name is Johnson and I come from WisconsinYour info : Johnson Wisconsin

$# 来代表参数个数:$@ 命令行中的参数(所有)$* 命令行中的参数(循环时候)单行显示if [ $# -lt 2 ]thenecho "$0 error: you must supply two arguments"elseecho "My name is $1 and I come from $2"fi特殊变量$0 代表script 自己的名称。当script 需要显示自己的用法或错误信息时,这个变量就可以派上用场:$ ./myscript Bob./myscript error: you must supply two args====================================================
pidof firefox 查看程序进程号。pstree –p pid 查看进程派生出来的子进程。nohup命令nohup(no hang up不挂起)此可以在你退出账号之后继续运行相应的进程。格式为:nohup command &echo命令\c 不换行\n 进纸\t 跳格\f 换行tee命令作用:一个副本输送到标准输出,另一个副本拷贝到相应的文件里,如下:$who | tee -a who.out $cat who.out脚本实例:(1)forfind脚本:#cat forfindfor loopdo find / -name $loop –printdone执行forfind的脚本#./forfind passwd shadow那么显示结果就相当于 find / -name passwd 和 find / -name shadow(2)forparm脚本内容#cat forparm3#!/bin/bashfor params in “$*”do echo “you supplied $params as a command line option”done forparm3执行脚本./forparm wo ni tayou supplied wo ni ta as a command line option#cat forparm4#!/bin/bashfor params in “$@”do echo “you supplied $params as a command line option”done forparm3执行脚本./forparm wo ni tayou supplied wo as a command line optionyou supplied ni as a command line optionyou supplied ta as a command line option$@ 命令行中的参数(所有)$* 命令行中的参数(循环时候)单行显示====================================================
(3)forping脚本内容:cat forping#!/bin/bashHOSTS=”10.2.5.10 10.2.5.200 10.2.100.100”for loop in $HOSTSdo ping –c 2 $loopdone====================================================
(4)多文件转换cat 1.txt | tr ‘[A-Z]’‘[a-z]’ 将文件1.txt中大写字母显示为小写字母,但不改变原文件的内容。有点像sed的用法。脚本内容:cat foruc#!/bin/bashfor files in $(ls LPSO*)do cat $files |tr “[a-z]” “[A-Z]”>${files}.ucdone执行脚本 ./foruc(5) smartzip的脚本,该脚本可以自动解压bzip2, gzip 和zip 类型的压缩文件:
#!/bin/bash
ftype=$(file "$1")
case $ftype in
"$1: Zip archive"*)
unzip "$1" ;;
"$1: gzip compressed"*)
gunzip "$1" ;;
"$1: bzip2 compressed"*)
bunzip2 "$1" ;;
*) echo "File $1 can not be uncompressed with smartzip";;
esac多sed删除操作

sed -r 's/.+(type)=([0-9]+).+/\2/'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息