一、linux shell 脚本

1、描述shell程序的运行原理(可附带必要的图形说明);

 Linux系统的shell作为操纵系统的外壳,为用户提供使用操纵系统的接口。它是命令语言、命令解释程序及程序设计语言的统称。
  shell是用户和Linux内核之间的接口程序,假如把Linux内核想象成一个球体的中心,shell就是围绕内核的外层。当从shell或其他程序向Linux传递命令时,内核会做出相应的反应。
  shell是一个命令语言解释器,它拥有自己内建的shell命令集,shell也能被系统中其他应用程序所调用。用户在提示符下输进的命令都由shell先解释然后传给Linux核心。
  有一些命令,比如改变工作目录命令cd,是包含在shell内部的。还有一些命令,例如拷贝命令cp和移动命令rm,是存在于文件系统中某个目录下的单独的程序。对用户而言,不必关心一个命令是建立在shell内部还是一个单独的程序。
  shell首先检查命令是否是内部命令,若不是再检查是否是一个应用程序(这里的应用程序可以是Linux本身的实用程序,如ls和rm,也可以是购买的贸易程序,如xv,或者是自由软件,如emacs)。然后shell在搜索路径里寻找这些应用程序(搜索路径就是一个能找到可执行程序的目录列表)。假如键进的命令不是一个内部命令并且在路径里没有找到这个可执行文件,将会显示一条错误信息。假如能够成功找到命令,该内部命令或应用程序将被分解为系统调用并传给Linux内核。

2、总结shell编程中所涉及到的所有知识点(如:变量、语法、命令状态等等等,要带图的哟

shell

定义变量

定义变量时,变量名不加美元符号($)如:

apple='app'

变量名的命名遵循的规则

#首个字符必须为字母

#中间不能有空格,可以使用下划线(_)

#不能使用标点符号

#不能使用bash里的关键字

使用变量

使用一个定义过的变量,只要在变量名前面加美元符号

例如

#myfather='xxx'

#echo $myfather

xxx

重新定义变量

已定义变量可以重新定义

删除变量

使用unset命令删除变量

[root@webTest ~]# app='apple'
[root@webTest ~]# echo $app
apple
[root@webTest ~]# unset app
[root@webTest ~]# echo $app

特殊变量列表

$0 : 当前脚本的文件名

$n :传递给脚本或函数的参数,n表示第n个参数。

$#:传递给脚本或函数的参数个数

$* :传递给脚本或函数的所有参数

$$ :当前shell进程Id,对于shel脚本,就是这些脚本所在的进程ID

命令行参数

运行脚本传递给脚本的参数称为命令行参数

$*和$@的区别

在没有“ ”的时候 $*和$@都以$1,$2,$3的形式显示

在有“ ”的时候 $*以$1,$2,$3整体显示,$@$1,$2,$3个体显示

环境变量

本地变量:当前shell的进程

环境变量:当前shel进程及其子进程

局部变量:某个函数执行过程

字符串

单引号

双引号

拼接字符串

运算符

Bash支持很多运算符,包括算数运算符、关系运算符、布尔运算符、字符串运算符和文件测试运算符。

算术运算符

关系运算符

布尔运算符

字符串运算符

文件测试运算符

流程控制

1.if...else...fi语句

 if [ expression ]
then
   Statement(s) to be executed if expression is true
else
   Statement(s) to be executed if expression is not true

fi


2.for循环语句

for 变量 in 列表
do
    command1
    command2
    ...
    commandN
done

3.while循环语句

while command
do
   Statement(s) to be executed if command is true
done

4.until 循环语句

until command
do
   Statement(s) to be executed until command is true
done
command 一般为条件表达式,如果返回值为false,则继续执行循环体内的语句,否则跳出循环。
例如,使用until命令输出0~9的数字:
#!/bin/bash
  
a=0
  
until [ ! $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

5.case...esac语句

case 值 in
模式1)
    command1
    command2
    command3
    ;;
模式2)
    command1
    command2
    command3
    ;;
*)
    command1
    command2
    command3
    ;;
esac

二、awk及sed工具使用详解

sed命令

sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

