??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品a久久久久久,精东粉嫩av免费一区二区三区,美女黄视频在线播放http://www.aygfsteel.com/yinzx/category/42831.htmlzh-cnTue, 08 Dec 2009 17:06:29 GMTTue, 08 Dec 2009 17:06:29 GMT60Shell~程基础http://www.aygfsteel.com/yinzx/archive/2009/12/02/304545.htmlyinyinWed, 02 Dec 2009 12:26:00 GMThttp://www.aygfsteel.com/yinzx/archive/2009/12/02/304545.htmlhttp://www.aygfsteel.com/yinzx/comments/304545.htmlhttp://www.aygfsteel.com/yinzx/archive/2009/12/02/304545.html#Feedback0http://www.aygfsteel.com/yinzx/comments/commentRss/304545.htmlhttp://www.aygfsteel.com/yinzx/services/trackbacks/304545.htmlShell~程基础

?gu)Ubuntu中文

本文作者:(x)Leal

授权许可Q?

~辑人员QFireHare, Dbzhang800


我们可以使用L一U文字编辑器Q比如gedit、kedit、emacs、vi{来~写shell脚本Q它必须以如下行开始(必须攑֜文g的第一行)(j)Q?

# !/bin/sh
...


注意Q最好?#8220;!/bin/bash”而不?#8220;!/bin/sh”Q如果用tc shell改ؓ(f)tcshQ其他类伹{?

W号#!用来告诉pȝ执行该脚本的E序Q本例?bin/sh。编辑结束ƈ保存后,如果要执行该脚本Q必d使其可执行:(x)

chmod +x filename

此后在该脚本所在目录下Q输?./filename 卛_执行该脚本?


目录

[隐藏]

[~辑] 变量赋值和引用

Shell~程中,使用变量无需事先声明Q同时变量名的命名须遵@如下规则Q?

  1. 首个字符必须为字母(a-zQA-ZQ?
  2. 中间不能有空|可以使用下划U(_Q?
  3. 不能使用标点W号
  4. 不能使用bash里的关键字(可用help命o(h)查看保留关键字)(j)

需要给变量赋值时Q可以这么写Q?

变量??

要取用一个变量的|只需在变量名前面加一? ( 注意: l变量赋值的时候,不能?="两边留空?)

#!/bin/sh
# 对变量赋|(x)
a="hello world"  #{号两边均不能有I格存在
# 打印变量a的|(x)
echo "A is:" $a

挑个自己喜欢的编辑器Q输入上q内容,q保存ؓ(f)文gfirstQ然后执?chmod +x first 使其可执行,最后输?./first 执行该脚本。其输出l果如下Q?nbsp;

A is: hello world

有时候变量名可能?x)和其它文字hQ比如:(x)

num=2
echo "this is the $numnd"

上述脚本q不?x)输?this is the 2nd"而是"this is the "Q这是由于shell?x)去搜?ch)变量numnd的|而实际上q个变量此时q没有倹{这Ӟ我们可以用花括号来告诉shell要打印的是num变量Q?

num=2
echo "this is the ${num}nd"

其输出结果ؓ(f)Qthis is the 2nd


需要注意shell的默认赋值是字符串赋倹{比如:(x)

var=1
var=$var+1
echo

打印出来的不?而是1Q?。ؓ(f)?jin)达到我们想要的效果有以下几U表达方式:(x)

let "var+=1"
var=$[$var+1]
var=`expr $var + 1`#注意加号两边的空|否则q是按照字符串的方式赋倹{?

注意Q前两种方式在bash下有效,在sh下会(x)出错?

let表示数学q算Qexpr用于整数D,每一用I格隔开Q?[]中括号内的表达式作为数学运先计算l果再输出?

Shell脚本中有许多变量是系l自动设定的Q我们将在用到这些变量时再作说明。除?jin)只在脚本内有效的普通shell变量外,q有环境变量Q即那些由export关键字处理过的变量。本文不讨论环境变量Q因为它们一般只在登录脚本中用到?

[~辑] Shell里的程控制

[~辑] if 语句

"if"表达式如果条件ؓ(f)真,则执行then后的部分:

if ....; then
....
elif ....; then
....
else
....
fi

大多数情况下Q可以用测试命令来Ҏ(gu)件进行测试,比如可以比较字符丌Ӏ判断文件是否存在及(qing)是否可读{等……通常? [ ] "来表C条件测试,注意q里的空格很重要Q要保Ҏ(gu)号前后的I格?

[ -f "somefile" ] Q判断是否是一个文?
[ -x "/bin/ls" ] Q判?bin/ls是否存在q有可执行权?
[ -n "$var" ] Q判?var变量是否有?
[ "$a" = "$b" ] Q判?a?b是否相等

执行man test可以查看所有测试表辑ּ可以比较和判断的cd。下面是一个简单的if语句Q?

#!/bin/sh
if [ ${SHELL} = "/bin/bash" ]; then
echo "your login shell is the bash (bourne again shell)"
else
echo "your login shell is not bash but ${SHELL}"
fi

变量$SHELL包含有登录shell的名Uͼ我们拿它?bin/bashq行比较以判断当前用的shell是否为bash?

[&& ?|| 操作W?/span>

熟?zhn)C语言的朋友可能会(x)喜欢下面的表辑ּQ?

[ -f "/etc/shadow" ] && echo "This computer uses shadow passwords"

q里?&& 是一个快h作符Q如果左边的表达式ؓ(f)真则执行双的语句,你也可以把它看作逻辑q算里的与操作。上q脚本表C如?etc/shadow文g存在Q则打印”This computer uses shadow passwords”。同样shell~程中还可以用或操作(||)Q例如:(x)

#!/bin/sh
mailfolder=/var/spool/mail/james
[ -r "$mailfolder" ] || { echo "Can not read $mailfolder" ; exit 1; }
echo "$mailfolder has mail from:"
grep "^From " $mailfolder

该脚本首先判断mailfolder是否可读Q如果可d打印该文件中?From" 一行。如果不可读则或操作生效Q打印错误信息后脚本退出。需要注意的是,q里我们必须使用如下两个命o(h)Q?

-打印错误信息
-退出程?

我们使用花括号以匿名函数的Ş式将两个命o(h)攑ֈ一起作Z个命令用;普通函数稍后再作说明。即使不用与和或操作W,我们也可以用if表达式完成Q何事情,但是使用与或操作W会(x)更便利很??

[~辑] case 语句

case表达式可以用来匹配一个给定的字符Ԍ而不是数字(可别和C语言里的switch...casehQ?

case ... in
...) do something here ;;
esac

让我们看一个例子,file命o(h)可以辨别Z个给定文件的文gcdQ如Qfile lf.gzQ其输出l果为:(x)

lf.gz: gzip compressed data, deflated, original filename,
last modified: Mon Aug 27 23:09:18 2001, os: Unix

我们利用q点写了(jin)一个名为smartzip的脚本,该脚本可以自动解压bzip2, gzip和zip cd的压~文Ӟ(x)

 #!/bin/sh
ftype=`file "$1"`   # Note ' and ` is different
case "$ftype" in
"$1: Zip archive"*)
unzip "$1" ;;
"$1: gzip compressed"*)
gunzip "$1" ;;
"$1: bzip2 compressed"*)
bunzip2 "$1" ;;
*) echo "File $1 can not be uncompressed with smartzip";;
esac

