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

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

2015-09-20 20:05 549 查看
1、描述shell程序的运行原理(可附带必要的图形说明);
Linux系统提供给用户的最重要的系统程序是Shell命令语言解释程序。它不属于内核部分,而是在核心之外,以用户态方式运行。其基本功能是解释并执行用户打入的各种命令,实现用户与Linux核心的接口。系统初启后,核心为每个终端用户建立一个进程去执行Shell解释程序。它的执行过程基本上按如下步骤:
(1)读取用户由键盘输入的命令行。(2)分析命令,以命令名作为文件名,并将其它参数改造为系统调用execve( )内部处理所要求的形式。 (3)终端进程调用fork( )建立一个子进程。 (4)终端进程本身用系统调用wait4( )来等待子进程完成(如果是后台命令,则不等待)。当子进程运行时调用execve( ),子进程根据文件名(即命令名)到目录中查找有关文件(这是命令解释程序构成的文件),将它调入内存,执行这个程序(解释这条命令)。 (5)如果命令末尾有&号(后台命令符号),则终端进程不用系统调用wait4( )等待,立即发提示符,让用户输入下一个命令,。如果命令末尾没有&号,则终端进程要一直等待,当子进程(即运行命令的进程)完成处理后终止,向父进程(终端进程)报告,此时终端进程醒来,在做必要的判别等工作后,终端进程发提示符,让用户输入新的命令,重复上述处理过程。2、总结shell编程中所涉及到的所有知识点(如:变量、语法、命令状态等等等,要带图的哟);
shell知识点总结
3、总结课程所讲的所有循环语句、条件判断的使用方法及其相关示例;(if (jpg|png is not exist);echo ”You say a XX“)
循环语句及判断条件
4、总结文本处理工具sed及awk的用法;(必须附带示例)
5、写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型;(不要怀疑,就是这么简单)
[root@pks tmp]# vim checkfile.sh
[root@pks tmp]# bash -n checkfile.sh
[root@pks tmp]# bash -x checkfile.sh
+ '[' 0 -lt 1 ']'
+ echo 'please enter /path/to/somefile'
please enter /path/to/somefile
+ exit 1
[root@pks tmp]# bash -x checkfile.sh /etc/fstab
+ '[' 1 -lt 1 ']'
+ '[' -e /etc/fstab ']'
+ echo '/etc/fstab is exists'
/etc/fstab is exists
+ file /etc/fstab
/etc/fstab: ASCII text
[root@pks tmp]# cat checkfile.sh
#!/bin/bash
#
if [ $# -lt 1 ];then
echo "please enter /path/to/somefile"
exit 1
fi
if [ -e $1 ];then
echo "$1 is exists"
file $1
else mkdir -p $1
fi


6、写一个脚本,完成如下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;(使用read,依然如此简单)
[root@pks tmp]# bash -x bidaxiao.sh
+ read -p 'enter two number' -t 30 a b
enter two number5 9
+ '[' 5 -gt 9 ']'
+ '[' 5 -lt 9 ']'
+ max=9
+ min=5
[root@pks tmp]# bash -x bidaxiao.sh
+ read -p 'enter two number' -t 30 a b
enter two number10 7
+ '[' 10 -gt 7 ']'
+ max=10
+ min=7
[root@pks tmp]# cat bidaxiao.sh
#!/bin/bash
#
read -p "enter two number" -t 30  a  b
if [ $a -gt $b ];then
max=$a
min=$b
elif [ $a -lt $b ];then
max=$b
min=$a
else
echo"two number is same"
fi


7、求100以内所有奇数之和(至少用3种方法。是的这是我们的作业^_^)
方法1

[root@pks tmp]# bash -n sum1.sh
[root@pks tmp]# bash  sum1.sh
the sum=2500
[root@pks tmp]# cat sum1.sh
#!/bin/bash
#
declare -i sum=0
declare -i i=0
until  [ $i -gt 100 ]; do
if [ $[${i}%2] -ne 0 ]; then
let sum+=$i
fi
let i++
done
echo "the sum=$sum"
[root@pks tmp]#
方法2
root@pks tmp]# cat sum2.sh
#!/bin/bash
#
declare -i sum=0
declare -i i=0
while  [ $i -le 100 ]; do
if [ $[${i}%2] -ne 0 ]; then
let sum+=$i
fi
let i++
done
echo "the sum=$sum"
[root@pks tmp]#