以上是个人理解图。

  1. 调用sed命令有两种形式:

  2. sed [options] 'command' file(s)

  3. sed [options] -f scriptfile file(s)

  4. a\

  5. 在当前行后面加入一行文本。

  6. b lable

  7. 分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾。

  8. c\

  9. 用新的文本改变本行的文本。

  10. d

  11. 从模板块(Pattern space)位置删除行。

  12. D

  13. 删除模板块的第一行。

  14. i\

  15. 在当前行上面插入文本。

  16. h

  17. 拷贝模板块的内容到内存中的缓冲区。

  18. H

  19. 追加模板块的内容到内存中的缓冲区

  20. g

  21. 获得内存缓冲区的内容,并替代当前模板块中的文本。

  22. G

  23. 获得内存缓冲区的内容,并追加到当前模板块文本的后面。

  24. l

  25. 列表不能打印字符的清单。

  26. n

  27. 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。

  28. N

  29. 追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码。

  30. p

  31. 打印模板块的行。

  32. P(大写)

  33. 打印模板块的第一行。

  34. q

  35. 退出Sed。

  36. r file

  37. 从file中读行。

  38. t label

  39. if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾。

  40. T label

  41. 错误分支,从最后一行开始,一旦发生错误或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾。

  42. w file

  43. 写并追加模板块到file末尾。

  44. W file

  45. 写并追加模板块的第一行到file末尾。

  46. !

  47. 表示后面的命令对所有没有被选定的行发生作用。

  48. s/re/string

  49. 用string替换正则表达式re。

  50. =

  51. 打印当前行号码。

  52. #

  53. 把注释扩展到下一个换行符以前。

  54. 以下的是替换标记

  55. *

  56. g表示行内全面替换。

  57. *

  58. p表示打印行。

  59. *

  60. w表示把行写入一个文件。

  61. *

  62. x表示互换模板块中的文本和缓冲区中的文本。

  63. *

  64. y表示把一个字符翻译为另外的字符(但是不用于正则表达式)

sed元字符集

^匹配行开始,如:/^xxx/匹配所有以xxx开头的行。

$ 匹配行结束,如:/xxx$/匹配所有以xxx结尾的行。

. 匹配一个非换行符的任意字符,如:/x.x/匹配x后接一个任意字符,最后是x。