你可能注意到上面使用?jin)一个特D变?1Q该变量包含有传递给该脚本的W一个参数倹{也是_(d)当我们运行:(x)

smartzip articles.zip

$1 是字符?articles.zip?

[~辑] select 语句

select表达式是bash的一U扩展应用,擅长于交互式场合。用户可以从一l不同的gq行选择Q?

select var in ... ; do
 break;
done
.... now $var can be used ....

下面是一个简单的CZQ?

#!/bin/sh
echo "What is your favourite OS?"
select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do
    break;
done
echo "You have selected $var"
  1. 如果 以上脚本q行出现 select QNOT FOUND ?#!/bin/sh 改ؓ(f) #!/bin/bash 找了(jin)半天才找到的{案

该脚本的q行l果如下Q?

What is your favourite OS?
1) Linux
2) Gnu Hurd
3) Free BSD
4) Other
#? 1
You have selected Linux

[~辑] while/for 循环

在shell中,可以使用如下循环Q?

while ...; do
....
done

只要试表达式条件ؓ(f)真,则while循环一直运行。关键字"break"用来跛_循环Q而关键字”continue”则可以蟩q一个@环的余下部分Q直接蟩C一ơ@环中?

for循环?x)查看一个字W串行表Q字W串用空格分隔)(j)Qƈ其赋给一个变量:(x)

