范文资料网>反思报告>脚本>《shell脚本基础学习

shell脚本基础学习

时间:2022-09-24 04:33:20 脚本 我要投稿
  • 相关推荐

shell脚本基础学习

现在我们来讨论编写一个脚本的一般步骤。任何优秀的脚本都应该具有帮助和输入参数。并且写一个伪脚本(framework.sh),该脚本包含了大多数脚本都需要的框架结构,是一个非常不错的主意。这时候,在写一个新的脚本时我们只需要执行一下copy命令:

shell脚本基础学习

cp framework.sh myscript

然后再插入自己的函数。

让我们再看个例子:

二进制到十进制的转换

脚本 b2d 将二进制数 (比如 1101) 转换为相应的十进制数。这也是一个用expr命令进行数学运算的例子:

1#!/bin/sh23# vim:setsw=4 ts=4 et:45help()67{89cat <1011b2h -- convert binary to decimal1213usage: b2h [-h] binarynum1415options: -h help text1617example: b2h 1110101819willreturn582021help2223exit 02425}2627error()2829{3031# print an error and exit3233echo "$1"3435exit 13637}3839lastchar()4041{4243#returnthe last character of a stringin$rval4445if[ -z "$1" ]; then4647# empty string4849rval=""5051return5253fi5455# wc puts some space behind the output this is why we need sed:5657numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `5859# now cut out the last char6061rval=`echo -n "$1" | cut -b $numofchar`6263}6465chop()6667{6869# remove the last characterinstring andreturnitin$rval7071if[ -z "$1" ]; then7273# empty string7475rval=""7677return7879fi8081# wc puts some space behind the output this is why we need sed:8283numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `8485if[ "$numofchar" = "1" ]; then8687# only one charinstring8889rval=""9091return9293fi9495numofcharminus1=`expr $numofchar "-" 1`9697# now cut all but the last char:9899rval=`echo -n "$1" | cut -b 0-${numofcharminus1}`100101}102103while[ -n "$1" ];do104105case$1in106107-h) help;shift 1;; #functionhelp is called108109--) shift;break;; # end of options110111-*) error "error: no such option $1. -h for help";;112113*)break;;114115esac116117done118119# the main program120121sum=0122123weight=1124125# one arg must be given:126127[ -z "$1" ] && help128129binnum="$1"130131binnumorig="$1"132133while[ -n "$binnum" ];do134135lastchar "$binnum"136137if[ "$rval" = "1" ]; then138139sum=`expr "$weight" "+" "$sum"`140141fi142143# remove the last positionin$binnum144145chop "$binnum"146147binnum="$rval"148149weight=`expr "$weight" "*" 2`150151done152153echo "binary $binnumorig is decimal $sum"

该脚本使用的算法是利用十进制和二进制数权值 (1,2,4,8,16,..),比如二进制"10"可以这样转换成十进制:

《shell脚本基础学习》全文内容当前网页未完全显示,剩余内容请访问下一页查看。

0 * 1 + 1 * 2 = 2

为了得到单个的二进制数我们是用了lastchar 函数。该函数使用wc –c计算字符个数,然后使用cut命令取出末尾一个字符。chop函数的功能则是移除最后一个字符。

文件循环程序

或许您是想将所有发出的邮件保存到一个文件中的人们中的一员,但是在过了几个月以后,这个文件可能会变得很大以至于使对该文件的访问速度变慢。下面的 脚本rotatefile可

以解决这个问题。这个脚本可以重命名邮件保存文件(假设为outmail)为outmail.1,而对于 outmail.1就变成了outmail.2 等等等等...

