Linux的shell2实验报告Word下载.docx

上传人:b****5 文档编号:21351888 上传时间:2023-01-29 格式:DOCX 页数:16 大小:163.36KB
下载 相关 举报
Linux的shell2实验报告Word下载.docx_第1页
第1页 / 共16页
Linux的shell2实验报告Word下载.docx_第2页
第2页 / 共16页
Linux的shell2实验报告Word下载.docx_第3页
第3页 / 共16页
Linux的shell2实验报告Word下载.docx_第4页
第4页 / 共16页
Linux的shell2实验报告Word下载.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

Linux的shell2实验报告Word下载.docx

《Linux的shell2实验报告Word下载.docx》由会员分享,可在线阅读,更多相关《Linux的shell2实验报告Word下载.docx(16页珍藏版)》请在冰豆网上搜索。

Linux的shell2实验报告Word下载.docx

~/bin

exportPATH

echo-e"

HelloWorld!

\a\n"

exit1

执行方法:

shsh01.sh

或者

chmoda+xsh01.sh;

./sh01.sh

解释

第一行#!

/bin/bash必须有

最后一行exit0返回给系统一个值可以用echo$?

查看该返回值。

好的编写脚本的习惯:

script的功能;

script的版本资讯;

script的作者与联络方式;

script的版权宣告方式;

script的History(历史纪录);

script内较特殊的指令,使用绝对路径的方式来下达;

script运作时需要的环境变数预先宣告与设定。

简单脚本的练习:

sh02.sh变量内容由使用者决定

[root@localhostscripts]#vish02.sh

read-p"

pleaseinputyourfirstname:

"

firstname

pleaseinputyourlastname:

lastname

\nyourfullnameis:

$firstname$lastname"

[root@localhostscripts]#shsh02.sh

liu

shu

yourfullnameis:

liushu

sh03.sh利用date进行档案的建立

b=${a:

-"

c"

}用法解释:

[root@localhostscripts]#a='

'

[root@localhostscripts]#b=${a:

}

[root@localhostscripts]#echo$b

c

[root@localhostscripts]#a="

d"

[root@localhostscripts]#vish03.sh

Iwilluse'

touch'

commandtocreate3files."

pleaseinputthefilenamewhatyouwant:

fileuser