for var in ....; do
....
done

下面的示例会(x)把A B C分别打印到屏q上Q?

#!/bin/sh
for var in A B C ; do
echo "var is $var"
done

下面是一个实用的脚本showrpmQ其功能是打C些RPM包的l计信息Q?

#!/bin/sh
# list a content summary of a number of RPM packages
# USAGE: showrpm rpmfile1 rpmfile2 ...
# EXAMPLE: showrpm /cdrom/RedHat/RPMS/*.rpm
for rpmpackage in $*; do
if [ -r "$rpmpackage" ];then
echo "=============== $rpmpackage =============="
rpm -qi -p $rpmpackage
else
echo "ERROR: cannot read file $rpmpackage"
fi
done

q里出现?jin)第二个?gu)变量$*Q该变量包含有输入的所有命令行参数倹{如果你q行showrpm openssh.rpm w3m.rpm webgrep.rpmQ那?$* 包含有 3 个字W串Q即openssh.rpm, w3m.rpm?webgrep.rpm?

[Shell里的一些特D符?/span>

[~辑] 引号

在向E序传递Q何参C前,E序?x)扩展通配W和变量。这里所谓的扩展是指E序?x)把通配W(比如*Q替换成适当的文件名Q把变量替换成变量倹{我们可以用引h防止q种扩展Q先来看一个例子,假设在当前目录下有两个jpg文gQmail.jpg和tux.jpg?

#!/bin/sh
echo *.jpg

q行l果为:(x)

mail.jpg tux.jpg

引号Q单引号和双引号Q可以防止通配W?的扩展:(x)

#!/bin/sh
echo "*.jpg"
echo '*.jpg'

其运行结果ؓ(f)Q?

*.jpg
*.jpg

其中单引h严格一些,它可以防止Q何变量扩展;而双引号可以防止通配W扩展但允许变量扩展Q?

#!/bin/sh
echo $SHELL
echo "$SHELL"
echo '$SHELL'

q行l果为:(x)

/bin/bash
/bin/bash
$SHELL

此外q有一U防止这U扩展的Ҏ(gu)Q即使用转义字符——反斜杆:\Q?

echo \*.jpg
echo \$SHELL

输出l果为:(x)



*.jpg
$SHELL

[~辑] Here Document

当要几行文字传递给一个命令时Q用here documents是一U不错的Ҏ(gu)。对每个脚本写一D帮助性的文字是很有用的,此时如果使用here documents׃必用echo函数一行行输出。Here document?<< 开_(d)后面接上一个字W串Q这个字W串q必d现在here document的末。下面是一个例子,在该例子中,我们对多个文件进行重命名Qƈ且用here documents打印帮助Q?

#!/bin/sh
# we have less than 3 arguments. Print the help text:
if [ $# -lt 3 ] ; then
cat << HELP
ren -- renames a number of files using sed regular expressions USAGE: ren 'regexp' 'replacement' files...
EXAMPLE: rename all *.HTM files in *.html:
ren 'HTM$' 'html' *.HTM
HELP
exit 0
fi
OLD="$1"
NEW="$2"
# The shift command removes one argument from the list of
# command line arguments.
shift
shift
# $* contains now all the files:
for file in $*; do
if [ -f "$file" ] ; then
newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
if [ -f "$newfile" ]; then
       echo "ERROR: $newfile exists already"
else
echo "renaming $file to $newfile ..."
mv "$file" "$newfile"
fi
fi
done

q个CZ有点复杂Q我们需要多q旉来说明一番。第一个if表达式判断输入命令行参数是否于3?(Ҏ(gu)变量$# 表示包含参数的个? 。如果输入参数小?个,则将帮助文字传递给cat命o(h)Q然后由cat命o(h)其打印在屏q上。打印帮助文字后E序退出。如果输入参数等于或大于3个,我们将W一个参数赋值给变量OLDQ第二个参数赋值给变量NEW。下一步,我们使用shift命o(h)第一个和W二个参C参数列表中删除,q样原来的第三个参数成为参数列?*的第一个参数。然后我们开始@环,命o(h)行参数列表被一个接一个地被赋值给变量$file。接着我们判断该文件是否存在,如果存在则通过sed命o(h)搜烦(ch)和替换来产生新的文g名。然后将反短斜线内命令结果赋值给newfile。这h们就辑ֈ?jin)目的?x)得到?jin)旧文g名和新文件名。然后?mv命o(h)q行重命?

[~辑] Shell里的函数

如果你写q比较复杂的脚本Q就?x)发现可能在几个地方使用了(jin)相同的代码Q这时如果用上函敎ͼ?x)方便很多。函数的大致样子如下Q?

functionname()
{
# inside the body $1 is the first argument given to the function
# $2 the second ...
body
}

你需要在每个脚本的开始对函数q行声明?

下面是一个名为xtitlebar的脚本,它可以改变终端窗口的名称。这里用了(jin)一个名为help的函敎ͼ该函数在脚本中用了(jin)两次Q?

#!/bin/sh
# vim: set sw=4 ts=4 et:
help()
{
cat << HELP
xtitlebar -- change the name of an xterm, gnome-terminal or kde konsole
USAGE: xtitlebar [-h] "string_for_titelbar"
OPTIONS: -h help text
EXAMPLE: xtitlebar "cvs"
HELP
exit 0
}
# in case of error or if -h is given we call the function help:
[ -z "$1" ] && help
[ "$1" = "-h" ] && help
# send the escape sequence to change the xterm titelbar:
echo -e "33]0;$107"
# 

在脚本中提供帮助是一U很好的~程?fn)惯Q可以方便其他用P和自己)(j)使用和理解脚本?

