Shell graphical progress bar

Source: Internet
Author: User

In shell script writing applications, graphical interfaces are sometimes required. For example, the default CP copy file mode is silent, and the copy progress and percentage cannot be seen. The dialog tool provides a graphical interface for shell. It provides a variety of graphical interfaces for shell scripts. Today we will introduce the progress bar graphical function provided by dialog.

The dialog command can be executed independently. The dialog -- title "copy" -- gauge "Files" 6 70 10

Note: title indicates the title of the graphic progress bar. gauge indicates the body content, the height of the progress bar is 6, the width is 70, and the display progress is 10%.

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

In the following case, the number of source files is counted, and the percentage of copied files is calculated accordingly. The progress is displayed in shell. The script has two parameters: the first parameter is the source file path, and the second parameter is the target path. If your application cases are different, you can use them with slight modifications.

  1. #! /Bin/bash
  2. # Description: A shell script to copy parameter1 to parameter2 and display a progress bar
  3. # Author: Jacob
  4. # Version: 0.1 Beta
  5. # Read the parameter for copy, $1 is source Dir and $2 is destination dir
  6. Dir = $1 /*
  7. Des = $2
  8. # Test the destination dirctory whether exists
  9. [-D $ des] & Echo "dir exist" & Exit 1
  10. # Create the destination dirctory
  11. Mkdir $ des
  12. # Set counter, it will auto increase to the number of source file
  13. I = 0
  14. # Count the number of source file
  15. N = 'echo $1/* | WC-W'
  16. For file in 'echo $ dir'
  17. Do
  18. # Calculate progress
  19. Percent = $(100 * (++ I)/n ))
  20. Cat <EOF
  21. Xxx
  22. $ Percent
  23. Copying File $ file...
  24. Xxx
  25. EOF
  26. /Bin/CP-r $ File $ des &>/dev/null
  27. Done | dialog -- title "copy" -- gauge "Files" 6 70
  28. Clear

Effect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.