shellexamples.docx

上传人:b****5 文档编号:6907367 上传时间:2023-01-12 格式:DOCX 页数:11 大小:17.46KB
下载 相关 举报
shellexamples.docx_第1页
第1页 / 共11页
shellexamples.docx_第2页
第2页 / 共11页
shellexamples.docx_第3页
第3页 / 共11页
shellexamples.docx_第4页
第4页 / 共11页
shellexamples.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

shellexamples.docx

《shellexamples.docx》由会员分享,可在线阅读,更多相关《shellexamples.docx(11页珍藏版)》请在冰豆网上搜索。

shellexamples.docx

shellexamples

93-114

1.greeting.sh

从键盘读入姓名字符串,并显示“hello****”,其中***表示输入的姓名。

#!

/bin/bash

#aSimpleshellScriptExample

#aFunction

functionsay_hello()

{

echo"EnterYourName,Please.:

"

readname

echo"Hello$name"

}

echo"ProgrammeStartsHere....."

say_hello

echo"ProgrammeEnds."

2.exam_if.sh

从键盘输入一个文件或目录的名字字符串,并根据文件的类型显示其是类型是目录还是文件,如果是普通文件,显示其是否可读,可写,可执行。

#!

/bin/bash

#anexamplescriptofif

clear

echo"Inputafileordirectoryname,please"

readpath_name

if[-d$path_name]

then

echo"$path_nameisadirectory"

elif[-f$path_name]

then

echo"$path_nameisaregularfile"

if[-r$path_name]

then

echo"$path_nameisareadablefile"

fi

if[-w$path_name]

then

echo"$path_nameisawriteablefile"

fi

if[-x$path_name]

then

echo"$path_nameisaexecutablefile"

fi

else

echo"Thisscriptcannotgetthefile/directory($path_name)information!

"

fi

3.exam_if-1.sh

输入目录名,通过cd命令切换到目录,显示切换结果。

#!

/bin/bash

#anotherexamplescriptofif

echo"Inputadirectory,please!

"

readdir_name

ifcd$dir_name>/dev/null2>&1;then

echo"enterdirectory:

$dir_namesucceed"

else

echo"enterdirectory:

$dir_namefailded"

fi

4.exam_case.sh

利用case结构实现读入字符串并判断,此字符串的值是“y”还是“n”。

#!

/bin/bash

#anexamplescriptofcase

clear

echo"AreyoulikeLinux?

"

readlike_it

case$like_itin

y|Y|yes|Yes)echo"Linuxisagoodfriendofus."

;;

n|N|no|No)echo"Tryit,andyouwilllikeit."

;;

*)echo"youansweris:

$like_it"

;;

esac

5.exam_for.sh

for结构应用

#!

/bin/bash

#anexamplescriptoffor

clear

forosinLinuxWindowsUnix

do

echo"OperationSystemis:

$os"

done

6.exam_until.sh

题目要求:

用until结构实现在屏幕上输出以下结果:

currnetvalueofloopis:

1

currnetvalueofloopis:

2

currnetvalueofloopis:

3

……..

currnetvalueofloopis:

9

答案:

#!

/bin/bash

#anexamplescriptofwhile

clear

loop=0

until[$loop-eq10]

do

letloop=$loop+1

echo"currnetvalueofloopis:

$loop"

done

7.exam_while.sh

用while结构实现在屏幕上输出以下结果:

currnetvalueofloopis:

1

currnetvalueofloopis:

2

currnetvalueofloopis:

3

……..

currnetvalueofloopis:

9

#!

/bin/bash

#anexamplescriptofwhile

clear

loop=0

while[$loop-ne10]

do

letloop=$loop+1

echo"currnetvalueofloopis:

$loop"

done

8.exam_param.sh

理解位置参数

#!

/bin/bash

#anexamplescriptoffunction

functiondemo_fun()#functiondefinitionstarts

{

echo"Yourcommandis:

$0$*"

echo"NumberofParameters(\$#)is:

$#"

echo"Scriptfilename(\$0)is:

$0"

echo"Parameters(\$*)is:

$*"

echo"Parameters(\$@)is:

$@"

count=1

forparamin$@

do

echo"Parameters(\$$count)is:

$param"

letcount=$count+1

done

}#functiondefinitionends

clear

demo_fun$@

9.exam_shift.sh

理解位置参数

#!

/bin/bash

#anexamplescriptoffunction

functiondemo_fun()#functiondefinitionstarts

{

echo"Yourcommandis:

$0$*"

echo"NumberofParameters(\$#)is:

$#"

echo"Scriptfilename(\$0)is:

$0"

echo"Parameters(\$*)is:

$*"

echo"Parameters(\$@)is:

$@"

count=1

while[-n"$1"]

do

echo"Parameters(\$$count)is:

$1"

letcount=$count+1

shift

done

}#functiondefinitionends

clear

demo_fun$@

10.exam_return.sh

理解return

#!

