编写shell脚本的一点经验总结
2012-02-17 22:15
441 查看
1.df -P #df显示的结果方便截取
[hanqi@localhost ~]$ df -P
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/mapper/VolGroup00-LogVol00 147282220 1541080 138138964 2% /
/dev/sda1 101086 12139 83728 13% /boot
tmpfs 1037208 0 1037208 0% /dev/shm
2.#判断输入的数字是否为整数的方法
1) read num
echo $num|grep -q '^[-]\?[0-9]\+$' && echo yes || echo error
2)echo 12.1 | grep -E '[^0-9]' >/dev/null && echo "not integer" || echo "integer" #判断正整数
#本人采用方法
3) [ "`echo $num/1 | bc`" == $num ] && echo "ok"
4)read -p "enter your b" test
[ -z ${test//[0-9]} ] && echo "Integar" || echo "Non-integer"
3.script_name=${0##*/} #获取脚本名称
script_path=${0%/*} #获取脚本路径,bash方式执行脚本是只能获得脚本名称,会使脚本出现错误
#获取脚本路径
,bash和绝对路径执行正确,但是./方式执行获得脚本路径有最后会多一个 . 未找到好的获得脚本绝对路径的方法,有的可以共享一下
getDir() {
dir=`echo $0 | grep "^/"`
if test "${dir}"; then
dirname $0
else
dirname `pwd`/$0
fi
}
dir=`getDir`
echo $dir
4.#关于时间
[root@fy disk_monitor]# date +%Y%m%d%H%M%S #年月日时分秒以数字形式显示,创建一个唯一的文件或目录
20120217162512
[root@fy disk_monitor]# date +%Y%m%d --date '1 days ago' #显示一天前的日期
20120216
[root@fy disk_monitor]# date '+%F %T' #记录日志
2012-02-17 16:31:10
5.#yes_or_no,有时需要用户决定是否执行某项操作,后边判断$a的值是什么来决定执行某些操作(或不执行某项操作)
echo -n "Whether backup(yes/no)?" #是否备份
read y
case "$y" in
y | yes ) a=1;;
n | no ) a=2;;
* ) echo "Please enter yes or no!"
esac
6.sed用法,处理文件流(内存中)
[root@fy disk_monitor]# echo "a/b/c" |sed 's/\///g' #去掉“/”
abc
[root@fy disk_monitor]# echo "a/b/c" |sed 's/\//-/g' #将/替换成-
a-b-c
sed -i可以直接更改文件内容,也可以采用cp一份sed完 > 临时文件保存到这个文件,在mv回去覆盖掉
7.shell脚本中执行sql语句的两种方法
#-p和密码之间没有空格,适合执行sql文件
mysql -h mysql_host -P mysql_port -u mysql_user -pmysql_pawd mysql_db < mysql.sql
#适合执行一个sql语句的方式。这种方法还是用于其他的需要自动交互式的脚本中
mysql -h mysql_host -P mysql_port -uroot <<EOF
use mysql_db;
show databases;
EOF
8.这个我也不知道是什么功能,总之有些特殊情况会用到
[root@fy script]# bash rsync/test.sh
a1
b2
[root@fy script]# cat rsync/test.sh
a="a b"
b=("1" "2")
i=0
for u in $a
do
echo "$u${b[$i]}"
i=$((i+1))
done
9.ls -l |awk '{print $NF}' #截取最后一个字段
10.shell脚本的调试方法
-n 读一遍脚本中的命令但不执行,用于检查脚本中的语法错误
-v 一边执行脚本,一边将执行过的脚本命令打印到标准错误输出
-x 提供跟踪执行信息,将执行的每一条命令和结果依次打印出来
三种选项的使用方法
1)$ sh -x ./script.sh #命令行提供参数
2)#! /bin/sh -x #在脚本开头提供参数
3)#! /bin/sh
if [ -z "$1" ]; then
set -x
echo "ERROR: Insufficient Args."
exit 1
set +x
fi #在脚本中用set命令启用或禁用参数、可以只对脚本中的某一段进行跟踪调试
本文出自 “红色石头” 博客,请务必保留此出处http://heishi317.blog.51cto.com/4301036/782329
[hanqi@localhost ~]$ df -P
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/mapper/VolGroup00-LogVol00 147282220 1541080 138138964 2% /
/dev/sda1 101086 12139 83728 13% /boot
tmpfs 1037208 0 1037208 0% /dev/shm
2.#判断输入的数字是否为整数的方法
1) read num
echo $num|grep -q '^[-]\?[0-9]\+$' && echo yes || echo error
2)echo 12.1 | grep -E '[^0-9]' >/dev/null && echo "not integer" || echo "integer" #判断正整数
#本人采用方法
3) [ "`echo $num/1 | bc`" == $num ] && echo "ok"
4)read -p "enter your b" test
[ -z ${test//[0-9]} ] && echo "Integar" || echo "Non-integer"
3.script_name=${0##*/} #获取脚本名称
script_path=${0%/*} #获取脚本路径,bash方式执行脚本是只能获得脚本名称,会使脚本出现错误
#获取脚本路径
,bash和绝对路径执行正确,但是./方式执行获得脚本路径有最后会多一个 . 未找到好的获得脚本绝对路径的方法,有的可以共享一下
getDir() {
dir=`echo $0 | grep "^/"`
if test "${dir}"; then
dirname $0
else
dirname `pwd`/$0
fi
}
dir=`getDir`
echo $dir
4.#关于时间
[root@fy disk_monitor]# date +%Y%m%d%H%M%S #年月日时分秒以数字形式显示,创建一个唯一的文件或目录
20120217162512
[root@fy disk_monitor]# date +%Y%m%d --date '1 days ago' #显示一天前的日期
20120216
[root@fy disk_monitor]# date '+%F %T' #记录日志
2012-02-17 16:31:10
5.#yes_or_no,有时需要用户决定是否执行某项操作,后边判断$a的值是什么来决定执行某些操作(或不执行某项操作)
echo -n "Whether backup(yes/no)?" #是否备份
read y
case "$y" in
y | yes ) a=1;;
n | no ) a=2;;
* ) echo "Please enter yes or no!"
esac
6.sed用法,处理文件流(内存中)
[root@fy disk_monitor]# echo "a/b/c" |sed 's/\///g' #去掉“/”
abc
[root@fy disk_monitor]# echo "a/b/c" |sed 's/\//-/g' #将/替换成-
a-b-c
sed -i可以直接更改文件内容,也可以采用cp一份sed完 > 临时文件保存到这个文件,在mv回去覆盖掉
7.shell脚本中执行sql语句的两种方法
#-p和密码之间没有空格,适合执行sql文件
mysql -h mysql_host -P mysql_port -u mysql_user -pmysql_pawd mysql_db < mysql.sql
#适合执行一个sql语句的方式。这种方法还是用于其他的需要自动交互式的脚本中
mysql -h mysql_host -P mysql_port -uroot <<EOF
use mysql_db;
show databases;
EOF
8.这个我也不知道是什么功能,总之有些特殊情况会用到
[root@fy script]# bash rsync/test.sh
a1
b2
[root@fy script]# cat rsync/test.sh
a="a b"
b=("1" "2")
i=0
for u in $a
do
echo "$u${b[$i]}"
i=$((i+1))
done
9.ls -l |awk '{print $NF}' #截取最后一个字段
10.shell脚本的调试方法
-n 读一遍脚本中的命令但不执行,用于检查脚本中的语法错误
-v 一边执行脚本,一边将执行过的脚本命令打印到标准错误输出
-x 提供跟踪执行信息,将执行的每一条命令和结果依次打印出来
三种选项的使用方法
1)$ sh -x ./script.sh #命令行提供参数
2)#! /bin/sh -x #在脚本开头提供参数
3)#! /bin/sh
if [ -z "$1" ]; then
set -x
echo "ERROR: Insufficient Args."
exit 1
set +x
fi #在脚本中用set命令启用或禁用参数、可以只对脚本中的某一段进行跟踪调试
本文出自 “红色石头” 博客,请务必保留此出处http://heishi317.blog.51cto.com/4301036/782329
相关文章推荐
- shell笔记-全面总结-强化脚本编写能力
- shell一段脚本的一点经验(实时文件流读写)
- shell脚本编写总结
- Shell脚本简单总结及编写脚本中的小技巧
- 【APP】Shell脚本简单总结及编写脚本中的小技巧 推荐
- Windows下编写Shell脚本的小经验
- Shell脚本简单总结及编写脚本中的小技巧
- 一点I2C接口经验总结
- 学习编写shell脚本(一)
- shell脚本学习总结----cut+sort+uniq+tr+find+dd+file
- cygwin安装教程以及使用notepad++编写shell脚本,并通过cygwin运行
- linux 编写shell管理脚本02.2
- Shell脚本编写的八条可靠建议(值得收藏)
- Makefile和shell脚本调用上的一些总结
- 转载]Linux 编写shell脚本(2)if for while
- 编写可靠 Shell 脚本的 8 个建议
- Shell脚本8种字符串截取方法总结
- 关于C语言多文件编写的一点小小总结
- shell脚本使用的几点总结
- 编写SHELL脚本--编写简单脚本