您的位置:首页 > 运维架构 > Linux

linux运维实战练习-2015年9月13日-9月15日课程作业

2015-08-20 22:57 731 查看
1、描述shell程序的运行原理; shell既是命令语言、程序设计语言也是命令解释程序。 shell脚本通常是以#!起始的文本文件,如“#!/bin/bash”,这行被称为shebang,指定了解释此脚本shell的路径,执行脚本时系统会调用这个shell来运行此脚本。字符#指明注释的开始。注释部分以#为起始,一直延续到行尾,注释行通常用于为代码提供注释信息,或者用于暂停执行某行代码,会被解释器忽略。 shell脚本是把命令堆砌在一起,shell通过词法分析,语法分析,语义分析,按顺序、选择或循环执行脚本中的命令。脚本运行结束,此shell进程也即终止2、总结shell编程中所涉及到的所有知识点(如:变量、语法、命令状态等等等,要带图的哟); 变量、变量运算及条件测试 条件判断语句if、case及read命令 循环语句for,while,until 函数、死循环、模块话编程3、总结课程所讲的所有循环语句、条件判断的使用方法及其相关示例;(if (jpg|png is not exist);echo ”You say a XX“) 循环语句for,while,until 变量、变量运算及条件测试4、写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型;(不要怀疑,就是这么简单)[root@liaodijin ~]# cat test.sh#!/bin/bash#read -t 10 -p "Please input a file path: " pathif [ -z "$path" ];then echo -e "\n\033[31mError:\033[0mplease input a file path" exit 1 elif [ ! -e "$path" ];then mkdir -p $path $>/dev/null echo "your input $path is not exist"elif [ -f "$path" ];then echo "$path is a general file" echo -e "\033[31mThe $path:\033[0m" cat $pathelif [ -d "$path" ];then echo "$path is a directory" echo -e "\033[31mThe $path:\033[0m" ls $pathelse echo "$path unknown type"fi 测试结果:[root@liaodijin ~]# bash test.shPlease input a file path: /etc/fstab/etc/fstab is a general fileThe /etc/fstab: ## /etc/fstab# Created by anaconda on Thu Sun 19 22:48:50 2015## Accessible filesystems, by reference, are maintained under '/dev/disk'# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1UUID=46c42e9f-f101-4209-9590-afa295415eaf /boot ext4 defaults 1 2/dev/mapper/VolGroup-lv_data /data ext4 defaults 1 2/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0tmpfs /dev/shm tmpfs defaults 0 0devpts /dev/pts devpts gid=5,mode=620 0 0sysfs /sys sysfs defaults 0 0proc /proc proc defaults 0 0[root@liaodijin ~]# bash test.shPlease input a file path: /tmp/tmp is a directoryThe /tmp:ks-script-utpPS_ yum.logks-script-utpPS_.log yum_save_tx-2015-09-15-20-24RcJDyn.yumtxshell yum_save_tx-2015-09-16-20-29ihcHuJ.yumtxtest.sh yum_save_tx-2015-09-16-20-30ZOEQ7w.yumtxtest.sh yum_save_tx-2015-09-16-20-32anadvE.yumtx[root@liaodijin ~]# bash test.shPlease input a file path: xxyour input xx is not exist[root@liaodijin ~]# ls~ cc shell test3.sh xx$ init.sh test1.sh test3.sh.origaa install.log test1.sh.orig test.shanaconda-ks.cfg install.log.syslog test.sh test.sh.origbb qq test.sh.orig trash.sh[root@liaodijin ~]# bash test.shPlease input a file path: /dev/null/dev/null unknown type[root@liaodijin ~]# bash test.shPlease input a file path: Error:please input a file path[root@liaodijin ~]# bash test.shPlease input a file path: aa bb ccyour input aa bb cc is not exist5、写一个脚本完成如下功能:判断给定的两个数值,哪个大哪个小,给定数值的方法:脚本参数,命令交互(使用read,依然如此简单)[root@test shell]# cat 6.sh#!/bin/bash #error(){cat<<EOF+--------------------------------------------------------+| Function of the script is to compare two numbers size | | Instructions:num_compare.sh num1 num2 || Example:num_compare.sh 11 22 | +--------------------------------------------------------+EOF}echo "$*"|grep '[[:alpha:]]' &>/dev/null && error && exit 1if [ $# -gt 2 -o $# -le 1 ];thenerrorexit 2elif [ "$1" -gt "$2" ];then echo -e "The max is $1\nThe min is $2"elif [ "$1" -lt "$2" ];then echo -e "The max is $2\nThe min is $1"elif [ "$1" -eq "$2" ];then echo "$1 and $2 as large as"else errorfi 测试结果如下:[root@test shell]# bash 6.sh+--------------------------------------------------------+| Function of the script is to compare two numbers size | | Instructions:num_compare.sh num1 num2 || Example:num_compare.sh 11 22 | +--------------------------------------------------------+[root@test shell]# bash 6.sh a 1+--------------------------------------------------------+| Function of the script is to compare two numbers size | | Instructions:num_compare.sh num1 num2 || Example:num_compare.sh 11 22 | +--------------------------------------------------------+[root@test shell]# bash 6.sh 1 a+--------------------------------------------------------+| Function of the script is to compare two numbers size | | Instructions:num_compare.sh num1 num2 || Example:num_compare.sh 11 22 | +--------------------------------------------------------+[root@test shell]# bash 6.sh xy zx+--------------------------------------------------------+| Function of the script is to compare two numbers size | | Instructions:num_compare.sh num1 num2 || Example:num_compare.sh 11 22 | +--------------------------------------------------------+[root@test shell]# bash 6.sh 12a 2b3+--------------------------------------------------------+| Function of the script is to compare two numbers size | | Instructions:num_compare.sh num1 num2 || Example:num_compare.sh 11 22 | +--------------------------------------------------------+[root@test shell]# bash 6.sh 10 34The max is 34The min is 106、求100以内所有奇数之和(至少用3种方法。是的这是我们的作业^_^)(1)、[root@liaodijin ~]# cat test4.sh#!/bin/bash#y=0for x in $(seq 1 2 100);do # for循环 y=$[$y+$x]done echo "The sum is $y"[root@liaodijin ~]# bash test4.shThe sum is 2500(2)、[root@liaodijin ~]# cat test5.sh#!/bin/bash#x=1while [ "$x" -le 100 ];do # while循环 if [ $[$x%2] -eq 1 ];then let y+=$x fi let x++done echo "The sum is $y"[root@liaodijin ~]# bash test5.shThe sum is 2500(3)、[root@liaodijin ~]# cat test10.sh#!/bin/bash#x=1until [ $x -gt 100 ];do # until循环 if [ $[$x%2] -eq 1 ];then let y+=$x fi let x++done echo "The sum is $y"[root@liaodijin ~]# bash test10.shThe sum is 25007、写一个脚本实现如下功能: (1) 传递两个文本文件路径给脚本; (2) 显示两个文件中空白行数较多的文件及其空白行的个数; (3) 显示两个文件中总行数较多的文件及其总行数;[root@liaodijin ~]# cat test6.sh#!/bin/bash#read -t 10 -p "Please enter two file path: " file1 file2 #发现一个问题,这里我不输入等10S后为什么不会结束,按ENTER键也不会结束spaceline1=$(grep "^$" $file1|wc -l)spaceline2=$(grep "^$" $file2|wc -l)line1=$(cat $file1|wc -l)line2=$(cat $file2|wc -l)###############################################################compare(){ # 这里我们发现第2问和第3问的要求通过一个表达式只是参数不同就可以实现,所以我们这里先定义一个函数,调用2次就可以实现,省得重复写命令 if [ $a -gt $b ];then echo -e " max $file1\n $file1 :$a"elif [ $a -lt $b ];then echo -e "max $file2\n $file2 :$b"elif [ $a -eq $b ];then echo -e "$file1 and $file2 as large as\n space line is $a"else echo "Error please enter two file path!"fi} ##############################################################a=$spaceline1b=$spaceline2echo "The space_line:"compareechoa=$line1b=$line2echo "The line:"compare 测试结果:[root@liaodijin ~]# bash test6.shPlease enter two file path: /root/aa /root/bbThe space_line: max /root/aa /root/aa :2 The line:max /root/bb /root/bb :11[root@liaodijin ~]# cat aaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa[root@liaodijin ~]# cat bbbbbbbbbbbbbbbbbbbbbbbabccccccccd dc8、写一个脚本(1) 提示用户输入一个字符串;(2) 判断:如果输入的是quit,则退出脚本;否则,(继续等待用户输入并)显示其输入的字符串内容;
[root@liaodijin ~]# cat test8.sh#!/bin/bash#read -t 10 -p "Please enter string: " varcase $var inquit) exit 1 ;;*) echo "$var" ;;esac 测试结果:[root@liaodijin ~]# bash test8.shPlease enter string: [root@liaodijin ~]# bash test8.shPlease enter string: nihaodafnihaodaf[root@liaodijin ~]# bash test8.shPlease enter string: quit2、[root@test shell]# cat 9.sh#!/bin/bash#while true;doread -p "Please enter string: " str[ $str == quit ]&&breakecho $strdone 测试结果:[root@test shell]# bash 9.shPlease enter string: nihaonihaoPlease enter string: sbsbPlease enter string: quit10、写一个脚本,打印2^n表;n等于一个用户输入的值;(不好意思,我调皮了)

11、写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数3、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。

本文出自 “但行好事,莫问前程。” 博客,请务必保留此出处http://sprint.blog.51cto.com/4824230/1686682
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: