zoukankan      html  css  js  c++  java
  • 第十三章、学习 Shell Scripts 简单的 shell script 练习

    简单的 shell script 练习




    简单范例




    • 对谈式脚本:变量内容由使用者决定
    [root@www scripts]# vi sh02.sh
    #!/bin/bash
    # Program:
    #	User inputs his first name and last name.  Program shows his full name.
    # History:
    # 2005/08/23	VBird	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 "
    Your full name is: $firstname $lastname" # 结果由萤幕输出
    




    • 随日期变化:利用 date 进行文件的创建
    [root@www scripts]# vi sh03.sh
    #!/bin/bash
    # Program:
    #	Program creates three files, which named by user's input 
    #	and date command.
    # History:
    # 2005/08/23	VBird	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' command to create 3 files." # 纯粹显示资讯
    read -p "Please input your filename: " fileuser         # 提示使用者输入
    
    # 2. 为了避免使用者随意按 Enter ,利用变量功能分析档名是否有配置?
    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"
    




    • 数值运算:简单的加减乘除

    『 $((计算式)) 』:数值运算。bash shell 里头默认仅支持整数

    [root@www scripts]# vi sh04.sh
    #!/bin/bash
    # Program:
    #	User inputs 2 integer numbers; program will cross these two numbers.
    # History:
    # 2005/08/23	VBird	First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo -e "You SHOULD input 2 numbers, I will cross them! 
    "
    read -p "first number:  " firstnu
    read -p "second number: " secnu
    total=$(($firstnu*$secnu))
    echo -e "
    The result of $firstnu x $secnu is ==> $total"
    

    在数值的运算上,我们可以使用『 declare -i total=$firstnu*$secnu 』 也可以使用上面的方式来进行!基本上,鸟哥比较建议使用这样的方式来进行运算:

    var=$((运算内容))

    不但容易记忆,而且也比较方便的多,因为两个小括号内可以加上空白字节!

    [root@www scripts]# echo $(( 13 % 3 ))
    1
    

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

    不同的 script 运行方式会造成不一样的结果!尤其影响 bash 的环境很大呢!脚本的运行方式除了前面小节谈到的方式之外,还可以利用 source 或小数点 (.) 来运行!


    • 利用直接运行的方式来运行 script

     script会使用一个新的 bash 环境来运行脚本内的命令!也就是说,使用这种运行方式时, 其实 script 是在子程序的 bash 内运行!『当子程序完成后,在子程序内的各项变量或动作将会结束而不会传回到父程序中』!

    [root@www scripts]# echo $firstname $lastname
        <==确认了,这两个变量并不存在!
    [root@www scripts]# sh sh02.sh
    Please input your first name: VBird <==这个名字是鸟哥自己输入的
    Please input your last name:  Tsai 
    
    Your full name is: VBird Tsai      <==看吧!在 script 运行中,这两个变量有生效
    [root@www scripts]# echo $firstname $lastname
        <==事实上,这两个变量在父程序的 bash 中还是不存在的!
    

    当你使用直接运行的方法来处理时,系统会给予一支新的 bash 让我们来运行 sh02.sh 里面的命令,因此你的 firstname, lastname 等变量其实是在下图中的子程序 bash 内运行的。 当 sh02.sh 运行完毕后,子程序 bash 内的所有数据便被移除,因此上表的练习中,在父程序底下 echo $firstname 时, 就看不到任何东西了!

    sh02.sh 在子程序中运行
    图 2.2.1、sh02.sh 在子程序中运行


    • 利用 source 来运行脚本:在父程序中运行
    [root@www scripts]# source sh02.sh
    Please input your first name: VBird
    Please input your last name:  Tsai
    
    Your full name is: VBird Tsai
    [root@www scripts]# echo $firstname $lastname
    VBird Tsai  <==嘿嘿!有数据产生喔!
    

    你不注销系统而要让某些写入 ~/.bashrc 的配置生效时,需要使用『 source ~/.bashrc 』而不能使用『 bash ~/.bashrc 』!

    sh02.sh 在父程序中运行
    图 2.2.2、sh02.sh 在父程序中运行

     

  • 相关阅读:
    python 得到列表的第二大的元素
    PHP 打印输出数组内容及结构 print_r 与 var_dump 函数
    php 数组元素加法
    PHP unlink() 函数
    PHP 文件创建/写入
    PHP chmod() 函数
    php 压缩文件
    php 每隔30s在页面显示字符串
    PHP basename() 函数
    PHP rtrim() 函数
  • 原文地址:https://www.cnblogs.com/ghgyj/p/3982831.html
Copyright © 2011-2022 走看看