== 命o(h)行参?== XXXXXXXXXXXXXXXXXXXXXXXXXX

我们已经见过$* ?$1, $2 ... $9 {特D变量,q些Ҏ(gu)变量包含?jin)用户从命o(h)行输入的参数。迄今ؓ(f)止,我们仅仅?jin)解了(jin)一些简单的命o(h)行语法(比如一些强制性的参数和查看帮助的-h选项Q。但是在~写更复杂的E序Ӟ(zhn)可能会(x)发现(zhn)需要更多的自定义的选项。通常的惯例是在所有可选的参数之前加一个减P后面再加上参数?(比如文g??

有好多方法可以实现对输入参数的分析,但是下面的用case表达式的例子无疑是一个不错的Ҏ(gu)?

#!/bin/sh
help()
{
cat << HELP
This is a generic command line parser demo.
USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2
HELP
exit 0
}
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;; # function help is called
-f) opt_f=1;shift 1;; # variable opt_f is set
-l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2
--) shift;break;; # end of options
-*) echo "error: no such option $1. -h for help";exit 1;;
*) break;;
esac
done
echo "opt_f is $opt_f"
echo "opt_l is $opt_l"
echo "first arg is $1"
echo "2nd arg is $2"

你可以这栯行该脚本Q?

cmdparser -l hello -f -- -somefile1 somefile2

q回l果如下Q?

opt_f is 1
opt_l is hello
first arg is -somefile1
2nd arg is somefile2

