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.
- #! /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
Effect