登陆论坛
|
论坛注册
|
加入收藏
|
设为首页
|
RSS
首页
Linux频道
软件下载
开发语言
技术论坛
嵌入式频道
|
数据库开发
|
WEB开发
|
软件开发
|
嵌入应用
|
内核研究
|
SHELL
|
输入您的搜索字词
提交搜索表单
您当前的位置:
首页
>
Linux频道
>
Linux开发区
>
SHELL
Shell编程基础
时间:2006-07-16 21:29:14 来源:Linux联盟收集 作者:
rval=`echo -n "$1" | cut -b 0-${numofcharminus1}`
Z3yLinux联盟
}
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
while [ -n "$1" ]; do
Z3yLinux联盟
case $1 in
Z3yLinux联盟
-h) help;shift 1;; # function help is called
Z3yLinux联盟
--) shift;break;; # end of options
Z3yLinux联盟
-*) error "error: no such option $1. -h for help";;
Z3yLinux联盟
*) break;;
Z3yLinux联盟
esac
Z3yLinux联盟
done
Z3yLinux联盟
Z3yLinux联盟
# The main program
Z3yLinux联盟
sum=0
Z3yLinux联盟
weight=1
Z3yLinux联盟
# one arg must be given:
Z3yLinux联盟
[ -z "$1" ] && help
Z3yLinux联盟
binnum="$1"
Z3yLinux联盟
binnumorig="$1"
Z3yLinux联盟
Z3yLinux联盟
while [ -n "$binnum" ]; do
Z3yLinux联盟
lastchar "$binnum"
Z3yLinux联盟
if [ "$rval" = "1" ]; then
Z3yLinux联盟
sum=`expr "$weight" "+" "$sum"`
Z3yLinux联盟
fi
Z3yLinux联盟
# remove the last position in $binnum
Z3yLinux联盟
chop "$binnum"
Z3yLinux联盟
binnum="$rval"
Z3yLinux联盟
weight=`expr "$weight" "*" 2`
Z3yLinux联盟
done
Z3yLinux联盟
Z3yLinux联盟
echo "binary $binnumorig is decimal $sum"
Z3yLinux联盟
#
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
该脚本使用的算法是利用十进制和二进制数权值 (1,2,4,8,16,..),比如二进制"10"可以这样转换成十进制:
Z3yLinux联盟
Z3yLinux联盟
0 * 1 + 1 * 2 = 2
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
为了得到单个的二进制数我们是用了lastchar 函数。该函数使用wc –c计算字符个数,然后使用cut命令取出末尾一个字符。Chop函数的功能则是移除最后一个字符。
Z3yLinux联盟
Z3yLinux联盟
文件循环程序
Z3yLinux联盟
Z3yLinux联盟
或许您是想将所有发出的邮件保存到一个文件中的人们中的一员,但是在过了几个月以后,这个文件可能会变得很大以至于使对该文件的访问速度变慢。下面的脚本rotatefile 可以解决这个问题。这个脚本可以重命名邮件保存文件(假设为outmail)为outmail.1,而对于outmail.1就变成了outmail.2 等等等等...
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/sh
Z3yLinux联盟
# vim: set sw=4 ts=4 et:
Z3yLinux联盟
ver="0.1"
Z3yLinux联盟
help()
Z3yLinux联盟
{
Z3yLinux联盟
cat < rotatefile -- rotate the file name
Z3yLinux联盟
Z3yLinux联盟
USAGE: rotatefile [-h] filename
Z3yLinux联盟
Z3yLinux联盟
OPTIONS: -h help text
Z3yLinux联盟
Z3yLinux联盟
EXAMPLE: rotatefile out
Z3yLinux联盟
This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1
Z3yLinux联盟
and create an empty out-file
Z3yLinux联盟
Z3yLinux联盟
The max number is 10
Z3yLinux联盟
Z3yLinux联盟
version $ver
Z3yLinux联盟
HELP
Z3yLinux联盟
exit 0
Z3yLinux联盟
}
Z3yLinux联盟
Z3yLinux联盟
error()
Z3yLinux联盟
{
Z3yLinux联盟
echo "$1"
Z3yLinux联盟
exit 1
Z3yLinux联盟
}
Z3yLinux联盟
while [ -n "$1" ]; do
Z3yLinux联盟
case $1 in
Z3yLinux联盟
-h) help;shift 1;;
Z3yLinux联盟
--) break;;
Z3yLinux联盟
-*) echo "error: no such option $1. -h for help";exit 1;;
Z3yLinux联盟
*) break;;
Z3yLinux联盟
esac
Z3yLinux联盟
done
Z3yLinux联盟
Z3yLinux联盟
# input check:
Z3yLinux联盟
if [ -z "$1" ] ; then
Z3yLinux联盟
error "ERROR: you must specify a file, use -h for help"
Z3yLinux联盟
fi
Z3yLinux联盟
filen="$1"
Z3yLinux联盟
# rename any .1 , .2 etc file:
Z3yLinux联盟
for n in 9 8 7 6 5 4 3 2 1; do
Z3yLinux联盟
if [ -f "$filen.$n" ]; then
Z3yLinux联盟
p=`expr $n + 1`
Z3yLinux联盟
echo "mv $filen.$n $filen.$p"
Z3yLinux联盟
mv $filen.$n $filen.$p
Z3yLinux联盟
fi
Z3yLinux联盟
done
Z3yLinux联盟
# rename the original file:
Z3yLinux联盟
if [ -f "$filen" ]; then
Z3yLinux联盟
echo "mv $filen $filen.1"
Z3yLinux联盟
mv $filen $filen.1
Z3yLinux联盟
fi
Z3yLinux联盟
echo touch $filen
Z3yLinux联盟
touch $filen
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
这个脚本是如何工作的呢?在检测用户提供了一个文件名以后,我们进行一个9到1的循环。文件9被命名为10,文件8重命名为9等等。循环完成之后,我们将原始文件命名为文件1同时建立一个与原始文件同名的空文件。
Z3yLinux联盟
Z3yLinux联盟
调试
Z3yLinux联盟
Z3yLinux联盟
最简单的调试命令当然是使用echo命令。您可以使用echo在任何怀疑出错的地方打印任何变量值。这也是绝大多数的shell程序员要花费80%的时间来调试程序的原因。Shell程序的好处在于不需要重新编译,插入一个echo命令也不需要多少时间。
Z3yLinux联盟
Z3yLinux联盟
shell也有一个真实的调试模式。如果在脚本"strangescript" 中有错误,您可以这样来进行调试:
Z3yLinux联盟
Z3yLinux联盟
sh -x strangescript
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
这将执行该脚本并显示所有变量的值。
Z3yLinux联盟
Z3yLinux联盟
shell还有一个不需要执行脚本只是检查语法的模式。可以这样使用:
Z3yLinux联盟
Z3yLinux联盟
sh -n your_script
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
这将返回所有语法错误。
Z3yLinux联盟
Z3yLinux联盟
我们希望您现在可以开始写您自己的shell脚本,希望您玩得开心。
Z3yLinux联盟
3
/
3
|‹
‹‹
1
2
3
来顶一下
返回首页
发表评论
共有
条评论
用户名:
密码:
验证码:
匿名发表
相关文章
无相关信息
栏目更新
栏目热门
站内搜索:
Linux频道
下载频道
图库
商品
嵌入式频道
高级搜索
网站首页
|
关于我们
|
服务条款
|
广告服务
|
联系我们
|
网站大全
|
免责声明
|
返回顶部
Copyright © 2007-2008 xxlinux.com, All rights reserved.
Powered by linux联盟
京ICP备05012402号