q个脚本是如何工作的呢?脚本首先在所有输入命令行参数中进行@环,输入参Ccase表达式进行比较,如果匚w则设|一个变量ƈ且移除该参数。根据unixpȝ的惯例,首先输入的应该是包含减号的参数?

[~辑] Shell脚本CZ

=== 一般编E步?== xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

现在我们来讨论编写一个脚本的一般步骤。Q何优U的脚本都应该h帮助和输入参数。写一个框架脚本(framework.shQ,该脚本包含了(jin)大多数脚本需要的框架l构Q是一个非怸错的L。这样一来,当我们开始编写新脚本Ӟ可以先执行如下命令:(x)

cp framework.sh myscript

然后再插入自q函数?

让我们来看看如下两个CZ?

[~辑] 二进制到十进制的转换

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

#!/bin/sh
# vim: set sw=4 ts=4 et:
help()
{
cat << HELP
b2d -- convert binary to decimal
USAGE: b2d [-h] binarynum
OPTIONS: -h help text
EXAMPLE: b2d 111010
will return 58
HELP
exit 0
}
error()
{
# print an error and exit
echo "$1"
exit 1
}
lastchar()
{
# return the last character of a string in $rval
if [ -z "$1" ]; then
# empty string
rval=""
return
fi
# wc puts some space behind the output this is why we need sed:
numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
# now cut out the last char
rval=`echo -n "$1" | cut -b $numofchar`
}
chop()
{
# remove the last character in string and return it in $rval
if [ -z "$1" ]; then
# empty string
rval=""
return
fi
# wc puts some space behind the output this is why we need sed:
numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
if [ "$numofchar" = "1" ]; then
# only one char in string
rval=""
return
fi
numofcharminus1=`expr $numofchar "-" 1`
# now cut all but the last char:
rval=`echo -n "$1" | cut -b -$numofcharminus1`
#原来?rval=`echo -n "$1" | cut -b 0-${numofcharminus1}`q行时出?
#原因是cut?开始计敎ͼ应该是cut -b 1-${numofcharminus1}
}
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;; # function help is called
--) shift;break;; # end of options
-*) error "error: no such option $1. -h for help";;
*) break;;
esac
done
# The main program
sum=0
weight=1
# one arg must be given:
[ -z "$1" ] && help
binnum="$1"
binnumorig="$1"
while [ -n "$binnum" ]; do
lastchar "$binnum"
if [ "$rval" = "1" ]; then
sum=`expr "$weight" "+" "$sum"`
fi
# remove the last position in $binnum
chop "$binnum"
binnum="$rval"
weight=`expr "$weight" "*" 2`
done
echo "binary $binnumorig is decimal $sum"
#

该脚本用的法是利用十q制和二q制数权?(1,2,4,8,16,..)Q比如二q制"10"可以q样转换成十q制Q?

0 * 1 + 1 * 2 = 2

Z(jin)得到单个的二q制数我们是用了(jin)lastchar 函数。该函数使用wc –c计算字符个数Q然后用cut命o(h)取出末尾一个字W。Chop函数的功能则是移除最后一个字W?

[~辑] 文g循环拯

你可能有q样的需求ƈ一直都q么做:(x)所有发出邮件保存到一个文件中。但是过?jin)几个月之后Q这个文件可能会(x)变得很大以至于该文g的访问速度变慢Q下面的脚本 rotatefile 可以解决q个问题。这个脚本可以重命名邮g保存文gQ假设ؓ(f)outmailQؓ(f)outmail.1Q而原来的outmail.1变成了(jin) outmail.2 {等...

#!/bin/sh
# vim: set sw=4 ts=4 et:
ver="0.1"
help()
{
cat << HELP
rotatefile -- rotate the file name
USAGE: rotatefile [-h] filename
OPTIONS: -h help text
EXAMPLE: rotatefile out
This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1[BR]
and create an empty out-file
The max number is 10
version $ver
HELP
exit 0
}
error()
{
echo "$1"
exit 1
}
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;;
--) break;;
-*) echo "error: no such option $1. -h for help";exit 1;;
*) break;;
esac
done
# input check:
if [ -z "$1" ] ; then
error "ERROR: you must specify a file, use -h for help"
fi
filen="$1"
# rename any .1 , .2 etc file:
for n in 9 8 7 6 5 4 3 2 1; do
if [ -f "$filen.$n" ]; then
p=`expr $n + 1`
echo "mv $filen.$n $filen.$p"
mv $filen.$n $filen.$p
fi
done
# rename the original file:
if [ -f "$filen" ]; then
echo "mv $filen $filen.1"
mv $filen $filen.1
fi
echo touch $filen
touch $filen

