1、描述shell程序的运行原理(可附带必要的图形说明);
2、总结shell编程中所涉及到的所有知识点(如:变量、语法、命令状态等等等,要带图的哟);
3、总结课程所讲的所有循环语句、条件判断的使用方法及其相关示例;
4、写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型;(不要怀疑,就是这么简单)
#!/bin/bash#if [ $# -ne 1 ]thenecho "Usage:$0 directory"exit 1fiif [ -d $1 ]then echo "$1 is exist" file $1else mkdir -p $1 &>/dev/null if [ $? -ne 0 ] then echo "mkdir error" exit 2 fifi#执行结果[root@os01 /]# ./mkdir.sh test1[root@os01 /]# ll -d test1/drwxr-xr-x 2 root root 6 Sep 17 13:47 test1/[root@os01 /]# ./mkdir.sh test1test1 is existtest1: directory
5、写一个脚本,完成如下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;(使用read,依然如此简单)
#!/bin/bash#read -p "plz input to num:" -t 10 num1 num2if [ -z "$num1" -o -z "$num2" ];then echo "Plz give two integers." exit 1fiif [ $num1 -gt $num2 ]then echo "Max:$num1 , Min:$num2"elif [ $num1 -lt $num2 ]then echo "Max:$num2 , Min:$num1"else echo "$num1 equal $num2"fi#执行结果[root@os01 /]# ./read.sh plz input to num:Plz give two integers.[root@os01 /]# ./read.sh plz input to num:9 5Max:9 , Min:5[root@os01 /]# ./read.sh plz input to num:9 99 equal 9
6、求100以内所有奇数之和(至少用3种方法。是的这是我们的作业^_^)
第一种:for
#!/bin/bashdeclare -i num=0for i in `seq 1 2 100`do num+=i# num=$[$num+$i]doneecho "sum is $num"执行结果:[root@os01 /]# ./1.sh sum is 2500
第二种:while
#!/bin/bash#declare -i i=1declare -i sum=0while [ $i -le 100 ]do sum+=i let i+=2doneecho "sum is $sum"#执行结果[root@os01 /]# ./2.sh sum is 2500
第三种:for contniue
#!/bin/bashdeclare -i i=1declare -i sum=0for i in `seq 1 100`do if [ $[$i%2] -eq 0 ] then continue fi sum+=idoneecho "sum is $sum"#执行结果[root@os01 /]# ./3.sh sum is 2500
第四种:until
#!/bin/bash#declare -i i=0declare -i sum=0until [ $i -eq 100 ]do let i++ if [ $[$i%2] -eq 0 ] then continue fisum+=idoneecho "sum is $sum"[root@os01 /]# ./4.sh sum is 2500
7、写一个脚本实现如下功能:
(1) 传递两个文本文件路径给脚本;
(2) 显示两个文件中空白行数较多的文件及其空白行的个数;
(3) 显示两个文件中总行数较多的文件及其总行数;
#!/bin/bash#read -p "plz input two file or dir path:" -t 50 f1 f2if [ -z $f1 -o -z $f2 ]then echo "Usg:$0 /path/file1 /path/file2" exit 1fiif [ ! -e $f1 -o ! -e $f2 ]then echo "$f1 or $f2 not exist" exit 2fif1_s=`grep '^$' $f1|wc -l`f2_s=`grep '^$' $f2|wc -l`if [ $f1_s -gt $f2_s ]then echo "space_max: $f1 : $f1_s;"elif [ $f1_s -lt $f2_s ]then echo "space_max: $f2 : $f2_s;"else echo "$f1,$f2 space lines is eq;space_line= $f1_s"fif1_lines=`wc -l $f1|cut -d" " -f1`f2_lines=`wc -l $f2|cut -d" " -f1`if [ $f1_lines -gt $f2_lines ]then echo "max_lines: $f1 :$f1_lines ;"elif [ $f1_lines -lt $f2_lines ]then echo "max_lines: $f2 : $f2_lines ;"else echo "$f1,$f2 lines is eq" echo "$f1 lines: $f1_lines , space_lines: $f1_s" echo "$f2 lines: $f2_lines , space_lines: $f2_s"fi#执行结果[root@os01 dir]# ./6.sh plz input two file or dir path:Usg:./6.sh /path/file1 /path/file2[root@os01 dir]# ./6.sh plz input two file or dir path:a.txt b.txta.txt,b.txt space lines is eq;space_line= 3max_lines: b.txt : 9 ;
8、写一个脚本
(1) 提示用户输入一个字符串;
(2) 判断:
如果输入的是quit,则退出脚本;
否则,则显示其输入的字符串内容;
#!/bin/bashread -p "plz input one char:" -t 20 charif [ -z $char ]then echo "Usg: $0 Character" exit 1ficase $char inquit) exit 0 ;;*) echo "$char"esac#执行结果[root@os01 dir]# ./4.sh plz input one char:Usg: ./4.sh Character #超时未输入退出[root@os01 dir]# ./4.sh plz input one char:quit #退出脚本
9、写一个脚本,打印2^n表;n等于一个用户输入的值;(不好意思,我调皮了)
两个结果不知道要求哪个。
#!/bin/bashread -p "plz input a num:" -t 10 numif [ -z $num ]then exit 1fiecho "2^0=1"echo "2^1=2"for i in `seq 2 $num`doa+=x2echo "2$a=$[2**$i]"done#执行结果[root@os01 dir]# ./mi.sh plz input a num:102^0=12^1=22x2=42x2x2=82x2x2x2=162x2x2x2x2=322x2x2x2x2x2=642x2x2x2x2x2x2=1282x2x2x2x2x2x2x2=2562x2x2x2x2x2x2x2x2=5122x2x2x2x2x2x2x2x2x2=1024
第二个:
#!/bin/bashread -p "plz input a num:" -t 10 numif [ -z $num ]then exit 1fifor i in `seq 0 $num`do echo "2^$i=$[2**$i]" done #执行结果:[root@os01 dir]# ./test.sh plz input a num:62^0=12^1=22^2=42^3=82^4=162^5=322^6=64
10、写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数3、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。
函数脚本:/shell/hs.sh 执行脚本:/shell/cal.sh
函数脚本:
取最大公约数思路:两个数值从1到较小数字都必须同时取余数为0,依次取出公约数覆盖输出到文件,最后一个为最大公约数。
取最小公倍数思路:从1到两个数字乘积循环取余数,较大数依次与循环变量相乘再对较小数字取余,当余为0时,为最小公倍数。
两个数字乘积肯定是公约数,所以从1到两个数乘积循环,从大的数字X1,X2,X3一直到两个数字乘积,第一个能对小的数字取余数为0(跳出break)的为最小公倍数。
#!/bin/bash#错误函数======================================================error() {if [ -z $m -o -z $n ] then echo "请输入两个数值" exit 1fi}#计算两个整数之和================================================zh() {read -p "请输入两个整数,计算它们的和:" -t 10 m nerrorzonghe=$[$m+$n]echo $zonghe}#计算最大公约束====================================================zdgys() {read -p "请输入两个整数,计算它们的最大公约数:" -t 10 m nerrorif [ $m -gt $n ]then for i in `seq 1 $n` do if [ $[$n%$i] -eq 0 -a $[$m%$i] -eq 0 ] then echo $i>/tmp/max1.txt fi donecat /tmp/max1.txtelif [ $m -le $n ]then for i in `seq 1 $n` do if [ $[$n%$i] -eq 0 -a $[$m%$i] -eq 0 ] then echo $i>/tmp/max2.txt fi donecat /tmp/max2.txtfi}#计算最小公倍数=====================================================zxgbs() {read -p "请输入两个数字,计算它们的最小公倍数:" -t 10 m n errorif [ $m -gt $n ]then for j in `seq 1 $[$m*$n]` do if [ $[$m*$j%$n] -eq 0 ] then echo "$[$m*$j]" break fi doneelif [ $m -le $n ]then for j in `seq 1 $[$m*$n]` do if [ $[$n*$j%$m] -eq 0 ] then echo "$[$n*$j]" break fi donefi}
执行脚本:/shell/cal.sh
#!/bin/bashread -p "plz input 'zh'|'zdgys'|'zxgbs':" -t 20 char. /shell/hs.shcase $char inzh) zh;;zdgys) zdgys;;zxgbs) zxgbs;;*) echo "input error:Usg:'zh'|'zdgys'|'zxgbs'"esac#执行结果:[root@os01 ~]# ./cal.sh plz input 'zh'|'zdgys'|'zxgbs':zh请输入两个整数,计算它们的和:10 90100[root@os01 ~]# ./cal.sh plz input 'zh'|'zdgys'|'zxgbs':zdgys请输入两个整数,计算它们的最大公约数:10 9010[root@os01 ~]# ./cal.sh plz input 'zh'|'zdgys'|'zxgbs':zxgbs请输入两个数字,计算它们的最小公倍数:10 9090[root@os01 ~]# ./cal.sh plz input 'zh'|'zdgys'|'zxgbs':as^Hinput error:Usg:'zh'|'zdgys'|'zxgbs'