filename=${fileuser:

filename"

echo$filename

date1=`date--date='

2daysago'

+%Y%m%d`

date2=`date--date='

1daysago'

date3=`date+%Y%m%d`

file1="

$filename"

$date1"

file2="

$date2"

file3="

$date3"

touch$file1

touch$file2

touch$file3

运行时可以输入名称和不输入名称分别运行来理解程序。

sh04.shbashshell里头预设仅支援到整数的资料

[root@localhostscripts]#vish04.sh

youshouldinput2number,iwillcrossthey!

\n"

firstname:

firstnu

secondnumber:

secnu

total=$(($firstnu*$secnu))

echo$total

\nthenumber$firstnu*$secnuis==>

$total"

第二个

判断:

/************************************************************************/

利用test指令的测试功能

[root@localhostscripts]#vish05.sh

Theprogramwillshwoyouthatfilenameisexistwhichinputbyyou.\n\n"

inputafilename:

"

filename

test-z$filename&

&

echo"

thefilename$filenamedonotexist"

&

exit0

test-f$filename&

filetype="

regularefile"

test-d$filename&

derectory"

test-r$filename&

perm="

read"

test-w$filename&

$permw"

test-x$filename&

$permx"

echo"

thefilename:

$filenameisa$filetype"

thethepermissionare:

$perm"

(5)利用test指令的测试功能

Test是判断逻辑。

执行:

(6)利用判断符号:

 

判断符号:

(7)$0$1的默认变量

用来判断下$1$0

(8)简单的IF语句:

[root@localhostscripts]#shsh08.sh

youmustinputparameters,ex>

sh08.shsimeword

注意:

语句中If跟fi相当于其他语言中括号的作用:

(9)探测开启哪些端口和服务

[root@localhostscripts]#netstat-tuln

(10)打印信息并开始探测:

运用管道跟if语句,可以简单做到:

(11)case语句:

选择支语句:

(12)function功能:

(13)for语句循环:

#第一个简单for示例

#foranimalindogcatelephant

#do

#echo"

thereare"

$animal"

s..."

#done

(14)

#开始测试文件权限

filelist=`ls$dir`

forfilenamein$filelist

do

test-r"

$dir/$filename"

$permreadable"

test-w"

$permwritable"

test-x"

$permexecutable"

the$dir/$filename'

spermissionis:

$perm"

done

利用判断符号[]

[root@localhostscripts]#vish06.sh

pleaseinput(Y):

yn

["

$yn"

=="

Y"

-o"

y"

]&

okcontinue"

Idon'

tknowwhatisyourinput"

Shellscript的默认变量($0,$1...)

/path/to/scriptnameopt1opt2opt3opt4...

$0$1$2$3$4...

[root@localhostscripts]#vish07.sh

Thescriptnameis:

$0"

[-n"

$1"

the1stparamteris:

$1"

||exit0

条件判断式

利用if....then

[root@localhostscripts]#vish06-2.sh

pleaseinput(y):

if["

]||["

];

then

ok,continue"

fi

idonotknowwhatisyourchoise"

那么如果我要侦测你所输入的参数是否为hello呢,也就是说,如果我想要知道,你在程式后面所接的第一个参数(就是$1)是否为hello,

(1)如果是的话,就显示"

Hello,howareyou?

(2)如果没有加任何参数,就提示使用者必须要使用的参数下达法;

(3)而如果加入的参数不是hello,就提醒使用者仅能使用hello为参数。

[root@localhostscripts]#vish08.sh

hello"

hello,howareyou?

elif["

$0simeword"

else

theonlyparameteris'

hello'

[root@localhostscripts]#netstat-tuln

探测开启哪些端口和服务?

[root@localhostscripts]#vish09.sh

#1.先打印一些提示信息

now,theservicesofyoursystemlinuxsystemwillbedetected!

thewww,ftp,sshwillbeedtect!

#2开始探测工作

testing=`netstat-tuln|grep"

22"

`

$testing"

!

="

sshsirunninginyoursystem"

25"

mailisrunninginyoursystem."

利用case.....esac判断

[root@localhostscripts]#vish08-2.sh

case$1in

hello,howareyou?

;

;

youmustinputparameter,ex>

$0someword"

*)

usage$0{hello}"

esac

利用function功能

[root@localhostscripts]#vish11.sh

functionprintit(){

yourchoiceis$1"

thisprogramwilprintyourselection!

#$1是主程序的参数

one"

printit1

two"

printit2

three"

printit3

*)

usage{one|two|three}"

(loop)循环

whiledodone,untildodone

while[condition]

程式段落

until[condition]

[root@localhostscripts]#vish12.sh

while["

yes"

["

YES"

]

read-p"

pleaseinputyes/YEStostopthisprogram:

#第二种循环

#until["

#read-p"

计算1+2+3+....+100两种方法:

whiledo....donefor...do....done

[root@localhostscripts]#vish13.sh

s=0

i=0

$i"

100"

i=$(($i+1))

s=$(($s+$i))

theresultof'

1+2+3+...+100'

is==>

$s"

#for循环来实现

#s=0

#for((i=1;

i<

=100;

i=i+1))

#s=$(($s+$i))

#echo"

1+2+...+100'

is$s"

for循环用法

让用户输入某个目录,然后我找出某目录内的文件的权限

[root@localhostscripts]#vish16.sh

#第二个示例

#1先测试目录是否存在

pleaseinputadirectory:

dir

$dir"

]||[!

-d"

the$dirisnotexistinyoursystem."

exit1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1