方法3
[root@pks tmp]# bash  -n sum3.sh
[root@pks tmp]# bash   sum3.sh
the sum=2500
[root@pks tmp]# cat sum3.sh
#!/bin/bash
#
declare -i sum=0
declare -i i=0
for (( i=1;i<=100;i++ ));do
if [ $[${i}%2] -ne 0 ]; then
let sum+=$i
fi
done
echo "the sum=$sum"
[root@pks tmp]#
8、写一个脚本实现如下功能:
(1) 传递两个文本文件路径给脚本;
(2) 显示两个文件中空白行数较多的文件及其空白行的个数;
(3) 显示两个文件中总行数较多的文件及其总行数;
[root@pks tmp]# cat /etc/test
pks
[root@pks tmp]# cat /etc/test1
pks
[root@pks tmp]# bash -n test.sh
[root@pks tmp]# bash test.sh
enter two file/etc/test /etc/test1
the max space file is /etc/test : 6
the max file is /etc/test : 7
[root@pks tmp]# cat test.sh
#!/bin/bash
#
read -p "enter two file"  -t 10 file1 file2
a=$(grep "^$" $file1 | wc -l )
b=$(grep "^$" $file2 | wc -l )
if [ $a -gt $b ];then
echo "the max space file is $file1 : $a"
else
echo "the max space file is $file2 : $b"
fi
c=$( wc -l $file1 | cut -d ' ' -f1 )
d=$( wc -l $file2 | cut -d ' ' -f1 )
if [ $c -gt $d ];then
echo "the max file is $file1 : $c"
else
echo "the max file is $file2 : $d"
fi
[root@pks tmp]#


9、写一个脚本
(1) 提示用户输入一个字符串;
(2) 判断:
如果输入的是quit,则退出脚本;
否则,则显示其输入的字符串内容;
[root@pks tmp]# bash strip1.sh
please enter string: quit
quit this strip
[root@pks tmp]# bash strip1.sh
please enter string: pks
pks
[root@pks tmp]# cat strip1.sh
#!/bin/bash
#
read -p "please enter string: " -t 10 str
if [ $str == 'quit' ];then
echo "quit this strip"
exit 0
else
echo "$str"
fi
[root@pks tmp]#
10、写一个脚本,打印2^n表;n等于一个用户输入的值;(不好意思,我调皮了)
[root@pks tmp]# bash strip2.sh
please enter a number then print 2^number 3
2^1=2
2^2=4
2^3=8
[root@pks tmp]# bash strip2.sh
please enter a number then print 2^number 5
2^1=2
2^2=4
2^3=8
2^4=16
2^5=32
[root@pks tmp]# cat strip2.sh
read -p "please enter a number then print 2^number " -t 10 n
declare -i muil=1
for (( i=1;i<=n;i++));do
let muil*=2
echo "2^$i=$muil"
done
[root@pks tmp]#
11、写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数3、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。
公约数特性:能同时整除若干个数字,最大公约数不会大过若干个数字中最小的数值
公倍数特性:能被若干数数字同时整除,最小公倍数不会大过若干数字的乘积
利用以上特性使用循环穷举的方法
#!/bin/bash
#

sum(){
read -p "enter two number for man yueshu : " a b
sum=$[$a+$b]
echo "sum=$sum"
}

max(){
read -p "enter two number for man yueshu : " a b
if [ $a -ge $b ]; then
for ((i=1;i<=b;i++));do
if [ $[$a%$i] -eq 0  -a  $[$b%$i] -eq 0 ];then
echo "$i">/mnt/max.txt
fi
done
fi
if [ $a -lt $b ]; then
for ((i=1;i<=a;i++));do
if [ $[$a%$i] -eq 0  -a  $[$b%$i] -eq 0 ];then
echo "$i">/mnt/max.txt
fi
done
fi
cat /mnt/max.txt
}

min(){
read -p "enter two number for mingongbeishu : " a b
c=$[$a*$b]
for ((i=1;i<=c;i++ ));do
if [ $[$i%$a] -eq 0 -a $[$i%$b] -eq 0  ]; then
echo "$i"
break
fi
done
}

read -p "enter sum | max | min :  "  string

case $string in
sum)
sum
;;
max)
max
;;
min)
min
;;
*)
echo """enter sum | max | min :  "
exit 1
esac
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息