志在指尖
用双手敲打未来

shell脚本编程(shell编程入门)

shell脚本编程

用户自定义函数语法
[function]function_name[()]
{
action;
[returnvalue;]
}
[]代表可选;
留意,函数返回值用$?接收,要在函数履行完毕后当即接收并保存,防止被其他函数履行成果冲掉。
无返回值的自定义函数
functionnoreturnFun(){
echo”Thisismyfirstshellfucniton”
}
echo”———noreturnFun函数开始履行”
noreturnFun
echo”———noreturnFun函数履行完毕”
有返回值的自定义函数
returnFun(){
echo”函数:对输入的两个数字做加法…”
echo-n”输入第一个数字:”
readaNum
echo-n”输入第二个数字:”
readbNum
echo”两个数字分别是$aNum和$bNum!”
return$(($aNum+$bNum))
}
returnFun
echo”输入的两个数字的和为$?”
总结
以上是编程之家为你收集整理的Shell脚本编程根底五Shell自定义函数全部内容,希望文章可以帮你解决Shell脚本编程根底五Shell自定义函数所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员老友。
shell编程入门
Shell脚本编程学习入门是本文要介绍的内容,咱们能够运用任意一种文字修改器,比方gedit、kedit、emacs、vi等来编写shell脚本,它有必要以如下行开端(有必要放在文件的第一行):
#!/bin/sh

