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 �Ci ‘read|enter’ read.sh
(2)vi string.txt
read
enter
grep �Cf string.txt read.sh
以上两种方法是grep满足其中一个字符串都匹配,那能不能同时满足多个字符串时才匹配呢?看下例子:
grep �Ci hostname /etc/*| grep �Ci “loongson.cn”
只有一行里同时满足存在hostname 和 loongson.cn 才匹配。
find 和 grep的配合使用,防止对多余的文件类型的检索。如下;
find /etc �Ctype f |xargs grep �Ci addr | grep �Ci “10.2.5.200”
====================================================
echo 'What would you like to do?'
read answer
case "$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"
;;
esac
case 语句的标准语法是:
case string in
expr1)
body1
;;
expr2)
body2
;;
...
exprN)
bodyN
;;
*)
bodyelse
;;
esac
====================================================
$ cat myscript
#!/bin/bash
echo "My name is $1 and I come from $2"
echo "Your info : $@"
$ ./myscript Johnson Wisconsin
My name is Johnson and I come from Wisconsin
Your info : Johnson Wisconsin
$# 来代表参数个数:
$@ 命令行中的参数(所有)
$* 命令行中的参数(循环时候)单行显示
if [ $# -lt 2 ]
then
echo "$0 error: you must supply two arguments"
else
echo "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 �Cp 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 forfind
for loop
do
find / -name $loop �Cprint
done
执行forfind的脚本
#./forfind passwd shadow
那么显示结果就相当于 find / -name passwd 和 find / -name shadow
(2)forparm脚本内容
#cat forparm3
#!/bin/bash
for params in “$*”
do
echo “you supplied $params as a command line option”
done
forparm3执行脚本
./forparm wo ni ta
you supplied wo ni ta as a command line option
#cat forparm4
#!/bin/bash
for params in “$@”
do
echo “you supplied $params as a command line option”
done
forparm3执行脚本
./forparm wo ni ta
you supplied wo as a command line option
you supplied ni as a command line option
you supplied ta as a command line option
$@ 命令行中的参数(所有)
$* 命令行中的参数(循环时候)单行显示
====================================================
(3)forping
脚本内容:cat forping
#!/bin/bash
HOSTS=”10.2.5.10 10.2.5.200 10.2.100.100”
for loop in $HOSTS
do
ping �Cc 2 $loop
done
====================================================
(4)多文件转换
cat 1.txt | tr ‘[A-Z]’‘[a-z]’ 将文件1.txt中大写字母显示为小写字母,但不改变原文件的内容。有点像sed的用法。
脚本内容:cat foruc
#!/bin/bash
for files in $(ls LPSO*)
do
cat $files |tr “[a-z]” “[A-Z]”>${files}.uc
done
执行脚本 ./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/'