admin管理员组

文章数量:1532721

2024年1月8日发(作者:)

linux实验三Shell编程

实验三:Shell编程

一.实验目的

巩固所学的Linux下shell编程语言,熟悉Shell脚本。

二.实验内容

1.建立一个test目录

2.进入此test目录

3.新建文件test1,运行下面shell脚本

#!/bin/sh

a = "hello world"

echo $a

回答此脚本运行结果

4.下面两种脚本,哪一个能够输出语句this is the 2nd

①num=2

echo "this is the $numnd"

②num=2

echo "this is the ${num}nd"

5.若有如下shell脚本

注意:空格很重要。要确保方括号里的空格

#!/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

①请回答此脚本中提到的环境变量

②请回答此脚本的功能

6.若有如下shell脚本

#!/bin/sh

n=0

while read line;do

let "a+=$line"

let "n+=1"

done <

echo $n

echo $a

①请回答此脚本中是否包含循环,若有实现什么操作?

②请回答此脚本的功能

7.①新建文件test2,运行下面shell脚本

#!/bin/bash

echo "Your choice?"

select var in "a" "b" "c"; do

break

done

echo $var

请回答此脚本运行结果

②给出你对下面脚本的理解

#!/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"

8.阅读下面脚本

#!/bin/sh

help()

{

cat <

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"

注意:shift作用是移除当前参数,若shift 2则移除当前和下一个参数

①此shell中case语句试图实现什么功能?

②若将shell保存为cmdparser,则执行cmdparser -l hello -f -- -somefile1 somefile2会有什么结果?

9.阅读下面shell脚本

#!/bin/bash

if [ $# -lt 3 ]; then

cat<

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

注意:sed基本上可以看成一个查找替换程序,从标准输入读入文本,并将结果输出到标准输出,sed使用正则表达式进行搜索。在newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`这一行中,backtick(`)的作用是取出两个backtick之间的命令输出结果,在这里,也就是将结果取出赋给变量newfile。

①OLD="$1"中$1的含义

②第二行if [ $# -lt 3 ]; then ,写出你对此行的理解

③此脚本定义了一个命令,请回答是什么命令?若该命令的参数小于3个,会输出什么结果?

④if [ -f "$newfile" ]; then这一行中,判断条件如何解释

⑤此脚本中有无循环语句,若有请回答实现什么功能

10.用Shell语言编程编写一Shell脚本

创建一菜单,菜单界面格式如下:

User:Host:Date

------------------------------------------------------------------------------

files in current directory

/doc/,e the VI editor

who is on system

H:Help information

Q:Exit menu

-------------------------------------------------------------------------------

Your choice [ 1 、2、3、H、Q ]

编写一Shell脚本,实现上述菜单的功能。

三.实验环境

Vpc虚拟机---redhat9

四.实验要求

在redhat9系统环境下,认真按实验内容做实验,巩固所学知识,做完实验后记录Shell脚本文档及运行结果

while read LINE

do

echo $LINE

done

/sites//doc/,.txt

<

本文标签: 脚本实验结果回答运行