shell脚本技巧 学习linux shell编程的朋友,会对shell中的文件读取方法、文件权限判断,特别是linux命令的使用技巧,普遍感到难掌握。 如果你也像我一样是linux shell脚本编程方面的新手,那么如下的一些shell技巧,相信会对你有很大的帮助,一起来看看吧。
0. shell 调试
在somefile.sh 文件里加上set+x set-x
gzip -t a.tar.gz
if [[ 0 == $? ]]; then echo "good zip" else echo "bad zip" fi
可以简化为:
gzip -t a.tar.gz && echo "good zip" || echo "bad zip"
2,判断文件非空
if [[ -s $file ]]; then
echo "not empty" fi
3,获取文件大小
stat -c %s $file
stat --printf='%s\n' $file wc -c $file
4,字符串替换
${string//pattern/replacement}
a='a,b,c' echo ${a//,/ /}
5,Contains 子字符串?
string="My string"
if [[ $string == *My* ]]; then echo "It's there!" fi
6,rsync备份
rsync -r -t -v /source_folder /destination_folder
rsync -r -t -v /source_folder [user@host:/destination_folder
7,批量重命名文件
去掉所有的bak后缀:
把所有的空格改成下划线:
find path -type f -exec rename 's/ /_/g' {} \;
把文件名都改成大写:
find path -type f -exec rename 'y/a-z/A-Z/' {} \;
8,for/while 循环
for ((i=0; i < 10; i++)); do echo $i; done
for line in $(cat a.txt); do echo $line; done for f in *.txt; do echo $f; done while read line ; do echo $line; done < a.txt cat a.txt | while read line; do echo $line; done
9,删除空行
cat a.txt | sed -e '/^$/d'
(echo "abc"; echo ""; echo "ddd";) | awk '{if (0 != NF) print $0;}'
10,比较文件的修改时间
[[ file1.txt -nt file2.txt ]] && echo true || echo false
[[ file1.txt -ot file2.txt ]] && echo true || echo false
11,实现Dictionary结构
hput() {
eval "hkey_$1"="$2" } hget() { eval echo '${'"hkey_$1"'}' } $ hput k1 aaa $ hget k1 aaa
12,去掉第二列
$echo 'a b c d e f' | cut -d ' ' -f1,3-
$a c d e f
13,把stderr输出保存到变量
$ a=$( (echo 'out'; echo 'error' 1>&2) 2>&1 1>/dev/null)
$ echo $a error
14. 删除前3行
15. 读取多个域到变量
16. 遍历数组
array=( one two three )
for i in ${array[@]} do echo $i done
17. 查看目录大小
18. 查看CPU信息
19. date
$ date +%Y-%m-%d
2012-12-24 $ date +%Y-%m-%d –date ‘-1 day' 2012-12-23 $ date +%Y-m-%d –date ‘Dec 25' 2011-12-25 $ date +%Y-m-%d –date ‘Dec 25 – 10 days' 2011-12-15
20. 获取路径名和文件名
$ dirname ‘/home/lalor/a.txt'
/home/lalor $ basename ‘/home/lalor/a.txt' a.txt
21. 并集和交集
$cat a
1 3 5 $cat b 3 4 5 6 7 $comm a b 1 3 4 5 6 7 $comm -1 -2 a b #交集 3 5 $comm a b | sed 's/\t//g' #并集 1 2 3 4 5 6 7 $comm -1 -3 a b | sed 's/\t//g' #b-a 4 6 7
22. awk复杂分隔符
多字符作分隔符
多种分隔符2
23. 产生一个随机数
24. 按照模式split 文件
25. 获取文件名或者扩展名
var=hack.fun.book.txt
echo ${var%.*} hack.fun.book echo ${var%%.*} hack echo ${var#.*} fun.book.txt echo ${var##.*} txt
26. 以 root 帐户执行上一条命令。
其中: * !! 是指上一条命令 * !$ 上一条命令的最后一个参数 * !* 上一条命令的所有参数 * !:3 上一条命令的第3个参数
$ls /tmp/somedir
ls: cannot access /tmp/somedir: No such file or directory $mkdir !$ madir /tmp/somedir
27. 利用 python 搭建一个简单的 Web 服务器,可通过 http://$HOSTNAME:8000 访问。
28. 在 Vim 中无需权限保存编辑的文件。
29. 将上一条命令中的 foo 替换为 bar,并执行。
30. 快速备份或复制文件。
31. 将 ssh keys 复制到 user@host 以启用无密码 SSH 登录。
32. 把 linux 桌面录制为视频。
33. man 妙用
34. 在 vim 中编辑上一条命令
35. 删除0 字节文件或垃圾文件 36. 在编写SHELL 时显示多行信息
cat << EOF
+--------------------------------------------------------------+ | === Welcome to Tunoff services === | +--------------------------------------------------------------+ EOF 注意,在指定结束符时,它必须是该行的唯一内容,并且该行必须以这个字符开头。
37. 如何给mysql建软链接
cd /usr/local/mysql/bin
for i in * do ln /usr/local/mysql/bin/$i /usr/bin/$i done
38. 获取IP地址:
39. 打开文件数目
40. 清除僵尸进程
41. 打印唯一行
42. 打印奇数行
43. 打印匹配行后的某一行
44. 打印某行后后面的10行
cat file | grep -A100 string
cat file | grep -B100 string #前面 cat file | grep -C100 string #前后 sed -n '/string/,+100p' awk '/string/{f=100}--f>=0'
45. 获取命令行最后一个参数
如何在脚本里面重定向?和普通重定向一样。
#!/bin/bash
#redirecting output to different locations echo "now redirecting all output to another location" &>/dev/null
如果要将所有的输出都重定向到某个文件呢?
#!/bin/bash
#redirecting output to different locations exec 2>testerror echo "This is the start of the script" echo "now redirecting all output to another location" exec 1>testout echo "This output should go to testout file" echo "but this should go the the testerror file" >& 2
输出结果:
This is the start of the script
now redirecting all output to another location lalor@lalor:~/temp$ cat testout This output should go to testout file lalor@lalor:~/temp$ cat testerror but this should go the the testerror file lalor@lalor:~/temp$
以追加的方式重定向:
取消重定向:
47. 函数
#!/bin/bash
function factorial { if [[ $1 -eq 1 ]]; then echo 1 else local temp=$[ $1 - 1 ] local result=`factorial $temp` echo $[ $result * $1 ] fi } result=`factorial 5` echo $result
创建函数库
#!/bin/bash
#adding values in an array function addarray { local sum=0 local newarray newarray=(`echo "$@"`) for value in ${newarray[*]} do sum=$[ $sum+$value ] done echo $sum } myarray=(1 2 3 4 5) echo "The original array is: ${myarray[*]}" arg1=`echo ${myarray[*]}` result=`addarray $arg1` echo "The result is $result"
48、shell脚本中的正则表达式
^[1-9]d*$ //匹配正整数
^-[1-9]d*$ //匹配负整数 ^-?[1-9]d*$ //匹配整数 ^[1-9]d*|0$ //匹配非负整数(正整数+ 0) ^-[1-9]d*|0$ //匹配非正整数(负整数+ 0) ^[1-9]d*.d*|0.d*[1-9]d*$ //匹配正浮点数 ^-([1-9]d*.d*|0.d*[1-9]d*)$ //匹配负浮点数 ^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ //匹配浮点数 ^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ //匹配非负浮点数(正浮点数+ 0) ^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$ //匹配非正浮点数(负浮点数+ 0)
评注:处理大量数据时有用,具体应用时注意修正
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
(责任编辑:IT)^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串 ^[a-z]+$ //匹配由26个英文字母的小写组成的字符串 ^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串 ^w+$ //匹配由数字、26个英文字母或者下划线组成的字符串 |