/bin/bash

#anexamplescriptofreturn

functionfun_return()

{

dir_name=$1

rtn_value=0

if!

cd$dir_name>/dev/null2>&1;then

rtn_value=1

fi

return$rtn_value

}

clear

iffun_return$@;then

echo"functionexecutessuccessfuly!

"

else

echo"functionexecutesfailed!

"

fi

11.exam_return_2.sh

#!

/bin/bash

#anexamplescriptofreturn

functionfun_return()

{

dir_name=$1

rtn_value=0

cd$dir_name>/dev/null2>&1

return

}

clear

iffun_return$@;then

echo"functionexecutessuccessfuly!

"

else

echo"functionexecutesfailed!

"

fi

12..bashrc

修改用户家目录下的.bashrc文件,实现用户登录后看到的命令提示符是:

HealthisbetterthanWealth!

#.bashrc

#Userspecificaliasesandfunctions

#Sourceglobaldefinitions

if[-f/etc/bashrc];then

./etc/bashrc

fi

echo"HealthisbetterthanWealth!

"

exportPS1="[\t]\u@\h/\w>"

13.mcp.sh

#!

/bin/bash

#anexamplescriptofbackupfiles

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

BACKUP_DIR=~/backup

BACKUP_LOG="$BACKUP_DIR/${LOG_START_TIME}.log"

functionwrite_log()

{

log_time=`date+"%Y-%m-%d-%H-%M-%S"`

backup_file_name=$2

err_msg="$log_timeERRORinbackupfile/directory($backup_file_name)"

suc_msg="$log_timeSUCCESSinbackupfile/directory($backup_file_name)"

if[$1-eq0];then

echo$suc_msg

echo$suc_msg>>$BACKUP_LOG

else

echo$err_msg

echo$err_msg>>$BACKUP_LOG

fi

}

functionbackup_file()

{

cp-fr$1$BACKUP_DIR>/dev/null2>&1

write_log$?

$1

}

functioncreate_log_file()

{

if[!

-e$BACKUP_DIR];then

mkdir$BACKUP_DIR

fi

if[-e$BACKUP_LOG];then

rm-f$BACKUP_LOG

fi

touch$BACKUP_LOG

}

clear

echo"BackupProcessBegins"

create_log_file

forfilein$@

do

backup_file$file

done

echo"BackupProcessEnds"

14.exam_for_1.sh

sum=0

for((i=1;i<101;i=i+1))//注意表达式外面的双重括号

do

letsum=sum+$i

done

echo$sum

15.if判断

用Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到/dev目录下。

参考程序:

#!

/bin/sh

FILENAME=

echo“Inputfilename:

readFILENAME

if[-c"$FILENAME"]

then

cp$FILENAME/dev

fi

16.循环1

2.设计一个shell程序,添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。

参考答案:

#!

/bin/sh

i=1

groupaddclass1

while[$i-le30]

do

if[$i-le9];then

USERNAME=stu0${i}

else

USERNAME=stu${i}

fi

useradd$USERNAME

mkdir/home/$USERNAME

chown-R$USERNAME/home/$USERNAME

chgrp-Rclass1/home/$USERNAME

i=$(($i+1))

done

17.循环2

编写shell程序,实现自动删除50个账号的功能。

账号名为stud1至stud50。

参考程序:

#!

/bin/sh

i=1

while[$i-le50]

do

userdel-rstud${i}

i=$(($i+1))

done。

18.循环3

4.设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限,其中其他用户的权限为:

读;文件所有者的权限为:

读、写、执行;文件所有者所在组的权限为:

读、执行。

参考答案:

建立程序Pro16如下:

#!

/bin/sh

i=1

while[i-le50]

do

if[-d/userdata];then

mkdir-p/userdata/user$i

chmod754/userdata/user$i

echo"user$i"

let"i=i+1"(或i=$(($i+1))

else

mkdir/userdata

mkdir-p/userdata/user$i

chmod754/userdata/user$i

echo"user$i"

let"i=i+1"(或i=$(($i+1))

fi

done

19.文件读取

1--SHELL读取文件的方法

#使用read命令读取一行数据

whilereadmyline

do

echo"LINE:

"$myline

done

#使用read命令读取一行数据

catdatafile.txt|whilereadmyline

do

echo"LINE:

"$myline

done

#读取一行数据

catdatafile.txt|whilemyline=$(line)

do

echo"LINE:

"$myline

done

#读取一行数据

whilemyline=$(line)

do

echo"LINE:

"$myline

done

#使用read命令读取变量数据

catdatafile.txt|whilereadparaaparabparac

do

echo"PARAA:

"$paraa

echo"PARAB:

"$parab

echo"PARAC:

"$parac

done

#使用read命令读取变量数据

whilereadparaaparabparac

do

echo"PARAA:

"$paraa

echo"PARAB:

"$parab

echo"PARAC:

"$parac

done

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

当前位置:首页 > 人文社科

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

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