1.防止在错误的计算机中运行
ssh远程调试中经常在2个或多个计算机中切换,特别是vmware共享文件夹启用后,2个计算机都可对其操作,而有些脚本只能在特定的这个计算机上运行,如清除日志,打包,解压,删除文件等。一旦在错误的机器上运行,很可能把重要的文件删掉而造成无法挽回的后果。
#!/bin/bash
s=`uname -n`
echo "s=$s"
host="zhangwei-desktop"
echo "host=$host"
if [ $s != $host ]
then
echo "Please run the script in $host"
exit 1
fi
rm -f cscope.files
rm -f tags
rm -f cscope.out
#cscope:
find . -name "*.[csh]" -print > cscope.files
cscope -b
#tags:
ctags -R -I EXPORT_SYMBOL -I MODULE_VERSION -I MODULE_AUTHOR -I MODULE_DESCRIPTION -I MODULE_SUPPORTED_DEVICE -I MODULE_LICENSE \
2.自定义kill脚本
使用kill -9 需要知道进程id,mykill脚本只需要给出程序名的一部分即可
#!/bin/bash
for i in $( ps -A | grep $1 | awk '{ print $1 }' )
do
echo "kill process *$1* , id: $i "
kill -9 $i
done
3.ftp上传脚本
#!/bin/bash
IP_ADDRESS="192.168.83.1"
HOSTNAME="RD-OBS"
REMOTE_PATH="/usr/obs/hustosd_zw/"
USERNAME="*****"
PASSWORD="*****"
ftp -in <<EOF
open $IP_ADDRESS
user $USERNAME $PASSWORD
binary
cd $REMOTE_PATH
put "src.tar.gz"
bye
EOF
4.源码行数统计脚本
(非递归)
#!/bin/bash
w=0
for s in $( find . -name *.[hc] )
do
t=$( wc -l $s | awk '{print $1}' )
let "w=w+t"
done
echo $w
(递归)
#!/bin/bash
f()
{
w=0
for s in $( ls $1 )
do
if [ -f $1 ]
then
let "w+=$(wc -l <$1)"
break
fi
st=${s%/*}
if [ -d $1/$st ]
then
let "w+=$(f $1/$st)"
else
case $st in
*.c|*.h)
let "w+=$(wc -l <"$1/$st")"
;;
esac
fi
done
echo $w
}
if [ -z "$1" ]
then
echo "Uasge:count_code path!"
exit 1
else
f $1
fi
5.源文件单词替换批处理
#!/bin/bash
for s in $(ls *.[ch])
do
echo "file: $s"
news="${s}_bak"
echo "newfile: $news"
cat $s | sed s/iscsi_socket_t/mds_socket_t/g > ${news}
mv -f ${news} $s
done
(以上代码均为原创,zz请注明!谢谢!)