zoukankan      html  css  js  c++  java
  • Shell Step by Step (4) —— Cron & Echo

    6.脚本定时任务

    # Example of job definition:
    # .-------------------------  minute (0 - 59)
    # |    .---------------------  hour (0 - 23)
    # |    |    .-----------------  day of month (1 - 31)
    # |    |    |    .-------------  month (1 - 12) 
    # |    |    |    |    .---------  day of week (0 - 6) 
    # |    |    |    |    |
    # *    *    *    *    *   user-name   command to be executed

    7.查看当前用户的UID

    root@kallen:/usr/data/kallendb_backup# ps -ef | grep UID 
    UID PID PPID C STIME TTY TIME CMD 
    root 2872 2384 0 09:43 pts/2 00:00:00 grep --color=auto UID

    8.用Shell模拟一个进度条

      #! /bin/bash
      #
      # Progress Bar
      # Print # to view the process bar
    
      # create variable
      b=''
    
      # for loop
      for ((i=0;$i<=100;i+=2))
      do
          printf "Progress:[%-50s]%d%%
    " $b $i
          sleep 0.1
    
          b=#$b
      done
      echo

    源码

    在Shell脚本的编写应用中,有时候会须要用到图形界面的案例,比方默认cp复制文件为静默模式。无法看到拷贝的进度与百分比。

    而dialog正是为Shell提供图形界面的工具,该工具能够为Shell脚本提供各式各样的图形界面,今天为大家介绍的是dialog提供的进度条图形功能。

    dialog指令能够单独运行。格式为

     dialog --title "Copy" --gauge "files" 6 70 10

    备注:

    title表示图形进度条的标题。
    gauge为正文内容。进度条高度为6,宽度70。显示运行进度为10%

    for i in {1..100} ; 
    do sleep 1; 
        echo $i | dialog --title 'Copy' --gauge 'I am busy!' 10 70 0; 
    done

    以下案例中通过统计源文件个数。再据此计算出复制文件的百分比,在Shell中提供进度的显示。


    该脚本有两个參数。第一个參数为源文件路径,第二个參数为目标路径。
    假设您的应用案例不同能够据此稍作改动就可以使用。

    #!/bin/bash    
    # Description: A shell script to copy parameter1 to 
    # parameter2 and Display a progress bar    
    # Author:Jacob    
    # Version:0.1 beta    
    
    # Read the parameter for copy,$1 is source dir 
    # and $2 is destination dir.    
    dir=$1/*    
    des=$2    
    # Test the destination dirctory whether exists    
    [ -d $des ] && echo "Dir Exist" && exit 1    
    # Create the destination dirctory    
    mkdir $des    
    # Set counter, it will auto increase to the number of 
    # source file.    
    i=0    
    # Count the number of source file    
    n=`echo $1/* |wc -w`    
    
    for file in `echo $dir`    
    do
        # Calculate progress    
        percent=$((100*(++i)/n))    
        cat <<EOF    
        XXX    
        $percent    
        Copying file $file ...    
        XXX    
        EOF    
        /bin/cp -r $file $des &>/dev/null
    done | dialog --title "Copy" --gauge "files" 6 70    
    clear

    效果如图:
    效果图

    9.Echo输出

    功能说明: 显示文字
    语 法:

    echo [ -ne ]  [ 字符串 ]echo [ --help ]  [--version ]  

    參数:

    -n          不要在最后自己主动换行
    -e          若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出;  
              删除前一个字符;    
    f          换行但光标仍旧停留在原来的位置;     
    
              光标移至行首。但不换行;    
    	          插入tab。   
    v          与f同样;   
    
    nn        插入nnn(八进制)所代表的ASCII字符。
    --help      显示帮助
    --version   显示版本号信息

    热门推荐

  • 相关阅读:
    2018.5.5-6 GDCPC2018广东省赛 6/10 Rank12 Au
    网络流24题总结
    BZOJ4259 残缺的字符串 FFT
    [转]CodePlus 2018 3月赛 博弈论与概率统计
    Aiiage Camp Day6 J Sort
    Daily Scrum7
    Daily Scrum6
    Daily Scrum5
    Daily Scrum4
    Daily Scrum3
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5423835.html
Copyright © 2011-2022 走看看