/usr/WebSphere/IHS/logs/access_log>/usr/WebSphere/IHS/logs/error_logapforwinbk.shexportDayOfWeek=`date+"%u"`cd/wasbackupcp-rf./WASBackup_"$DayOfWeek".tar./apbkarchive" />
当前位置:文档之家› 很实用的Shell脚本(实践版)

很实用的Shell脚本(实践版)

很实用的Shell脚本(实践版)
很实用的Shell脚本(实践版)

ApacheLogRotate.sh

echo`date`":Clean the apache logs"

>/usr/WebSphere/IHS/logs/access_log

>/usr/WebSphere/IHS/logs/error_log

apforwinbk.sh

export DayOfWeek=`date+"%u"`

cd/wasbackup

cp-rf./WASBackup_"$DayOfWeek".tar./apbk

archiveOld.sh

export TargetDir=/erpHome/jln.ear/erp.war/$1/$2

export TT=`date+"%Y%m%d"`

export DayOfWeek=`date+"%u"`

if(test-e$TargetDir)then

mkdir-p/erpHome/shell/old/$1

cd$TargetDir

#zip-qm/erpHome/old/$1/$2_$TT.zip`find.-mtime+7|awk'{print$0}'` mv/erpHome/shell/old/$1/$2_$DayOfWeek.zip/erpHome/shell/old/$1/$2.zip.old

find.-mtime+7|xargs nice-n20zip-qm /erpHome/shell/old/$1/$2_$DayOfWeek.zip

#find.-mtime+7

Fi

archiveOld2.sh

./erpHome/shell/setP.sh

export DayOfWeek=`date+"%u"`

if test"$#"-lt1||test-z"$1"

then

echo At least one argument needed.

exit

fi

if test!-e"$ShellHome/old/$1"

then

cd$ShellHome/old/

mkdir-p$1

fi

if test!-e"$ERPHome/$1"

then

echo"$ERPHome/$1""does not exist"

exit

fi

cd$ERPHome/$1

mv$ShellHome/old/$1_$DayOfWeek.zip$ShellHome/old/$1.zip.old

find.-mtime+7-type f|xargs nice-n20zip-qm$ShellHome/old/$1_$DayOfWeek.zip #find.-mtime+7-type f

archiveOldAll.sh

/erpHome/shell/archiveOld.sh work/dr/engine jrprint

/erpHome/shell/archiveOld.sh work/de tmp

/erpHome/shell/archiveOld2.sh public

backupLog.sh

echo`date`":Backup the logs"

./erpHome/shell/setP.sh

cd$ERPHome/waslogs

mkdir old/

mv std*_*.txt old/

mv$LOGHome/LOG_$DD.zip$LOGHome/LOG.zip.old

zip-qmr$LOGHome/LOG_$DD.zip`ls$ERPHome/waslogs|grep-Ev"std|FATAL_DEBUG.xml"` ls-l$LOGHome/LOG_$DD.zip

echo`date`":Backup the logs done."

backupWASAll.sh

./erpHome/shell/setP.sh

cd/erpHome

#rename old backup file

echo"####"`date`"ERP program backup####"

#mv/wasbackup/WASBackup_"$DayOfWeek".tar/wasbackup/WASBackup_old.tar

rm-Rf/wasbackup/erpbak/WASBackup_"$DayOfWeek".tar

#backup ERP program

nice-n20tar-cf/wasbackup/erpbak/WASBackup_"$DayOfWeek".tar jln.ear/erp.war DIClient DIServer ThreadPool erpdata

ls-l/wasbackup/erpbak/WASBackup_"$DayOfWeek".tar

collectAudit.sh

./erpHome/shell/setP.sh jln

audit shutdown

auditpr-v$LOGHome/AUDIT_$YYMMDD.txt

mv/audit/trail$LOGHome/trail_$TT

gzip$LOGHome/trail_$TT

audit start

last|grep"$TT2">$LOGHome/LOGIN_$YYMMDD.txt

grep"$TT3"/var/adm/sulog>$LOGHome/SU_$YYMMDD.txt

cplogs.sh

export dd=`perl-e'@y=localtime(time()-86400);printf"%02d\n",$y[3];'`

cd/erpHome/shell/logs

mkdir/test/erplogs

cp./LOG_"$dd".zip/test/erplogs

cpnmon.sh

cd/worktmp/nmon

export dd=`perl-e'@y=localtime(time()-86400);printf"%02d\n",$y[3];'` export d=`date+%y%m`"$dd"