注意:最好运用“!/bin/bash”而不是“!/bin/sh”,假如运用tcshell改为tcsh,其他类似。
符号#!用来告诉体系履行该sell脚本的程序,本例运用/bin/sh。修改结束并保存后,假如要履行该shell脚本,有必要先使其可履行:
chmod+xfilename
尔后在该shell脚本所在目录下,输入./filename即可履行该shell脚本。
Shell里的一些特别符号
a[]
shell离得函数
假如你写过比较杂乱的shell脚本,就会发现可能在几个当地运用了相同的代码,这时假如用上函数,会便利很多。函数的大致样子如下:
functionname()
{
#insidethebody$1isthefirstargumentgiventothefunction
#$2thesecond…
body
}
你需要在每个脚本的开端对函数进行声明。
下面是一个名为xtitlebar的shell脚本,它能够改动终端窗口的名称。这里运用了一个名为help的函数,该函数在shell脚本中运用了两次:
#!/bin/sh
#vim:setsw=4ts=4et:
help()
{
cat<<HELP
xtitlebar–changethenameofanxterm,gnome-terminalorkdekonsole
USAGE:xtitlebar[-h]”string_for_titelbar”
OPTIONS:-hhelptext
EXAMPLE:xtitlebar”cvs”
HELP
exit0
}
#incaseoferrororif-hisgivenwecallthefunctionhelp:
[-z”$1″]&&help
[“$1″=”-h”]&&help
#sendtheescapesequencetochangethextermtitelbar:
echo-e”33]0;$107″
#
在shell脚本中供给协助是一种很好的编程习惯,能够便利其他用户(和自己)运用和了解脚本。
指令行参数
咱们已经见过$*和$1,$2…$9等特别变量,这些特别变量包括了用户从指令行输入的参数。迄今为止,咱们仅仅了解了一些简单的指令行语法(比方一些强制性的参数和检查协助的-h选项)。可是在编写更杂乱的程序时,您可能会发现您需要更多的自定义的选项。一般的惯例是在一切可选的参数之前加一个减号,后面再加上参数值(比方文件名)。
有很多办法能够完成对输入参数的剖析,可是下面的运用case表达式的比如无疑是一个不错的办法。
#!/bin/sh
help()
{
cat<<HELP
Thisisagenericcommandlineparserdemo.
USAGEEXAMPLE:cmdparser-lhello-f—somefile1somefile2
HELP
exit0
}
while[-n”$1″];do
case$1in
-h)help;shift1;;#functionhelpiscalled
-f)opt_f=1;shift1;;#variableopt_fisset
-l)opt_l=$2;shift2;;#-ltakesanargument->shiftby2
–)shift;break;;#endofoptions
-*)echo”error:nosuchoption$1.-hforhelp”;exit1;;
*)break;;
esac
done
echo”opt_fis$opt_f”
echo”opt_lis$opt_l”
echo”firstargis$1″
echo”2ndargis$2″
你能够这样运转该脚本:
cmdparser-lhello-f—somefile1somefile2
返回成果如下:
opt_fis1
opt_lishello
firstargis-somefile1
2ndargissomefile2
这个shell脚本是怎么作业的呢?脚本首要在一切输入指令行参数中进行循环,将输入参数与case表达式进行比较,假如匹配则设置一个变量而且移除该参数。根据unix体系的惯例,首要输入的应该是包括减号的参数。
shell脚本示例
一般编程过程
现在咱们来评论编写一个脚本的一般过程。任何优异的脚本都应该具有协助和输入参数。写一个框架脚本(framework.sh),该shell脚本包括了大多数脚本需要的框架结构,是一个十分不错的主见。这样一来,当咱们开端编写新脚本时,能够先履行如下指令:
cpframework.shmyscript
然后再插入自己的函数。
让咱们来看看如下两个示例。
二进制到十进制的转换
脚本b2d将二进制数(比方1101)转换为相应的十进制数。这也是一个用expr指令进行数学运算的比如:
#!/bin/sh
#vim:setsw=4ts=4et:
help()
{
cat<<HELP
b2d–convertbinarytodecimal
USAGE:b2d[-h]binarynum
OPTIONS:-hhelptext
EXAMPLE:b2d111010
willreturn58
HELP
exit0
}
error()
{
#printanerrorandexit
echo”$1″
exit1
}
lastchar()
{
#returnthelastcharacterofastringin$rval
if[-z”$1″];then
#emptystring
rval=””
return
fi
#wcputssomespacebehindtheoutputthisiswhyweneedsed:
numofchar=`echo-n”$1″|sed’s///g’|wc-c`
#nowcutoutthelastchar
rval=`echo-n”$1″|cut-b$numofchar`
}
chop()
{
#removethelastcharacterinstringandreturnitin$rval
if[-z”$1″];then
#emptystring
rval=””
return
fi
#wcputssomespacebehindtheoutputthisiswhyweneedsed:
numofchar=`echo-n”$1″|wc-c|sed’s///g’`
if[“$numofchar”=”1″];then
#onlyonecharinstring
rval=””
return
fi
numofcharminus1=`expr$numofchar”-“1`
#nowcutallbutthelastchar:
rval=`echo-n”$1″|cut-b-$numofcharminus1`
#本来的rval=`echo-n”$1″|cut-b0-${numofcharminus1}`运转时犯错.
#原因是cut从1开端计数,应该是cut-b1-${numofcharminus1}
}
while[-n”$1″];do
case$1in
-h)help;shift1;;#functionhelpiscalled
–)shift;break;;#endofoptions
-*)error”error:nosuchoption$1.-hforhelp”;;
*)break;;
esac
done
#Themainprogram
sum=0
weight=1
#oneargmustbegiven:
[-z”$1″]&&help
binnum=”$1″
binnumorig=”$1″
while[-n”$binnum”];do
lastchar”$binnum”
if[“$rval”=”1″];then
sum=`expr”$weight””+””$sum”`
fi
#removethelastpositionin$binnum
chop”$binnum”
binnum=”$rval”
weight=`expr”$weight””*”2`
done
echo”binary$binnumorigisdecimal$sum”
#
该shell脚本运用的算法是利用十进制和二进制数权值(1,2,4,8,16,..),比方二进制”10″能够这样转换成十进制:
0*1+1*2=2
为了得到单个的二进制数咱们是用了lastchar函数。该函数运用wc–c计算字符个数,然后运用cut指令取出结尾一个字符。Chop函数的功用则是移除最后一个字符。
文件循环转载
你可能有这样的需求并一直都这么做:将一切发出邮件保存到一个文件中。可是过了几个月之后,这个文件可能会变得很大以至于该文件的访问速度变慢;下面的shell脚本rotatefile能够解决这个问题。这个脚本能够重命名邮件保存文件(假设为outmail)为outmail.1,而本来的outmail.1就变成了outmail.2等等…
#!/bin/sh
#vim:setsw=4ts=4et:
ver=”0.1″
help()
{
cat<<HELP
rotatefile–rotatethefilename
USAGE:rotatefile[-h]filename
OPTIONS:-hhelptext
EXAMPLE:rotatefileout
Thiswille.grenameout.2toout.3,out.1toout.2,outtoout.1[BR]
andcreateanemptyout-file
Themaxnumberis10
version$ver
HELP
exit0
}
error()
{
echo”$1″
exit1
}
while[-n”$1″];do
case$1in
-h)help;shift1;;
–)break;;
-*)echo”error:nosuchoption$1.-hforhelp”;exit1;;
*)break;;
esac
done
#inputcheck:
if[-z”$1″];then
error”ERROR:youmustspecifyafile,use-hforhelp”
fi
filen=”$1″
#renameany.1,.2etcfile:
fornin987654321;do
if[-f”$filen.$n”];then
p=`expr$n+1`
echo”mv$filen.$n$filen.$p”
mv$filen.$n$filen.$p
fi
done
#renametheoriginalfile:
if[-f”$filen”];then
echo”mv$filen$filen.1″
mv$filen$filen.1
fi
echotouch$filen
touch$filen
这个shell脚本是怎么作业的呢?在检测到用户供给了一个文件名之后,首要进行一个9到1的循环;文件名.9重命名为文件名.10,文件名.8重命名为文件名.9……等等。循环结束之后,把原始文件命名为文件名.1,一起创立一个和原始文件同名的空文件(touch$filen)
脚本调试
最简单的调试办法当然是运用echo指令。你能够在任何怀疑犯错的当地用echo打印变量值,这也是大部分shell程序员花费80%的时刻用于调试的原因。Shell脚本的好处在于无需从头编译,而插入一个echo指令也不需要多少时刻。
shell也有一个真正的调试形式,假如脚本”strangescript”犯错,能够运用如下指令进行调试:
sh-xstrangescript
7上述指令会履行该脚本,一起显现一切变量的值。
shell脚本中还有一个不履行脚本只检查语法的形式,指令如下:
sh-nyour_script
这个指令会返回一切语法错误。

未经允许不得转载:IT技术网站 » shell脚本编程(shell编程入门)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载