【答案】实验三 Vi及Shell程序设计
实验三 Vi及Shell程序设计【实验目的】1、 掌握vi的操作方法。2、 掌握Shell脚本的编程方法。【实验内容】第一部分 vi1、 请在/tmp目录下建立一个名为vitest的目录;(请书写命令)rootwwq-VirtualBox:# mkdir /tmp/vitest2、 进入vitest目录;rootwwq-VirtualBox:#cd /tmp/vitest3、 将/etc/man.config复制到本目录下;rootwwq-VirtualBox:#cp /etc/man.config .注意:Ubuntu中没有man.config文件4、 使用vi开启本目录下的man.config文件;rootwwq-VirtualBox:#vi man.config5、 在vi中设定行号;:set number6、 移动到第58行,向右移动40个字符,请问看到什么目录?/dir/bin/foo7、 移到第1行,并向下搜寻bzip2字符串,请问它在第几行?第118行8、 将50到100行之间的man改为MAN,如何实现?:50,100s/man/MAN/gc9、 修改完后,想全部复原,怎么样实现?:q!或者按u撤销10、 复制65到73行这9行的内容,并粘贴到最后一行之后;按“65G”使光标移到65行,再按“9yy”,再按“G”到最后一行,再按“P”就可以完成操作。11、 删除21到42行之间的开头为#符号的批注数据,如何实现?按“21G”之后,再按“22dd”即可删除22行。12、 将这个文件另存为man.test.config文件;:w man.test.config13、 转到第27行,并删除15个字符,结果出现的第一个单词是什么?删除:按“27G”,再按“15x”即可删除15个字符出现“you”单词14、 在第一行新增一行,输入I am a student,怎样实现?按“1G”到第一行,再按“O”新增一行并进入插入模式,输入文字后按Esc回到一般模式。15、 保存后退出。:wq第二部分 Shell程序设计1、 请编写一个Shell脚本,当执行该脚本的时候,该脚本可以显示:1)你目前的身份(用USER)2)你目前所在的目录(用PWD)#!/bin/bashecho "name is $(whoami)"echo "current directory is $(pwd)"name is rootcurrent directory is /root2、 请编写一个Shell脚本,该程序可以计算“你还有多少天可以过生日”。#!/bin/bashread -p "input birthday(MMDD):" birnow=date +%m%dif "$bir" = "$now" ;thenecho "Happy Birthday!"elif "$bir" -gt "$now" ;thenyear=date +%Ytotal_d=$($(date -date="$year$bir" +%s-date +%s)/60/60/24)echo "Your birthday will be $total_d later."elseyear=$(date +%Y+1)total_d=$($(date -date="$year$bir" +%s-date +%s)/60/60/24)echo "Your birthday will be $total_d later."fiinput birthday(MMDD):0916Your birthday will be 127 later.3、 让用户输入一个数字,程序可以由1+2+3.一直累加到用户输入的数字为止。#!/bin/bashread -p "input an integer number:" numberi=0s=0while "$i" != "$number" doi=$($i+1)s=$($s+$i)doneecho "the sum of 1-$number is $s"input an integer number:3the sum of 1-3 is 64、 请编写一个Shell脚本,它的作用是:1)先查看一下/root/test/logical这个名称是否存在;2)若不存在,则建立一个文件,使用touch来建立,建立完成后离开;3)如果存在的话,判断该名称是否为文件,若为文件则将它删除后建立一个目录,目录名为logical,之后离开;4)如果存在的话,判断该名称是否为目录,若为目录则删除此目录。#!/bin/bashif ! -e logical ; thentouch logicalecho "make a file logical"exit 1elif -e logical && -f logical ; thenrm logicalmkdir logicalecho "remove file logical"echo "make directory logical"exit 1elif -e logical && -d logical ; thenrm -rf logicalecho "remove directory logical"exit 1elseecho "do nothing."fimake a file logicalremove file logicalmake directory logicalremove directory logicalmake a file logical5、 我们知道/etc/passwd里面以:来分隔,第一栏为账号名称。请编写一个Shell脚本,可以将/etc/passwd的第一栏取出,而且每一栏都以一行字符串The 1 account is “root”来显示,那个1表示行数。#!/bin/bashaccounts=cat /etc/passwd | cut -d: -f1for account in $accountsdodeclare -i i=$i+1echo "$i account is "$account""done1 account is "root"2 account is "daemon"3 account is "bin".6、 请编写一个Shell脚本,利用for循环把当前目录下的所有*.c文件复制到指定的目录中,并显示复制后该目录内按文件大小排序的目录文件清单。#!/bin/bashcd $1for x in $1/*.cdocp $x $2donels -S $2rootwwq-VirtualBox:# sh copy.sh . testa.c7、 请编写一个Shell脚本,它把第二个位置参数及其以后的各个位置参数指定的文件复制到第一个位置参数执行的目录中。#!/bin/basha=$1shiftcp $* $arootwwq-VirtualBox:# sh shift.sh test copy.sh remove.sh birth.sha.c copy.sh remove.sh birth.sh8、 请编写一个Shell脚本,根据键盘可以循环输入学生成绩(百分制),并显示对应的成绩标准(及格和不及格),按Q键退出,按其他键提示重新输入。#!/bin/bashwhileread -p "input number:" vardocase $var inQ) exit;q) exit;esacecho "number is $var"if "$var" -ge 60 then echo "score is passed."else echo "score failed to pass."fidoneinput number:60number is 60score is passed.input number:50number is 50score failed to pass.input number:100number is 100score is passed.input number:q请完成上述任务,并提交实验报告。