export HOSTNAME=`hostname`

export data="$HOSTNAME"_"$d"_"*"

ftp-n<

open10.32.1.138

user nmon nmon

cd$HOSTNAME

put$data

bye

EOF

ddd.sh

cd$1

touch11.txt

filter.awk

BEGIN{

DiscardURLs="/erp/de/deCommand/erp/jsp/"

DiscardAmount=split(DiscardURLs,DiscardURL,"")

ResponseThreshold=4.9999

nlines=0

totalCount=0

}

function extractTime(myDate,aSize){

if(aSize==2)return myDate[2]

if(myDate[2]=="上午")return myDate[3]

split(myDate[3],dates,":")

return dates[1]+12":"dates[2]":"dates[3]

}

function isSkip(myURL){

for(i=1;i<=DiscardAmount;i++)

if(index(myURL,DiscardURL[i])>0)return1

return0

}

{

if(isSkip($3)==0){

if(NF==10){

totalCount++;

if($4>ResponseThreshold){

mySize=split($2,myDate,"")

myTime=extractTime(myDate,mySize)

print$1"\t"myTime"\t"$3"\t"$4"\t"$5"\t"$6

nlines++

}

}

}

}END{

print"本日网页执行总次数:"totalCount"(JSP,servlet,但不包含图片等静态网页,也不包含/erp/jsp/开头的这些监控画面)"

print"共"nlines"笔异常资料"

formatHTML.awk

BEGIN{

}

function getSysName(myURL){

fCount=split(myURL,myArray,"/")

return myArray[3]

}

function getMaxInArray(oArray,oResult,oIndex){

maxValue=0

for(idx in oCount){

if(oCount[idx]>maxValue){

maxValue=oCount[idx]

maxSys=idx

}

}

delete oArray[maxSys]

oResult[oIndex,1]=maxSys

oResult[oIndex,2]=maxValue

}

function sort(array,result,thresHold){

for(i=1;i<=5;i++){

getMaxInArray(array,result,i)

}

}

{

oSys=getSysName($3)

if(NF!=6){

print$0"
"

}

else{

oCount[oSys]++

oRecord++;

print""$1""$2 ""$3""$4""$5""$6""

}

}END{

oThreshold oRecord/10

sort(oCount,oResult,oThreshold)

print""

print"

htmlHead.txt

执行时间超过5秒的网址

increaseERP.sh

./erpHome/shell/setP.sh

cd/erpHome

find jln.ear-type f-mtime2-print>/tmp/filelist.txt

find erpdata-type f-mtime2-print>>/tmp/filelist.txt

tar cvfL/wasbackup/WASBackup_"$DayOfWeek".tar/tmp/filelist.txt

night.old

./erpHome/shell/setP.sh

$ShellHome/backupLog.sh>>$NightLog2>&1

$ShellHome/ApacheLogRotate.sh>>$NightLog2>&1

mv/erpHome/bx.ear/erp.war/access.txt/erpHome/shell/logs/access_$YYMMDD.txt

awk-F"\t"-f/erpHome/shell/filter.awk/erpHome/shell/logs/access_$YYMMDD.txt> /erpHome/shell/logs/temp.csv

#echo"ㄓ方筿福, 丁,呼,磅︽ 丁,QueryString,UserID">/erpHome/shell/logs/temp2.csv echo"来源电脑,时间,网址,执行时间,QueryString,UserID">/erpHome/shell/logs/temp2.csv sort-t","+3-n-r/erpHome/shell/logs/temp.csv>>/erpHome/shell/logs/temp2.csv

awk-F","-f/erpHome/shell/formatHTML.awk/erpHome/shell/logs/temp2.csv> $ERPHome/public/$YYMMDD.html

gzip-v/erpHome/shell/logs/access_$YYMMDD.txt>>$NightLog2>&1

sv

#awk-f/erpHome/shell/filter.awk/erpHome/shell/logs/access_$YYMMDD.txt> $ERPHome/public/$YYMMDD.html

#gzip-v/erpHome/shell/logs/access_$YYMMDD.txt>>$NightLog2>&1

night.sh

./erpHome/shell/setP.sh

#/usr/WebSphere/AppServer/bin/stopServer.sh server1>>$NightLog2>&1

$ShellHome/backupLog.sh>>$NightLog2>&1

$ShellHome/ApacheLogRotate.sh>>$NightLog2>&1

#$ShellHome/processFile.sh db_serv20000>>$NightLog2>&1

#mv$ERPHome/access.txt/erpHome/shell/logs/access_$YYMMDD.txt

awk-F"\t"-f/erpHome/shell/filter.awk/erpHome/shell/logs/access_$YYMMDD.txt> /erpHome/shell/logs/temp.csv

sort+3-n-r/erpHome/shell/logs/temp.csv>/erpHome/shell/logs/temp2.csv

cat/erpHome/shell/htmlHead.txt>$ERPHome/public/$YYMMDD.html

awk-F"\t"-f/erpHome/shell/formatHTML.awk/erpHome/shell/logs/temp2.csv>> $ERPHome/public/$YYMMDD.html

cat/erpHome/shell/htmlFoot.txt>>$ERPHome/public/$YYMMDD.html

#gzip-v/erpHome/shell/logs/access_$YYMMDD.txt>>$NightLog2>&1

/erpHome/shell/archiveOldAll.sh>>$NightLog2>&1

#/usr/WebSphere/AppServer/bin/startServer.sh server1>>$NightLog2>&1

night.sh.bak

./erpHome/shell/setP.sh

#/usr/WebSphere/AppServer/bin/stopServer.sh server1>>$NightLog2>&1

$ShellHome/backupLog.sh>>$NightLog2>&1

$ShellHome/ApacheLogRotate.sh>>$NightLog2>&1

#$ShellHome/processFile.sh db_serv20000>>$NightLog2>&1

#mv$ERPHome/access.txt/erpHome/shell/logs/access_$YYMMDD.txt

#awk-F"\t"-f/erpHome/shell/filter.awk/erpHome/shell/logs/access_$YYMMDD.txt> /erpHome/shell/logs/temp.csv

#sort+3-n-r/erpHome/shell/logs/temp.csv>/erpHome/shell/logs/temp2.csv

#cat/erpHome/shell/htmlHead.txt>$ERPHome/public/$YYMMDD.html

#awk-F"\t"-f/erpHome/shell/formatHTML.awk/erpHome/shell/logs/temp2.csv>> $ERPHome/public/$YYMMDD.html

#cat/erpHome/shell/htmlFoot.txt>>$ERPHome/public/$YYMMDD.html

gzip-v/erpHome/shell/logs/access_$YYMMDD.txt>>$NightLog2>&1

/erpHome/shell/archiveOldAll.sh>>$NightLog2>&1

#/usr/WebSphere/AppServer/bin/startServer.sh server1>>$NightLog2>&1

#

Old

.

馵..@public!IHS.old.zip'public.zip.old#public_5.zip$public_7.zip*public_1.zip%public_2.zip&pu blic_4.zip(public_6.zip)shell_test.zipwork"public_3.zipxjh@p55013:/erpHome/shell#

processFile.sh

export YYMM=`/usr/bin/date+"%y%m"`;export YYMM

export YYMMDD=`/usr/bin/date+"%y%m%d"`;export YYMMDD

mkdir-p/nmon/report/$1/$YYMM/$YYMMDD/

cd/nmon

nice-n20./nmon2rrd-f$1_"$YYMMDD"_$2.nmon-d/nmon/report/$1/$YYMM/$YYMMDD -x

gzip-9/nmon/report/$1/$YYMM/$YYMMDD/*rrd*

gzip-9$1_"$YYMMDD"_$2.nmon

res.sh

/erpHome/shell/stopServer.sh$1

/erpHome/shell/startServer.sh$1

restartjvm.sh

/usr/WebSphere/AppServer/profiles/AppSrv01/bin/stopServer.sh s1 /usr/WebSphere/AppServer/profiles/AppSrv01/bin/startServer.sh s1 #/usr/WebSphere/AppServer/profiles/AppSrv01/bin/stopServer.sh s2 #/usr/WebSphere/AppServer/profiles/AppSrv01/bin/startServer.sh s2 rmGClog.sh

cd/usr/WebSphere/AppServer/profiles/AppSrv01/logs/s1

>native_stderr.log

cd/usr/WebSphere/AppServer/profiles/AppSrv01/logs/s2

>native_stderr.log

cd/usr/WebSphere/AppServer/profiles/AppSrv01/logs/t1

>native_stderr.log

cd/usr/WebSphere/AppServer/profiles/AppSrv01/logs/t2

rmwasbackup.sh

cd/wasbackup

find.-ctime+1-exec rm{}\;

server.lst

server1

setP.sh

export ERPHome=/erpHome/jln.ear/erp.war

if!test-e$ERPHome

then

echo"Warning!"$ERPHome"not exists."

exit

fi

export ShellHome=/erpHome/shell

export LOGHome=/erpHome/shell/logs

export WAS_HOME=/usr/WebSphere/AppServer

export Java_Home=$WAS_HOME/java/

export TT=`date+"%Y%m%d%H%M%S"`

export TT2=`date+"%b%d"`

export TT3=`date+"%m/%d"`

export YYMMDD=`date+"%Y%m%d"`

export DD=`date+"%d"`

export UpdateLog=$LOGHome/update_$YYMMDD.txt export NightLog=$LOGHome/night_$YYMMDD.txt

export DayOfWeek=`date+"%u"`

startAllService.sh

/usr/WebSphere/IHS/bin/apachectl start

/erpHome/ThreadPool/bin/go.sh start

/erpHome/DIServer/bin/go.sh start

/usr/WebSphere/AppServer/bin/startServer.sh server1

startServer.sh

#!/usr/bin/ksh

if test"$#"-lt1||test-z"$1"

then

echo Usage:$0[ServerName]

echo"Available ServerName:"

awk'{print"\t\t\t"$1}'/erpHome/shell/server.lst

exit

fi

IFSsave=$IFS

while IFS='';read line

do

if test$1=$line

then

export ValidArg=1

break

fi

done

IFS=$IFSsave

if test-z"$ValidArg"

then

echo Invalid ServerName.

echo"Available ServerName:"

awk'{print"\t\t\t"$1}'/erpHome/shell/server.lst

exit

fi

export ServerName=$1

echo"####################start$1`date`####################">> /erpHome/shell/logs/serverRestart.log

/usr/WebSphere/AppServer/bin/startServer.sh$1

stopServer.sh

#!/usr/bin/ksh

if test"$#"-lt1||test-z"$1"

then

echo Usage:$0[ServerName]

echo"Available ServerName:"

awk'{print"\t\t\t"$1}'/erpHome/shell/server.lst

exit

fi

IFSsave=$IFS

while IFS='';read line

do

if test$1=$line

then

export ValidArg=1

break

fi

done

IFS=$IFSsave

if test-z"$ValidArg"

then

echo Invalid ServerName.

echo"Available ServerName:"

awk'{print"\t\t\t"$1}'/erpHome/shell/server.lst

exit

fi

export ServerName=$1

echo"####################stop$1`date`####################">> /erpHome/shell/logs/serverRestart.log

/usr/WebSphere/AppServer/bin/stopServer.sh$1

syncConfig.sh

cp/erpHome/jln.ear/erp.war/WEB-INF/web.xml

/usr/WebSphere/AppServer/profiles/Dmgr01/config/cells/p55013Cell01/applications/jln.ear/ deployments/jln/erp.war/WEB-INF

cp/erpHome/jln.ear/erp.war/WEB-INF/web.xml

/usr/WebSphere/AppServer/profiles/AppSrv01/config/cells/p55013Cell01/applications/jln.ear /deployments/jln/erp.war/WEB-INF

/usr/WebSphere/AppServer/profiles/Dmgr01/bin/GenPluginCfg.sh

syncConfig.sh.bak

cp/erpHome/jln.ear/erp.war/WEB-INF/web.xml

/usr/WebSphere/AppServer/profiles/Dmgr01/config/cells/p510Cell01/applications/jln.ear/de ployments/jln/erp.war/WEB-INF

cp/erpHome/jln.ear/erp.war/WEB-INF/web.xml

/usr/WebSphere/AppServer/profiles/AppSrv01/config/cells/p510Cell01/applications/jln.ear/d eployments/jln/erp.war/WEB-INF

/usr/WebSphere/AppServer/profiles/Dmgr01/bin/GenPluginCfg.sh

updateFTPSrc.sh

./erpHome/shell/setP.sh

echo`date`>$ERPHome/work/da/log/update/last.log

export DAHome=/home/ftpuser/pub

if!test$(find$DAHome-type f|wc-l)-eq0

then

echo`date`":Update source">>$UpdateLog2>&1

echo"################Update the source################">> $UpdateLog2>&1

echo$ERPHome>>$UpdateLog2>&1

/usr/bin/cp-r$DAHome/*$ERPHome>>$UpdateLog2>&1

#/usr/bin/cp-r$DAHome/*/erpHome/shell/src>>$UpdateLog2>&1

#/usr/bin/rm-fre$DAHome/*>>$UpdateLog2>&1

find$DAHome-type f-exec\rm-e{}\;>>$UpdateLog2>&1

#echo"copy">>$UpdateLog2>&1

#mail-s"$1update program"root<$UpdateLog

echo`date`":Update source done">>$UpdateLog2>&1

fi

updateSource.sh

./erpHome/shell/setP.sh

$ShellHome/updateSrc.sh bin

$ShellHome/updateSrc.sh toc

$ShellHome/updateFTPSrc.sh

updateSrc.sh

./erpHome/shell/setP.sh

echo`date`>$ERPHome/work/da/log/update/last.log

export DAHome=$ERPHome/work/da/$1

if!test$(find$DAHome-type f|wc-l)-eq0

then

echo`date`":Update source">>$UpdateLog2>&1

echo"################Update the source################">> $UpdateLog2>&1

echo$ERPHome>>$UpdateLog2>&1

/usr/bin/cp-r$DAHome/*$ERPHome>>$UpdateLog2>&1

#/usr/bin/cp-r$DAHome/*/erpHome/shell/src>>$UpdateLog2>&1

#/usr/bin/rm-fre$DAHome/*>>$UpdateLog2>&1

find$DAHome-type f-exec\rm-e{}\;>>$UpdateLog2>&1

#echo"copy">>$UpdateLog2>&1

#mail-s"$1update program"root<$UpdateLog

echo`date`":Update source done">>$UpdateLog2>&1

fi

viewHtml.sh

./erpHome/shell/setP.sh

mv$ERPHome/access.txt/erpHome/shell/logs/access_$YYMMDD.txt

awk-F"\t"-f/erpHome/shell/filter.awk/erpHome/shell/logs/access_$YYMMDD.txt> /erpHome/shell/logs/temp.csv

Shell脚本编程详解-吐血共享

第12章 Shell 脚本编程 ● Shell 命令行的运行 ● 编写、修改权限和执行Shell 程序的步骤 ● 在Shell 程序中使用参数和变量 ● 表达式比较、循环结构语句和条件结构语句 ● 在Shell 程序中使用函数和调用其他Shell 程序 12-1 Shell 命令行书写规则 ◆ Shell 命令行的书写规则 对Shell 命令行基本功能的理解有助于编写更好的Shell 程序,在执行Shell 命令时多个命令可以在一个命令行上运行,但此时要使用分号(;)分隔命令,例如: [root@localhost root]# ls a* -l;free;df 长Shell 命令行可以使用反斜线字符(\)在命令行上扩充,例如: [root@localhost root]# echo “ this is \ >long command ” This is long command 注意: “>”符号是自动产生的,而不是输入的。 12-2 编写/修改权限及执行Shell 程序的步骤 ◆ 编写Shell 程序 ◆ 执行Shell 程序 Shell 程序有很多类似C 语言和其他程序设计语言的特征,但是又没有程序语言那样复杂。Shell 程序是指放在一个文件中的一系列Linux 命令和实用程序。在执行的时候,通过Linux 操作系统一个接一个地解释和执行每条命令。首先,来编写第一个Shell 程序,从中学习Shell 程序的编写、修改权限、执行过程。

12-2-1 编辑Shell程序 编辑一个内容如下的源程序,保存文件名为date,可将其存放在目录/bin下。 [root@localhost bin]#vi date #! /bin/sh echo “Mr.$USER,Today is:” echo &date “+%B%d%A” echo “Wish you a lucky day !” 注意: #! /bin/sh通知采用Bash解释。如果在echo语句中执行Shell命令date,则需要在date 命令前加符号“&”,其中%B%d%A为输入格式控制符。 12-2-2 建立可执行程序 编辑完该文件之后不能立即执行该文件,需给文件设置可执行程序权限。使用如下命令。[root@localhost bin]#chmod +x date 12-2-3 执行Shell程序 执行Shell程序有下面三种方法: 方法一: [root@localhost bin]#./ date Mr.root,Today is: 二月 06 星期二 Wish you a lucky day ! 方法二: 另一种执行date的方法就是把它作为一个参数传递给Shell命令: [root@localhost bin]# Bash date Mr.root,Today is: 二月 06 星期二 Wish you a lucky day ! 方法三: 为了在任何目录都可以编译和执行Shell所编写的程序,即把/bin的这个目录添加到整个环境变量中。 具体操作如下: [root@localhost root]#export PATH=/bin:$PATH [root@localhost bin]# date Mr.root,Today is: 二月 06 星期二 Wish you a lucky day !

Shell脚本及linux命令学习

Shell脚本及linux命令学习 ?echo "内容" |mail -s "标题" "收件人" mail [邮箱] <

个人整理shell脚本编程笔记

shell脚本编程学习笔记(一) 一、脚本格式 vim shell.sh #!/bin/bash//声明脚本解释器,这个‘#’号不是注释,其余是注释 #Program://程序内容说明 #History://时间和作者 二、shell变量的种类 用户自定义变量:由用户自己定义、修改和使用 与定义变量:Bash与定义的特殊变量,不能直接修改 位置变量:通过命令行给程序传递执行的参数 1、定义变量: 变量名要以英文字母或下划线开头,区分大小写。 格式:变量名=值 输出变量:echo $变量名 2、键盘输入为变量内容: 格式:read [-p "信息"] 变量名 如:read -p "pewase input your name:" name 3、不同引号对变量的作用 双引号"":可解析变量,$符号为变量前缀。 单引号'':不解析变量,$为普通字符。 反引号``:将命令执行的结果输出给变量。 三、shell条件测试 1、test命令: 用途:测试特定的表达式是否成立,当条件成立时,命令执行后的返回值为0,否则为其他数字。 格式:test 条件表达式[ 条件表达式] (注意:[]与表达式之间存在空格) 2、常见的测试类型: 测试文件状态 格式:[ 操作符文件或目录] 如:if [ -d /etc ] then echo "exists" else echo "not exists" fi 常见的测试操作符: -d:测试是否为目录 -e:测试目录或文件是否存在 -f:测试是否为文件 -r:测试当前用户是否有读权限 -w:测试当前用户是否有写权限 -x:测试当前用户是否有执行权限

如何做才能学好Shell脚本的经验总结

如何做才能学好Shell脚本的经验总结 大多同学反馈Shell脚本不容易学,感觉学完了Shell脚本这部分课程,还是不能写出脚本来。我来帮大家分析一下原因,并解说如何做才能学好Shell脚本。 之所以你觉得难学,写不出东西来。是因为,太生疏。写Shell脚本其实跟婴儿学走步,学骑自行车或者学游泳一样,这是一个熟练活儿。 要想写出一手漂亮的脚本,我总结了以下几点: 1 打好基本功 Shell脚本,本质上是多条Linux系统命令加上逻辑(条件、循环)构成的一个文本文档。要想实现一个需求,只要你可以用系统命令实现,那Shell脚本就可以实现。如果你Linux 命令都不会,那怎么能写出脚本来? 2 学好正则 如果你多写几个Shell脚本,就会发现,好多需求是从一个大字符串里截取出小字符串。这就需要你对正则表达式十分熟练,操作正则的几个工具也要熟练使用(grep/sed/awk)。 3 逻辑思维能力 大型的Shell脚本中,有很强的逻辑,条件满足时做什么,不满足时做什么。条件里面还可以嵌套条件,也可以嵌套循环(for、while等)。 这种逻辑能力并不是我们生来具备的,它是需要不断练习、长期积累出来的功力。 你可能会问,哪里有那么多的脚本需求?关键是没有机会练习啊。是的,这的确是个问题,尤其是正在学习阶段的同学,平时哪里有什么需求啊。要是上班了还好,每天有很多工作做,生产环境中也有不少需求用Shell脚本来实现。 对于学习阶段的同学,我考虑到你们的处境,所以去年花了几个月时间,在公众号里坚持每日发一个Shell练习题,这些资源你可以利用起来。其实,有不少同学只坚持了一个月就已经感觉到了明显的提升。

shell脚本精华总结

1.shell做解释程序,非交互地执行脚本,两种执行方式: 用sh命令执行脚本文件sh hello.sh 给脚本文件添加执行权限,用./命令执行chmod 755 hello.sh ./hello.sh 2.vim (1)必须从命令模式转入插入模式才能进行输入编辑,可用命令有: 新增 (append) a :从光标所在位置后面开始新增资料 A:从光标所在行最后面的地方开始新增资料。 插入 (insert) i:从光标所在位置前面开始插入资料 I :从光标所在行的第一个非空白字元前面开始插入资料。 开始 (open) o :在光标所在行下新增一列并进入输入模式 O: 在光标所在行上方新增一列并进入输入模式 (2)冒号进入行命令模式,输入set nu可设置vi显示行号 :/string 搜索指定的字符串。 按键n:继续进行搜索 如果写的是斜杠(/):按n会从当前位置向后查找;如果写的是问号(?),按n键则从当前位置向前查找 查找且替换 :%s /SEARCH /REPLACE 把文档中所有SEARCH替换成REPLACE; :#,# s /SEARCH /REPLACE /g #号表示数字,表示从多少行到多少行,把SEARCH替换成REPLACE; :s /SEARCH /REPLACE /g 把当前光标所在行中的SEARCH单词,替换成REPLACE,并把所有SEARCH高亮显示 例题: 1. mkdir /tmp/vitest 2. cd /tmp/vitest 3. cp /etc/man.config . 4. vi man.config 5. :set nu 6. 先按下 58G 再按下40→ 会看到 /dir/bin/foo 这个字样在双引号内; 7. 先执行 1G 或 gg 后,直接输入 /bzip2 8. 直接执行 :50,100s/man/MAN/gc 即可 9. (1)简单的方法可以一直按 u 恢复到原始状态,(2)使用不储存离开 :q! 之后,再重新读取一次该文件; 10. 51G 然后再 10yy 之后按下 G 到最后一行,再p粘贴10行 11. 11G 之后,再给他 20dd即可删除 20 行了; 12. :w man.test.config 13. 29G 之后,再给他 15x 即可删除 15 个字符; 14. :wq! #!/bin/bash 必须的,指出shell的类型

Shell脚本学习指南笔记

入门 Shell脚本最常用于系统管理工作,或者用于结合现有的程序以完成小型、特定的工作。 脚本编程语言与编译型语言的差异 许多中、大型程序都是编译型语言,如Fortran、Ada、C、C++或者Java(有些特殊)。这类程序只要从源代码(Source Code)转换成目标代码(Object Code)便能直接通过计算机执行。这样的好处是高效,缺点是它们多半运行于底层,处理的是字节、数字或是机器层级的对象,很难进行“将一个目录里所有文件复制到另一个目录中”这类对文件的简单操作。 脚本语言通常是解释型(interpreted)的,由解释器(interpreter)读入程序代码,将其转换成内部形式。好处是它们多半运行在比编译型语言高的层次,能够轻易处理文件与目录之类的对象,缺点是效率不如编译型。但是编写更快,而且目前的速度也已经足够快,常用脚本语言有:awk、Perl、Python、Ruby与Shell。Shell的特点有: 1.简单性 2.可移植性 3.开发容易 一个简单的脚本 who命令可以知道系统有谁登陆,如果有很多用户在登陆,结果会很长,可以使用wc(字数统计)命令,算出行数(line)、字数(word)和字符数(character)。可以使用wc –l,只计算出行数$ who | wc –l | 管道符号可以在两个命令之间建立管道(pipeline):who的输出成为了wc的输入。下面就写一个shell脚本将管道转变成一个独立的命令。 其实在Shell中开发周期很类似,先直接在命令行上测试,之后写入到独立的脚本中。 第一行的#! 当Shell执行一个程序时,要求Unix内核启动一个新的进程(process),在该进程里执行指定的程序。内核知道怎样为编译型程序执行,但Shell是解释型程序,当Shell要求内核执行时,内核无法执行,会回应”not executable format file”不是可执行的格式文件的错误信息。Shell收到此错误信息时,就会确定不是编译型程序,是Shell脚本,接着会启动一个新的/bin/sh副本来执行该程序。 在当前有很多种Shell,需要通过第一行#!来指定用哪个Shell来执行。一般如下: #! 解释器地址选项 比如标准的Shell脚本: #! /bin/sh

Shell 脚本基础学习笔记

Shell 脚本基础学习笔记 shell脚本的解释用“#”号,本文档为了习惯方便,解释大都用“//”,也有用“#”的SHELL 最基本的语法

基本元字符集及其含义(2008-05-24) abc 表示abc 三個連續的字符, 但彼此獨立而非集合. (可簡單視為三個char set) (abc) 表示abc 這三個連續字符的集合. (可簡單視為一個char set) a|b 表示單一字符, 或a 或b . (abc|xyz) 表示或abc 或xyz 這兩個char. set 之一. (註二) [abc] 表示單一字符, 可為a 或b 或c . (與wildcard 之[abc] 原理相同) [^abc] 表示單一字符, 不為a 或b 或c 即可. (與wildcard 之[!abc] 原理相同) ^ 只匹配行首 $ 只匹配行尾 * 只一个单字符后紧跟*,匹配0个或多个此单字符 [ ] 只匹配[ ]内字符。可以是一个单字符,也可以是字符序列。可以使用-表示[ ]内字符序列范围,如用[ 1 - 5 ]代替[ 1 2 3 4 5 ] \ 只用来屏蔽一个元字符的特殊含义。因为有时在s h e l l中一些元字符有特殊含义。\可以使其失去应有意义 . 只匹配任意单字符 p a t t e r n \ { n \ } 只用来匹配前面p a t t e r n出现次数。n为次数 p a t t e r n \ { n,\ }含义同上,但次数最少为n p a t t e r n \ { n,m \ }含义同上,但p a t t e r n出现次数在n与m之间 现在详细讲解其中特殊含义 1、使用句点匹配单字符 例一:beg.n:以beg开头,中间夹一个任意字符。 例二:. . . .X C. . . .:共10个字符,前四个之后为XC 例三:列出所有用户都有写权限的目录或文件: ls -l |grep ...x..x..x 2、行首以^匹配字符串或字符序列 ^只允许在一行的开始匹配字符或单词。 例如,使用ls -l命令,并匹配目录。 $ ls -l | grep ^d 3、在行尾以$匹配字符串或字符 可以说$与^正相反,它在行尾匹配字符串或字符,$符号放在匹配单词后。 例一:列出文件httpd1.conf中所有以单词common结尾的行 $grep common$ httpd1.conf

最全面实用的Linux Shell脚本编程知识点总结

(这些往往是经常用到,但是各种网络上的材料都语焉不详的东西,个人认为比较有用)七种文件类型 d 目录l 符号链接 s 套接字文件 b 块设备文件 c 字符设备文件p 命名管道文件 - 普通文件 正则表达式 从一个文件或命令输出中抽取或过滤文本时。可使用正则表达式(RE),正则表达式是一些特殊或不很特殊的字符串模式的集合。 基本的元字符集: ^ 只匹配行首。 $ 只匹配行尾。 * 一个单字符后紧跟*,匹配0个或多个此单字符。 [] 匹配[]内字符,可以是一个单字符,也可以是字符序列。可以使用-来表示[]内范围,如[1-5]等价于[1,2,3,4,5]。 \ 屏蔽一个元字符的特殊含义,如\$表示字符$,而不表示匹配行 尾。

. 匹配任意单字符。 pattern\{n\} 匹配pattern出现的次数n pattern\{n,\}m匹配pattern出现的次数,但表示次数最少为n pattern\{n,m\} 匹配pattern出现的次数在n与m之间(n,m为0-255)几个常见的例子: 显示可执行的文件:ls –l | grep …x...x..x 只显示文件夹:ls –l | grep ^d 匹配所有的空行:^$ 匹配所有的单词:[A-Z a-z]* 匹配任一非字母型字符:[^A-Z a-z] 包含八个字符的行:^……..$(8个.) 字符类描述 以下是可用字符类的相当完整的列表: [:alnum:] 字母数字[a-z A-Z 0-9] [:alpha:] 字母[a-z A-Z] [:blank:] 空格或制表键 [:cntrl:] 任何控制字符 [:digit:] 数字[0-9]

shell脚本学习与总结

shell脚本学习与总结 1.shell 脚本是区分小写的 2.Unix特殊字符有:( ; $ ? & * () [] ` ‘ “ +使用其时要进行转义() 3.Shell的注释以#开头 4.函数的定义 Function fuction_name(){ Command to execute } 调用时直接用function_name. 5.控制结构 1)If...then语句 If [ test_command ] Then Commands if 2)If...then...else语句 If [ test_command ] Then Commands Else

commands if 3)If...then...elif...then...(else)语句If [ test_command ] Then Commands Elif [ test_command ] Then Commands Else Commands Fi 4)for ... In语句 For loop_varible in argument_list Do Commands done 5)while语句 While test_command_is_true Do Commands Done

6)until 语句 Until test_command_is_true Do Commands Done 7)case语句 Case $variable in Match_1) Commands_for_1Match_2) Commands_for_2. . . *) #option for other values Commands_for_no_matchesac 6.break、continue、exit和return语句 Break跳出整个循环体,然后执行循环体外接下来的代码;Continue 结束本次循环,继续下次循环; Exit 退出整个脚本,一般在其后加入一个整数(如exit 0),作为返回代码发送给系统; Return 用于在函数中返回数据,或返回一个结果给调用函数 7.here文档

相关主题
文本预览
来源电脑时间网址执行时间QueryStringUserID