* 匹配0个或多个字符,如:/*xxx/匹配所有模板是一个或多个空格后紧跟xxx的行。

[] 匹配一个指定范围内的字符,如/[xx]xx/匹配xxx和Xxx。

[^] 匹配一个不在指定范围内的字符,如:/[^A-F]xx/匹配不包含A-F字母开头,紧跟xx的行。

x\{m\} 重复字符x,m次,如:/1\{3\}/匹配包含3个1的行。

x\{m,\} 重复字符x,至少m次,如:/1\{3,\}/匹配至少有3个1的行。

x\{m,n\} 重复字符x,至少m次,不多于n次,如:/1\{3,6\}/匹配3~6个1的行。

sed实例

d命令

sed '3d ' apple ------ 删除apple文件第3行

sed '$d ' apple ------ 删除apple文件最后一行

sed '/test/d' apple ----- 删除apple文件中所有包含test的行

s命令

sed 's/app/apple/g' example-----在整行范围内把app替换为apple。如果没有g标记,则只有每行第一个匹配的app被替换成apple。

sed -n 's/^app/apple/p' example-----(-n)选项和p标志一起使用表示只打印那些发生替换的行。也就是说,如果某一行开头的app被替换成apple,就打印它。

sed 's/^192.168.0.1/&localhost/' example-----&符号表示替换换字符串中被找到的部份。所有以192.168.0.1开头的行都会被替换成它自已加 localhost,变成192.168.0.1localhost。

sed 's#10#100#g' example-----不论什么字符,紧跟着s命令的都被认为是新的分隔符,所以,“#”在这里是分隔符,代替了默认的“/”分隔符。表示把所有10替换成100。

e命令

sed -e '1,5d' -e 's/test/check/' example-----(-e)选项允许在同一行里执行多条命令。如例子所示,第一条命令删除1至5行,第二条命令用check替换test。命令的执 行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。

w 命令

sed -n '/test/w file' apple

a\命令

将 it is big 追加到以apple开头的行前面

sed '/^apple/a\/it is big' file

awk

AWK是一种优良的文本处理工具。它不仅是 Linux 中也是任何环境中现有的功能最强大的数据处理引擎之一。

1、OPTIONS:

-F:指定输入文本时所使用的字段分隔符

-v:自定义变量

2、pattern:

包括地址定界:

/pat1/,/pat2/

/pattern/

!/pattern

expression:表达式:>,<,=,>=,<=,=,!=,==,~

BEGIN:执行前的准备工作

END:执行后的收尾工作

流程控制

在awk 中两个特别的表达式,BEGIN和END,这两者都可用于pattern中。提供BEGIN和END的作用是给程序赋予初始状态和在程序结束之后执行一些扫尾的工作。任何在BEGIN之后列出的操作(在{}内)将在awk开始扫描输入之前执行,而END之后列出的操作将在扫描完全部的输入之后执行。因此,通常使用BEGIN来显示变量和预置(初始化)变量,使用END来输出最终结果。

if else语句

格式:

if(表达式)

语句

else

while语句

格式为:

while(表达式)

语句

do-while语句

格式为:

do

{

语句

}while(条件判断语句)

for语句

格式为:

for(初始表达式;终止条件;步长表达式)

{语句}

action

(1) Expressions   
(2) Control statements    
(3) Compound statements    
(4) input statements    
(5) output statements

awk使用

显示文本文件apple匹配(含有)字符串"xxx"的所有行。

awk '/xxx/' apple

它将显示所有匹配Sun或sun的行与匹配Moon或moon的行之间的行,并显示到标准输出上。

awk '/[Ss]un/,/[Mm]oon/' file

三、实战练习及解答

1、写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型;(不要怀疑,就是这么简单)

第一种方法:

[root@webTest home]# cat test.sh
#!/bin/bash
#$1 位置参数
if [ -z $1 ];thenecho "必须输入一个参数"exit 1
fi
if  [ -e $1 ];thenif [ -d $1 ]; then echo "输入的参数$1是个已存在的目录"fi
elseecho "输入的目录不存在,系统将自动为你创建该目录" mkdir -p $1echo   "你输入的目录为$1,且文件类型为:$(file $1|awk -F: '{print $2}')"
fi
[root@webTest home]# sh test.sh sss
输入的目录不存在,系统将自动为你创建该目录
你输入的目录为sss,且目录为:directory
[root@webTest home]# sh test.sh ssssss
输入的目录不存在,系统将自动为你创建该目录
你输入的目录为ssssss,且目录为:directory
[root@webTest home]# ll
total 120
-rw-r--r--.   1 root       root         244 Sep 17 17:58 1
drwxr-xr-x. 192 root       root       12288 Aug  4 14:53 html
drwxr-xr-x.   2 root       root        4096 Jan 21  2015 jar_logs
drwxr-xr-x.  31 root       root        4096 Sep 24 20:36 m2
-rw-r--r--.   1 root       root       10578 Jun 26 15:13 messageCenter.html
drwx------.   4        500        500  4096 Mar 27 16:46 micxp
drwxr-xr-x.   2 root       root        4096 Feb  2  2015 package
drwxr-xr-x.   2 root       root       12288 Jan 20  2015 photos
drwxr-xr-x.   6 root       root        4096 Jun 25 15:56 qianbinbin_software
drwxr-xr-x.   3 root       root        4096 Sep 22 10:06 run_script
drwxr-xr-x.   2 root       root        4096 Sep 24 21:35 ss
drwxr-xr-x.   2 root       root        4096 Sep 24 21:40 sss
drwxr-xr-x.   2 root       root        4096 Sep 24 21:39 ssss
drwxr-xr-x.   2 root       root        4096 Sep 24 21:40 ssssss
-rw-r--r--.   1 root       root         148 Sep 17 17:04 test2.sh
-rw-r--r--.   1 root       root         263 Sep 17 17:59 test3.sh
-rw-r--r--.   1 weihongjun weihongjun    17 Jun 25 16:25 test.htm
-rw-r--r--.   1 root       root           0 Jun 25 16:25 test.html
-rw-r--r--.   1 root       root         379 Sep 24 21:39 test.sh
drwx------.   4 weihongjun weihongjun  4096 Mar 30 09:45 weihongjun
drwxr-xr-x.   3 root       root        4096 Jan 21  2015 workspace
-rw-r--r--.   1 root       root          22 Sep 24 21:27 x.txt
drwxr-xr-x.   2 root       root        4096 Sep 24 21:31 xxx
drwxr-xr-x.   2 root       root        4096 Sep 24 21:30 xxxxxx
drwxr-xr-x.   2 root       root        4096 Sep 24 21:35 xxxxxxxxx

第二种方法:

#!/bin/bash
:<<COMMENTS
if [ -z $1 ];thenecho "必须输入一个参数"exit 1
fi
if  [ -e $1 ];thenif [ -d $1 ]; thenecho "输入的参数$1是个已存在的目录"fi
elseecho "输入的目录不存在,系统将自动为你创建该目录"mkdir -p $1echo   "你输入的目录为$1,且文件类型为:$(file $1|awk -F: '{print $2}')"
fi
COMMENTS
echo "===================================================================="
! [ -e $1 ] && mkdir -p $1  || echo "输入的文件或目录已经存在" && echo "你输入的目录为$1,且文件类型为:$(file $1|awk -F: '{print $2}')"

结果为:

2、写一个脚本,完成如下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;(使用read,依然如此简单)

[root@web01-dev duoduo_test]# clear
[root@web01-dev duoduo_test]# cat test2.sh
#!/bin/bash
read -p "Please input two integer:" -t 10 num1 num2
if [ $num1 -gt $num2 ];thenecho "$num1 大于 $num2."
elif [ $num1 -eq $num2 ];thenecho "$num1 等于 $num2."
elseecho "$num1 小于 $num2."
fi
[root@web01-dev duoduo_test]# sh test2.sh
Please input two integer:2 3
2 小于 3.
[root@web01-dev duoduo_test]# sh test2.sh
Please input two integer:2 2
2 等于 2.
[root@web01-dev duoduo_test]# sh test2.sh
Please input two integer:3 2
3 大于 2.
[root@web01-dev duoduo_test]#

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

1.
[root@webTest home]# cat xie.sh
#!/bin/bash
declare -i sum=0
for i in {1..100}
doif [ $[$i%2] -eq 1 ]thensum=$[$sum+$i]fi
done
echo "Sum is : $sum"
[root@webTest home]# sh xie.sh
Sum is : 25002.
#!/bin/bash
declare -i sum=0
for i in $(seq 1 2 100)
do
sum=$(($sum+$i))
done
echo "Sum is : $sum"
[root@webTest home]# sh xie.sh
Sum is : 25003.
#!/bin/bash
declare -i sum=0
declare -i i=0
while [ $i -le 100 ]
doif [ $[$i%2] -eq 1 ]thenlet sum+=$ifi
let i++
done
echo "sum is : $sum"
[root@webTest home]# sh xie.sh
sum is : 2500

4、写一个脚本实现如下功能:

(1) 传递两个文本文件路径给脚本;

(2) 显示两个文件中空白行数较多的文件及其空白行的个数;

(3) 显示两个文件中总行数较多的文件及其总行数;

5、写一个脚本

(1) 提示用户输入一个字符串;

(2) 判断:

如果输入的是quit,则退出脚本;

否则,则显示其输入的字符串内容;

#!/bin/bash
read -p "please input a string:" -t 10 str1
if [[ -z "$str1" ]]
then
echo "please input something"
elif [[ "$str1" == "quit" ]]
then
exit 0
else
echo "you inupt is $str1"
fi
[root@webTest home]# sh xie.sh
please input a string:please input something
[root@webTest home]# sh xie.sh
please input a string:test
you inupt is test
[root@webTest home]# sh xie.sh
please input a string:quit

6、写一个脚本,打印2^n表;n等于一个用户输入的值;(不好意思,我调皮了)

#!/bin/bash
if [ $# -lt 1 ]
then
echo "usage: command  number"
exit 1
fi
if [ $1 -le 0 ]
then
echo "please input a number big the 0"
elif [ $1 -eq 1 ]
then
echo "2x1=2"
else
sum=2
echo -n "2"
for i in `seq 2 $1`
dolet sum*=2echo -n "X2"
done
echo "=$sum"
fi
[root@webTest home]# sh xie.sh
usage: command  number
[root@webTest home]# sh xie.sh
usage: command  number
[root@webTest home]# sh xie.sh 0
please input a number big the 0
[root@webTest home]# sh xie.sh 1
2x1=2
[root@webTest home]# sh xie.sh 2
2X2=4
[root@webTest home]# sh xie.sh 3
2X2X2=8
[root@webTest home]# sh xie.sh 4
2X2X2X2=16
[root@webTest home]# sh xie.sh 8
2X2X2X2X2X2X2X2=256
[root@webTest home]# sh xie.sh 32
2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2=4294967296

7、写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数

#!/bin/bash
while [ ture ]
doecho "calcule usage is  script operater number1 number2"echo "operater have 3 methord:"echo -e "1.summary                         usage example:  \033[31msum number1 number2\033[0m"echo -e "2.Least Common Multiple (LCM)     usage example:  \033[31mlcm number1 number2\033[0m"echo -e "3.Greatest Common Divisor(GCD)    usage example:  \033[31mgcd number1 number2\033[0m"echo -e "inupt \033[31mquit\033[0m this script will exit"read -p "please input select:  " op num1 num2function testsum() {clearecho "================================================="echo -e "\033[31msum is `expr $num1 \* $num2` \033[0m"echo "================================================="}function gongyue(){a=$num1local b=$num2local tmp=0if [ $a -lt $b ]thentmp=$aa=$bb=$tmpfiwhile [ $b -ne 0 ]dotmp=$(($a%$b))a=$bb=$tmpdoneclearecho "================================================="echo -e "\033[31mgongyue is $a\033[0m"echo "================================================="}function gongbei(){clearecho "================================================="echo -e  "\033[31mgongbei is $(($num1*$num2/$a))\033[0m"echo "================================================="}if [[ "$op" == "quit" ]]thenbreakexit 0elif [[ "$op" == "sum" ]]thentestsum num1 num2elif [[ "$op" == "lcm" ]]thengongyue num1 num2elif [[ "$op" == "gcd" ]]thengongbei num1 num2elseecho "input error" fi
donewhile [ ture ]
doecho "calcule usage is  script operater number1 number2"echo "operater have 3 methord:"echo -e "1.summary                         usage example:  \033[31msum number1 number2\033[0m"echo -e "2.Least Common Multiple (LCM)     usage example:  \033[31mlcm number1 number2\033[0m"echo -e "3.Greatest Common Divisor(GCD)    usage example:  \033[31mgcd number1 number2\033[0m"echo -e "inupt \033[31mquit\033[0m this script will exit"read -p "please input select:  " op num1 num2function testsum() {clearecho "================================================="echo -e "\033[31msum is `expr $num1 \* $num2` \033[0m"echo "================================================="}function gongyue(){a=$num1local b=$num2local tmp=0if [ $a -lt $b ]thentmp=$aa=$bb=$tmpfiwhile [ $b -ne 0 ]dotmp=$(($a%$b))a=$bb=$tmpdoneclearecho "================================================="echo -e "\033[31mgongyue is $a\033[0m"echo "================================================="}function gongbei(){clearecho "================================================="echo -e  "\033[31mgongbei is $(($num1*$num2/$a))\033[0m"echo "================================================="}if [[ "$op" == "quit" ]]thenbreakexit 0elif [[ "$op" == "sum" ]]thentestsum num1 num2elif [[ "$op" == "lcm" ]]thengongyue num1 num2elif [[ "$op" == "gcd" ]]thengongbei num1 num2elseecho "input error" fi
done
calcule usage is  script operater number1 number2
operater have 3 methord:
1.summary                         usage example:  sum number1 number2
2.Least Common Multiple (LCM)     usage example:  lcm number1 number2
3.Greatest Common Divisor(GCD)    usage example:  gcd number1 number2
inupt quit this script will exit
please input select:  sh xie    ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Z
[31]+  Stopped                 sh xie.sh
[root@webTest home]# sh xie.sh
calcule usage is  script operater number1 number2
operater have 3 methord:
1.summary                         usage example:  sum number1 number2
2.Least Common Multiple (LCM)     usage example:  lcm number1 number2
3.Greatest Common Divisor(GCD)    usage example:  gcd number1 number2
inupt quit this script will exit
please input select:  sum 1397 2413
=================================================
sum is 3370961
=================================================
calcule usage is  script operater number1 number2
operater have 3 methord:
1.summary                         usage example:  sum number1 number2
2.Least Common Multiple (LCM)     usage example:  lcm number1 number2
3.Greatest Common Divisor(GCD)    usage example:  gcd number1 number2
inupt quit this script will exit
please input select:  lcm 1397 2413
=================================================
gongyue is 127
=================================================
calcule usage is  script operater number1 number2
operater have 3 methord:
1.summary                         usage example:  sum number1 number2
2.Least Common Multiple (LCM)     usage example:  lcm number1 number2
3.Greatest Common Divisor(GCD)    usage example:  gcd number1 number2
inupt quit this script will exit
please input select:  gcd 1397 2413
=================================================
gongbei is 26543
=================================================
calcule usage is  script operater number1 number2
operater have 3 methord:
1.summary                         usage example:  sum number1 number2
2.Least Common Multiple (LCM)     usage example:  lcm number1 number2
3.Greatest Common Divisor(GCD)    usage example:  gcd number1 number2
inupt quit this script will exit
please input select:  quit
[root@webTest home]#

8、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。

#!/bin/bash
#
sum(){
#  val=$[$num1+$num2]val=`expr $num1 + $num2`echo "Total value : $val"
}gcd(){
if [ $num1 -eq $num2 ]; thenecho "max is:${num1},mini is:${num1}."exit
fi
if [ $num1 -gt $num2 ]; thenGREAT=$num1SMALL=$num2
elseGREAT=$num2SMALL=$num1
fi
declare -i GCD_RESULT=1
declare -i greattmp=1
declare -i smalltmp=1
declare -i i=1
while [ $i -le $SMALL ]; dogreattmp=`expr $GREAT % $i`smalltmp=`expr $SMALL % $i`[ ${greattmp} -eq 0 ] && [ ${smalltmp} -eq 0 ] && GCD_RESULT=${i}i=`expr ${i} + 1`
done
#LCM_RESULT=`expr $SMALL / $GCD_RESULT`
#LCM_RESULT=`expr $LCM_RESULT \* $GREAT`
echo "max is:${GCD_RESULT}"
}lcm(){
if [ $num1 -eq $num2 ]; thenecho "max is:${num1},mini is:${num1}."exit
fi
if [ $num1 -gt $num2 ]; thenGREAT=$num1SMALL=$num2
elseGREAT=$num2SMALL=$num1
fi
declare -i LCM_RESULT=1
declare -i greattmp=1
declare -i smalltmp=1
declare -i GCD_RESULT=1
declare -i i=1
while [ $i -le $SMALL ]; dogreattmp=`expr $GREAT % $i`smalltmp=`expr $SMALL % $i`[ ${greattmp} -eq 0 ] && [ ${smalltmp} -eq 0 ] && GCD_RESULT=${i}i=`expr ${i} + 1`
done
LCM_RESULT=`expr $SMALL / $GCD_RESULT`
LCM_RESULT=`expr $LCM_RESULT \* $GREAT`
echo "mini is:${LCM_RESULT}."
}
prog=$(basename $0)
read -p "Plz enter two integer: " num1 num2
read -p "Plz enter sum | gcd | lcm : " num
case $num insum)sum;;gcd)gcd;;lcm)lcm;;*)echo "Usage:$prog sum|gcd|lcm"exit 1esac[root@webTest home]# sh xie.sh
Plz enter two integer: 91 49
Plz enter sum | gcd | lcm : sum
Total value : 140
[root@webTest home]# sh xie.sh
Plz enter two integer: 91 49
Plz enter sum | gcd | lcm : gcd
max is:7
[root@webTest home]# sh xie.sh
Plz enter two integer: 91 49
Plz enter sum | gcd | lcm : lcm
mini is:637.
[root@webTest home]#

转载于:https://blog.51cto.com/icestick8586/1698369

linux运维实战练习及linux shell脚本、awk、sed工具命令学习总结相关推荐

  1. Linux运维实战:CentOS7.6操作系统从入门到精通(6-10)

    第6章 CentOS用户管理 第7章 CentOS 7 文件权限管理 第8章 CentOS 7 程序包的管理与安装 第9章 文件的归档和压缩 第10章 CentOS 7 系统进程管理 Linux运维实 ...

  2. Linux运维实战|大文件切割

    介绍 日常工作中需要对日志文件进行分析,当日志文件过大时,Linux中使用vim.cat.vim.grep.awk等这些工具对大文件日志进行分析将会成为梦魇,具体表现在: 执行速度缓慢,文件内容需要加 ...

  3. 《Linux运维实战:Centos7.6一键离线部署mongodb4.2.23副本集群》

    一.部署背景 由于业务系统的特殊性,我们需要面向不通的客户安装我们的业务系统,而作为基础组件中的mongodb针对不同的客户环境需要多次部署,作为一个运维工程师,提升工作效率也是工作中的重要一环.所以 ...

  4. 老男孩linux运维实战培训中心讲师介绍

    老男孩linux运维实战培训中心讲师介绍 老男孩,资深unix/Linux系统运维网站架构专家.高级运维总监.从事一线网站运维及系统架构管理10年以上,13年的教育教学培训经历(擅长教育心理,职业规划 ...

  5. 《Linux运维实战:使用openssl生成免费证书》

    文章目录 一.背景 二.生成证书 2.1.证书格式为cer 2.2.证书格式为pem 三.Nginx配置 四.安装客户端证书 总结:整理不易,如果对你有帮助,可否点赞关注一下? 一.背景 由于第三方外 ...

  6. Linux运维实战:CentOS7.6操作系统从入门到精通(11-15)

    第11章 重定向和文件的查找 第12章 磁盘介绍及管理 第13章 Linux文件系统结构 第14章 RAID的原理与搭建 第15章 LVM管理和SSM工具使用 Linux运维实战:CentOS7.6操 ...

  7. 《Linux运维实战:使用mongodump和mongorestore备份与恢复Mongodb数据》

    一.备份与恢复方案 mongodump是MongoDB官方提供的备份工具,它可以从MongoDB数据库读取数据,并生成BSON文件,mongodump适合用于备份和恢复数据量较小的MongoDB数据库 ...

  8. linux crontab不运行,Linux运维知识之解决Linux中crontab不执行ntpdate问题

    本文主要向大家介绍了Linux运维知识之解决Linux中crontab不执行ntpdate问题,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助. 解决Linux中crontab尚未 ...

  9. linux 系统速度慢,Linux运维人员你知道Linux系统运行速度太慢的原因吗?

    今天小编要跟大家分享的文章是关于Linux系统运行速度太慢的原因.相信正在从事Linux运维工作的小伙伴都会遇到过Linux系统运行速度过慢的问题,那遇到这种情况时,应该怎么解决呢?我们在搞清楚如何加 ...

最新文章

  1. java培训分享:学习java开发的优势是什么
  2. 风云编程python-动态排名可视化——带你领略编程语言20年风云变化
  3. telnet WIN7 不回显的解决办法
  4. SpringBoot: xxxx for method parameter type String is not present]
  5. mysql root密码重置
  6. 《高效能人士的七个习惯》
  7. 解码(七):音频重采样SwrContext和swr_convert相关函数详解
  8. linux 中ifconfig命令 结果解释
  9. pytorch---nn模块(3)自定义nn 模块
  10. Centos7安装jdk1.8
  11. web压力测试工具比较
  12. ios 融云 重写对话列表_iOS集成融云SDK即时通讯
  13. 线程池的使用和工作原理
  14. 模拟登录人人网,豆瓣
  15. 数据库考研信息管理系统
  16. 山东省大学生软件设计大赛一等奖作品参赛视频
  17. 安装andriod studio
  18. 截止9月10日苹果审核指南中文版
  19. Java_运动员和教练案例代码实现
  20. vue-element-table列内容显示过多隐藏

热门文章

  1. ext js IE9显示白板 页面浏览器模式强制渲染IE8
  2. android消息池,回转寿司你一定吃过!——Android消息机制(构造)
  3. phone clone android,Phone Clone
  4. Xamarin Essentials教程振动Vibration
  5. Xamarin iOS教程之进度条和滚动视图
  6. autosar架构详细介绍_【技术】基于AUTOSAR的电机驱动系统分析
  7. fastreport.net 交叉表居中显示_浅析Sql中内连接、外连接、全连接、交叉连接的区别...
  8. 静态路由协议的默认管理距离是_距离矢量路由选择协议
  9. 集合php,PHP问题集合
  10. python值nonzero函数的解析