3.2 Shell Scripts的简单实例练习

3.2 Shell Scripts的简单实例练习

磨刀不误砍柴功,下面就来编写一些实际例子吧:

3.2.1 简单例子

  1. 输入名字,进行组合并输出

    [haoqiqi@localhost bin]$ vim showname.sh
    #!/bin/bash
    # Program:
    #       User inputs his first name and last name.Program shows his full name.
    # History:
    # 2019/10/08    haoqiqi First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    read -p "Please input your first name:" firstname
    read -p "Please input your last name:" lastname
    echo -e "\nYour full name is: ${firstname} ${lastname}"
    
    [haoqiqi@localhost bin]$ sh showname.sh
    Please input your first name:hao
    Please input your last name:qiqi
    
    Your full name is: hao qiqi    
    
  2. 利用日期建立新文档

    # 连续使用三天的日期信息,建立三个文档
    [haoqiqi@localhost bin]$ vim create_3_filename.sh
    
    #!/bin/bash
    # Program:
    #       Program creates three files.which named by user'sinput and date command.
    # History:
    # 2019/10/08    haoqiqi         First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    # 1.输入文件名,保存至fileuser变量
    echo -e "I will use 'touch' commend to create 3 files."
    read -p "Please input your filename:"fileuser
    
    # 2.避免使用者因为错误按下enter造成fileuser为空,利用变量功能分析档名是否有设定
    filename=${fileuser:-"filename"}
    
    # 3.利用date指令获取日期
    date1=$(date --date='2 days ago' +%Y%m%d)       # 前两天的日期
    date2=$(date --date='1 days ago' +%Y%m%d)       # 前一天的日期
    date3=$(date +%Y%m%d)                           # 今天的日期
    file1=${filename}${date1}                       # 配置文件名
    file2=${filename}${date2}
    file3=${filename}${date3}
    
    # 4.建立文档名
    touch "${file1}"
    touch "${file2}"
    touch "${file3}"
    
    [haoqiqi@localhost bin]$ sh create_3_filename.sh
    I will use 'touch' commend to create 3 files.
    Please input your filename:fileuser
    

    建立结果需要去看主目录/bin文件夹下,新建了三个txt文件,命名方式为:filename+日期:
    在这里插入图片描述

  3. 简单的乘法运算

    [haoqiqi@localhost bin]$ vim multiplying.sh
    #!/bin/bash
    # Program:
    #       User inputs 2 integer numbers,program will cross these two numbers.
    # History:
    # 2019/10/13    haoqiqi         First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo -e "Please input 2 numbers. \n"
    read -p "first name:" firstnu
    read -p "second name:" secdnu
    total=$((${firstnu}*${secdnu}))
    echo -e "\n ${firstnu}*${secdnu}=${total}"
    
    [haoqiqi@localhost bin]$ sh multiplying.sh
    Please input 2 numbers. 
    
    first name:5
    second name:6
    
     5*6=30
    

    对于运算来说,一般建议使用如下方式运行:
    var=$((变量内容))
    这样做的好处是使用了两个小括号,终于可以使用空格符啦,不能使用空格真的很难受,但是加上两个小括号就可以使用啦。
    当想要计算含小数点的数据时,需要使用bc指令的协助:

    [haoqiqi@localhost bin]$ echo "59.9*48.158"|bc
    2884.664
    
  4. 数值运算,使用bc,计算pi

    [haoqiqi@localhost bin]$ vim cal_pi.sh
    #!/bin/bash
    # Program:
    #       User input a scale number to calculate pi number.
    # History:
    # 2019/10/13    haoqiqi         First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    echo -e "Calculate PI"
    echo -e "Please input a float number to calculate pi value.\n"
    read -p "The scale number (10~1000)?"checking
    num=${checking:-"10"}
    echo -e "Starting calculate pi.Be patient."
    time echo "scale=${num};4*a(l)" | be -lq
    
    
    [haoqiqi@localhost bin]$ sh cal_pi.sh
    Calculate PI
    Please input a float number to calculate pi value.
    
    The scale number (10~1000)?checking20
    Starting calculate pi.Be patient.
    0
    
    real	0m0.002s
    user	0m0.001s
    sys	0m0.000s
    # ??? 好像计算错了???
    # 先给自己留个坑,回头解决
    

3.2.2 script 的执行方式差异(source,sh script,./script)

对于script 来说,不同的执行方式可能会造成不一样的结果,下面就来讨论一下执行方式的不同会带来什么样的区别。

  1. 利用直接执行的方式执行 script
    利用此种方式执行的脚本程序会被当作是子程序来执行,且当子程序完成后,子程序内的变量会被当作局部变量,不回回传至主程序。举个例子吧
[haoqiqi@localhost ~]$ cd bin
[haoqiqi@localhost bin]$ echo ${firstname} ${lastname}
									#此处表明,这两个变量并不存在
[haoqiqi@localhost bin]$ sh showname.sh
Please input your first name:hao
Please input your last name:qiqi

Your full name is: hao qiqi			#利用直接执行的方式,得到了两个变量
[haoqiqi@localhost bin]$ echo ${firstname} ${lastname}
									#在主程序中,其仍不存在			
  1. 利用 source 执行脚本
    如果使用 source 执行脚本程序,则会出现不一样的情况:
[haoqiqi@localhost bin]$ source showname.sh
Please input your first name:hao
Please input your last name:qiqi

Your full name is: hao qiqi
[haoqiqi@localhost bin]$ echo ${firstname} ${lastname}
hao qiqi

可以看出来,利用source 执行程序时,程序中的变量均会被返回至主程序,可以直接进行调用。
这是因为 source 对 script 的执行方式为均在主程序下执行,因此各项动作都会在原本的 bash 内生效。

本节内容就这些了, 欢迎关注我的微信公众号与我联系。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值