A progress bar (gauge box) -- dialog Linux

来源:互联网 发布:python sin cos 编辑:程序博客网 时间:2024/05/21 21:40
  • You can create a progress bar (progress indicator) when copying/moving files or making backups using thegauge 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
原创粉丝点击