Shell Step by Step (4) —— Cron & Echo

來源:互聯網
上載者:User

標籤:shell   echo   crontab   uid   pid   

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%%\r" $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/nulldone | dialog --title "Copy" --gauge "files" 6 70    clear

效果

9.Echo輸出

功能說明: 顯示文字
語 法:

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

參數:

-n          不要在最後自動換行-e          若字串中出現以下字元,則特別加以處理,而不會將它當成一般文字輸出;  \b          刪除前一個字元;    \f          換行但游標仍舊停留在原來的位置;     \r          游標移至行首,但不換行;    \t          插入tab;   \v          與\f相同;   \nnn        插入nnn(八進位)所代表的ASCII字元;--help      顯示協助--version   顯示版本資訊

熱門推薦

  • LNMP環境搭建——Nginx篇

  • 在RHEL6.5中配置本地YUM源

  • Ubuntu下Zabbix安裝及使用問題

  • MySQL雙主熱備問題處理

Shell Step by Step (4) —— Cron & Echo

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.