Shell脚本编程

您所在的位置:网站首页 shell程序怎么编写 Shell脚本编程

Shell脚本编程

2023-09-01 23:45| 来源: 网络整理| 查看: 265

一、多条shell命令的使用

 

[root@ceph01 ~]# date ; who Thu Jan 3 08:51:38 CST 2019 root pts/0 2019-01-03 08:50 (192.16.1.2)

     其实这就是一个简单的脚本,date先执行输出然后执行who输出。在shell命令行可以连续执行多条命令,命令行的最大字符数不得超过255个字符。

二、如何创建脚本文件 $:vim example.sh #!/bin/bash                  #指明所使用的shell  #this script displays the date and who's logged on          #本脚本的功能说明,注释 date                             #shell命令 who        

 运行脚本:

$:chmod u+x example.sh $: ./example.sh Thu Jan 3 09:11:09 CST 2019 root pts/0 2019-01-03 08:50 (192.16.1.2) 三、显示提示消息

(1)   使用echo命令就能显示一个简单的文本字符串

$: echo this is a test this is a test

(2)echo命令既可以用双引号也可以用单引号来标记文本字符串

[root@ceph01 test]# echo "This is a test to see if you're paying attention" This is a test to see if you're paying attention [root@ceph01 test]# echo 'Rich says "scripting is easy".' Rich says "scripting is easy". [root@ceph01 test]# echo -n "This is a test" #   This is a test  [root@ceph01 test]# 四、使用变量

      通常想要在shell命令中结合其他数据以处理信息,为此可以使用变量。变量允许在shell脚本中暂存信息,以便与脚本中其他命令一起使用。

(1)环境变量

    使用$符号的环境变量可以从脚本中引进这些环境变量

[root@ceph01 test]# cat example1.sh #!/bin/bash # display user information from the system echo "User info for usered: $USER" echo UID:$UID echo HOME:$HOME

 

 

    运行输出:

[root@ceph01 test]# ./example1.sh User info for usered: root UID:0 HOME:/root

(2)用户变量

     shell脚本中允许在脚本中设置和使用自己的变量,设置的变量可以暂时存储数据并在脚本中使用它们,可以由20个字符的字母 、数字 、下划线组成的文本字符串!

[root@ceph01 test]# cat example2.sh #!/bin/bash # testing variables days=10 guest="Katie" echo "$guest cheaked in $days days ago" days=5 guest="Jessica" echo "$guest cheaked in $days days ago"

运行脚本:

[root@ceph01 test]# ./example2.sh Katie cheaked in 10 days ago Jessica cheaked in 5 days ago

(3)反引号

     反引号允许将shell命令的输出赋值给变量,(在美式键盘上,它通常与~号在同一按键上)。

[root@ceph01 test]# cat example3.sh #!/bin/bash # using the backtick character testing=`date`  #变量testing就是接收的date命令的输出值 echo "date and time are:" $testing

运行脚本:

[root@ceph01 test]# ./example3.sh date and time are: Thu Jan 3 14:16:19 CST 2019 五、重定向输入输出

 重定向输入输出就是将命令的输出重定向到另一位置,如文件中,重定向既能用于输入也能用于输出。

(1)输出重定向(> 大于号)

[root@ceph01 test]# date > outfile [root@ceph01 test]# cat outfile Thu Jan 3 14:28:36 CST 2019

如果想将输出内容附加到现有文件,而不是重写文件则使用(>>)

[root@ceph01 test]# who >> outfile [root@ceph01 test]# cat outfile Thu Jan 3 14:28:36 CST 2019 root pts/0 2019-01-03 14:13 (192.16.1.2) root pts/1 2019-01-03 10:16 (10.2.152.249)

(2)输入重定向(< 小于号)

    wc命令对数据中的文本计数(默认情况生成3个值):

                    文本的行数

                    文本的单词数

                    文本的字节数

[root@ceph01 test]# wc < outfile 3 16 135

 内置重定向( test string 3 > EOF 3 9 42 六、管道(|)

可以将输出重定向到另一条命令,而不是将命令的输出重定向到一个文件,这个过程称为管道传送

[root@ceph01 test]# rpm -qa | sort > rpm.list [root@ceph01 test]# more rpm.list abattis-cantarell-fonts-0.0.25-1.el7.noarch abrt-2.1.11-48.el7.centos.aarch64 七、数学计算

(1)expr命令

[root@ceph01 test]# expr 1 + 5 6 [root@ceph01 test]# expr 2 * 5 #*属于特殊字符,需加(\)转义字符来识别 expr: syntax error [root@ceph01 test]# expr 2 \* 5 10

[root@ceph01 test]# cat expr.sh #!/bin/bash # An example of using the expr command var1=10 var2=20 var3=`expr $var2 / $var1` echo The result is $var3

运行脚本:

[root@ceph01 test]# ./expr.sh The result is 2

(2)括号的使用

[root@ceph01 test]# var1=$[1 + 5] [root@ceph01 test]# echo $var1 6 [root@ceph01 test]# var2=$[$var1 * 2] [root@ceph01 test]# echo $var2 12 [root@ceph01 test]# cat expr1.sh #!/bin/bash var1=100 var2=50 var3=45 var4=$[$var1 * ($var2 - $var3)] echo The final result is $var4

运行脚本

[root@ceph01 test]# ./expr1.sh The final result is 500

(3)浮点解决方案

      1> bc的基本常识

       bash计算器实际上是一种编程语言,该语言允许在命令行中输入浮点表达式,然后解释表达式并计算他们,最后返回结果。

       bash计算器可以识别:数字(整点和浮点);变量(简单变量和数组);注释;表达式;编程语言;函数

[root@ceph01 test]# bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 12 * 5.4 64.8 3.14 * (3+5) 25.12 quit

浮点算术被称为scale的内置变量控制。可通过设置scale的值来控制十进制小数位数。(-q命令行参数禁止bash计算器的冗余欢迎标语)

[root@ceph01 test]# bc -q 3.44 / 5 0 scale=4 3.44 / 5 .6880 quit [root@ceph01 test]# bc -q var1=10 var1 * 2 20 var2 = var1 / 2 print var2 5 quit

    2> bc在脚本中的使用

[root@ceph01 test]# cat bc.sh #!/bin/bash var1=`echo " scale=4; 3.44 /5" | bc` echo The answer is $var1 [root@ceph01 test]# ./bc.sh The answer is .6880 [root@ceph01 test]# cat bc1.sh #!/bin/bash var1=20 var2=3.14159 var3=`echo " scale=4; $var1 * $var2 " | bc` var4=`echo " scale=4; $var3 * $var2 " | bc` echo The final result is $var4 [root@ceph01 test]# ./bc1.sh The final result is 197.39175 八、退出脚本

 

$:echo  $?      #查询脚本执行退出状态

或脚本中加exit 0等!!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3