A progress bar (gauge box) — dialog Linux

來源:互聯網
上載者:User
  • You can create a progress bar (progress indicator) when copying/moving files or making backups using the
    gauge box.
  • It displays a meter along the bottom of the box. The meter indicates the percentage. New percentages are read from standard input, one integer per line. The meter is updated to reflect each new percentage.
  • If the standard input reads the string "START_BAR", then the first line following is taken as an integer percentage, then subsequent lines up to another "START_BAR" are used for a new prompt. The gauge exits when EOF is reached on the standard input.
  • The syntax is as follows:
echo percentage | dialog --gauge "text" height width percentecho "10" | dialog --gauge "Please wait" 10 70 0echo "50" | dialog --gauge "Please wait" 10 70 0echo "100" | dialog --gauge "Please wait" 10 70 0
  • However, you need to use the
    while or
    for loop to show 0 to 100% progress. In this example, the
    for loop is used to display progress:
for i in $(seq 0 10 100) ; do sleep 1; echo $i | dialog --gauge "Please wait" 10 70 0; done
Example

Create a shell script dvdcopy.sh:

#!/bin/bash# dvdcopy.sh - A sample shell script to display a progress bar# set counter to 0 counter=0(# set infinite while loopwhile :docat <<EOFXXX$counterDisk copy /dev/dvd to /home/data ( $counter%):XXXEOF# increase counter by 10(( counter+=10 ))[ $counter -eq 100 ] && break# delay it a specified amount of time i.e 1 secsleep 1done) |dialog --title "File Copy" --gauge "Please wait" 7 70 0

Save and close the file. Run it as follows:

chmod +x dvdcopy.sh./dvdcopy.sh

Sample outputs:

A sample progress bar (gauge box)

File Copy Progress Bar With Dialog
  • Create a shell script called pcp.sh:
#!/bin/bash# pcp.sh: A shell script to copy /bin/* and /etc/* files#         Display a progress bar while copying files.  # * Based upon Greg's (GreyCat's) GPLd wiki example. *# --------------------------------------------------------# Create an array of all files in /etc and /bin directoryDIRS=(/bin/* /etc/*) # Destination directoryDEST="/tmp/test.$$" # Create $DEST if does not exits[ ! -d $DEST ] && mkdir -p $DEST ## Show a progress bar# ---------------------------------# Redirect dialog commands input using substitution#dialog --title "Copy file" --gauge "Copying file..." 10 75 < <(   # Get total number of files in array   n=${#DIRS[*]};     # set counter - it will increase every-time a file is copied to $DEST   i=0    #   # Start the for loop    #   # read each file from $DIRS array    # $f has filename    for f in "${DIRS[@]}"   do      # calculate progress      PCT=$(( 100*(++i)/n ))       # update dialog box cat <<EOFXXX$PCTCopying file "$f"...XXXEOF  # copy file $f to $DEST   /bin/cp $f ${DEST} &>/dev/null   done) # just delete $DEST directory/bin/rm -rf $DEST

Save and close the file. Run it as follows:

chmod +x pcp.sh./pcp.sh
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.