1#!/bin/sh23# vim:setsw=4 ts=4 et:45ver="0.1"67help()89{1011cat <1213rotatefile -- rotate the file name1415usage: rotatefile [-h] filename1617options: -h help text1819example: rotatefile out2021this will e.g rename out.2 to out.3, out.1 to out.2, out to out.12223and create an empty out-file2425the max number is 102627version $ver2829help3031exit 03233}3435error()3637{3839echo "$1"4041exit 14243}4445while[ -n "$1" ];do4647case$1in4849-h) help;shift 1;;5051--)break;;5253-*) echo "error: no such option $1. -h for help";exit 1;;5455*)break;;5657esac5859done6061# input check:6263if[ -z "$1" ] ; then6465error "error: you must specify a file, use -h for help"6667fi6869filen="$1"7071# rename any .1 , .2 etc file:7273fornin9 8 7 6 5 4 3 2 1;do7475if[ -f "$filen.$n" ]; then7677p=`expr $n + 1`7879echo "mv $filen.$n $filen.$p"8081mv $filen.$n $filen.$p8283fi8485done8687# rename the original file:8889if[ -f "$filen" ]; then9091echo "mv $filen $filen.1"9293mv $filen $filen.19495fi9697echo touch $filen9899touch $filen

这个脚本是如何工作的呢?在检测用户提供了一个文件名以后,我们进行一个9到1的循环。文件9被命名为10,文件8重命名为9等等。循环完成之后,我们将原始文件命名为文件1

同时建立一个与原始文件同名的空文件。

调试

最简单的调试命令当然是使用echo命令。您可以使用echo在任何怀疑出错的地方打印任何变量值。这也是绝大多数的shell程序员要花费80%的时间来调试程序的原因。shell程序的

好处在于不需要重新编译,插入一个echo命令也不需要多少时间。

shell也有一个真实的调试模式。如果在脚本"strangescript" 中有错误,您可以这样来进行调试:

sh -x strangescript

这将执行该脚本并显示所有变量的值。

shell还有一个不需要执行脚本只是检查语法的模式。可以这样使用:

sh -n your_script

这将返回所有语法错误

这里linux shell脚本基础学习就全部结束了。感谢大家的支持。

转载2017-04-26 17:33 | #2楼

linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头、注释、变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提。

1. linux 脚本编写基础

◆1.1 语法基本介绍

1.1.1 开头

程序必须以下面的行开始(必须方在文件的第一行):

#!/bin/sh

符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/sh来执行程序。

当编辑好脚本时,如果要执行该脚本,还必须使其可执行。

要使脚本可执行:

编译 chmod +x filename 这样才能用./filename 来运行

1.1.2 注释

在进行shell编程时,以#开头的句子表示注释,直到这一行的结束。我们真诚地建议您在程序中使用注释。

如果您使用了注释,那么即使相当长的时间内没有使用该脚本,您也能在很短的时间内明白该脚本的作用及工作原理。

1.1.3 变量

在其他编程语言中您必须使用变量。在shell编程中,所有的变量都由字符串组成,并且您不需要对变量进行声明。要赋值给一个变量,您可以这样写:

#!/bin/sh

#对变量赋值:

a="hello world"

# 现在打印变量a的内容:

echo "a is:"

echo $a

有时候变量名很容易与其他文字混淆,比如:

num=2

echo "this is the $numnd"

这并不会打印出"this is the 2nd",而仅仅打印"this is the ",因为shell会去搜索变量numnd的值,但是这个变量时没有值的。可以使用花括号来告诉shell我们要打印的是num变量:

num=2

echo "this is the ${num}nd"

这将打印: this is the 2nd

1.1.4 环境变量

由export关键字处理过的变量叫做环境变量。我们不对环境变量进行讨论,因为通常情况下仅仅在登录脚本中使用环境变量。

这一讲就介绍到这里,下面我们会接触到具体的linux shell脚本基础的实质部分。

【shell脚本基础学习】相关文章:

脚本语言基础知识09-24

平面拍摄脚本09-24

牙膏广告脚本09-24

电影脚本写作09-24

校园电影脚本09-24

宣传短片脚本09-24

电影脚本制作09-24

婚礼摄像脚本09-24

CS各种脚本解释全解包含经典复活变身脚本09-24