q个脚本是如何工作的呢?在检到用户提供?jin)一个文件名之后Q首先进行一??的@环;文g?9重命名ؓ(f)文g?10Q文件名.8重命名ؓ(f)文g? 9……{等。@环结束之后,把原始文件命名ؓ(f)文g?1Q同时创Z个和原始文g同名的空文gQtouch $filenQ?

[~辑] 脚本调试

最单的调试Ҏ(gu)当然是用echo命o(h)。你可以在Q何怀疑出错的地方用echo打印变量|q也是大部分shellE序员花?0%的时间用于调试的原因。Shell脚本的好处在于无需重新~译Q而插入一个echo命o(h)也不需要多时间?

shell也有一个真正的调试模式Q如果脚?strangescript"出错Q可以用如下命令进行调试:(x)

sh -x strangescript

上述命o(h)?x)执行该脚本Q同时显C所有变量的倹{?

shellq有一个不执行脚本只检查语法的模式Q命令如下:(x)

sh -n your_script

q个命o(h)?x)返回所有语法错误?

我们希望你现在已l可以开始编写自qshell脚本?jin),情享受q䆾乐趣吧! :)



yin 2009-12-02 20:26 发表评论
]]>
Linux创徏文ghttp://www.aygfsteel.com/yinzx/archive/2009/11/24/303485.htmlyinyinTue, 24 Nov 2009 08:16:00 GMThttp://www.aygfsteel.com/yinzx/archive/2009/11/24/303485.htmlhttp://www.aygfsteel.com/yinzx/comments/303485.htmlhttp://www.aygfsteel.com/yinzx/archive/2009/11/24/303485.html#Feedback0http://www.aygfsteel.com/yinzx/comments/commentRss/303485.htmlhttp://www.aygfsteel.com/yinzx/services/trackbacks/303485.html 本文讲解?jin)Linux创徏文g命o(h)的方?,希望Ҏ(gu)的学?fn)有所帮助?br />
创徏文g?

mkdir aaa

mkdir -p aaa

创徏文g命o(h)

vi foo.txt

emacs foo.txt

echo "aaaa" > foo.txt

cat > foo.txt

清除命o(h)

> foo.txt

mkdir /home/u1 创徏文g?home/u1

chown oracle /home/u1 表示改变目录所有者ؓ(f)oracle账户;

chgrp dba /home/u1 改变/home/u1目录为dba所?

chmod 755 /home/u1 表示oracle账户?home/u1目录?55权限;

rmdir /home/u1 表示删除/home/u1目录

hostname可以查看linux的计机?

whoami可以查看当前用户;

pwd昄当前路径;

df查看pȝ的硬件信?br />
ls -lrt l表示昄详细列表Q?t表示按时间排?-r反向排序

cat orcl_ora_3436.trc|grep bucket

以下查看相关文g内容:

more /etc/oratab

cat /etc/passwd

cat /etc/group

以上是Linux创徏文g命o(h)的方?br /> 本文作?未知



yin 2009-11-24 16:16 发表评论
]]>
վ֩ģ壺 | ʷ| ־| ͨ| | ˼| | ϰ| | ̨| ʡ| | Ӷ| | Դ| ̨| | | | ǿ| | κ| | | Ϫ| | ¹| | ɽ| | ij| | ¦| ۩| | 㽭ʡ| | | ƽ| | |