LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using Dialog in Shell Script (https://www.linuxquestions.org/questions/programming-9/using-dialog-in-shell-script-365677/)

kushalkoolwal 09-21-2005 02:56 PM

Using Dialog in Shell Script
 
I am trying to write a mini-installer kind of shell script using the dialog utility. Does any one know some good tutorial from their experience which explains using dialog in detail. I googled but I did not find anything useful in particular.

david_ross 09-21-2005 03:13 PM

What do you want to know that isn't covered in the man page?

kushalkoolwal 09-21-2005 03:39 PM

Quote:

Originally posted by david_ross
What do you want to know that isn't covered in the man page?
Actually I am looking for some good examples. Some times man pages takes loads of time to figure out becase there are hardly examples.

heema 09-22-2005 03:00 AM

Here is a dialog HowTo

http://www2.linuxjournal.com/article/2460

kushalkoolwal 09-23-2005 10:44 AM

Quote:

Originally posted by heema
Here is a dialog HowTo

http://www2.linuxjournal.com/article/2460


I am trying to show a progress bar using the dialog utility's gauge option which I found from the following site pointed out by one of the forum memeber
http://www2.linuxjournal.com/article/2460


#!/bin/bash
{ for I in 10 20 30 40 50 60 70 80 90 \
80 70 60 50 40 30 20 10 0; do
echo $I
sleep 1
done
echo; } | dialog --guage "A guage demo" 6 70 0

They have given a very simple example(look above) of implementing the gauge utility. Actually in my script I am copying huge files from one disk to another which takes couple of minutes, so I wanted to show the progress bar while the copying the files using thd dialog utility.



Does any one have any experiences in doing this?

david_ross 09-23-2005 01:08 PM

All you are really doing is echoing the percentage of the copy done. If you know the number of the files being copied then you just need to make a little formula to know the percentage of the files you have copied:
percent=$(((100/$total)*$filenum))

Where $total is the number of files being copied and $filenum is the filenumber being copied. You would get a more accurate account if you did this by checking the number of bytes transfered compared to the total, although this would require more processing and depending on your situation might not be justified.

kushalkoolwal 09-23-2005 01:45 PM

Quote:

Originally posted by david_ross
All you are really doing is echoing the percentage of the copy done. If you know the number of the files being copied then you just need to make a little formula to know the percentage of the files you have copied:
percent=$(((100/$total)*$filenum))

Where $total is the number of files being copied and $filenum is the filenumber being copied. You would get a more accurate account if you did this by checking the number of bytes transfered compared to the total, although this would require more processing and depending on your situation might not be justified.


Thanks for your suggestion. Yes I have file.tar.bz2 compressed files which have various directories and files(hidden/special/configuration files). Can I some how know the total number of files contained in the tar ball. Is there any utility which will give this information.

Also how will I know which number is being copied? I am using the following command:

debian#cp deb-install.tar.bz2 /tmp/DebianInstall

thanks

david_ross 09-23-2005 02:35 PM

In that case you are only copying one file, the easiest way to do this (since it is a local copy) would be to stat the original file to find it's size, then send the copy command into the background or fork the script, then the dialog script can stat the destination file periodically to find out what size it is and update the dialog acordingly.

kushalkoolwal 09-24-2005 12:11 PM

Quote:

Originally posted by david_ross
In that case you are only copying one file, the easiest way to do this (since it is a local copy) would be to stat the original file to find it's size, then send the copy command into the background or fork the script, then the dialog script can stat the destination file periodically to find out what size it is and update the dialog acordingly.
Thank you so much for giving the idea. Can you please give me a sample code or a Pseudo kind of thing? I am not that familiar with advanced Shell scripting.


Thank you once again.

david_ross 09-24-2005 01:20 PM

This should give a pretty good demonstration but there is no error handling:
Code:

#!/bin/bash

# Set the source and destination files
srcfile="/tmp/500M.src"
dstfile="/tmp/500M.dst"

# Create a large file for testing with
echo Creating 500M.src as a source file
dd if=/dev/zero of=$srcfile bs=1048576 count=500
srcsize=`stat -c %s $srcfile`
echo Done. Copy will start in 5 seconds
sleep 5

# Remove the destination file if it exists then start to copy
dstsize=0
rm -f $dstfile
cp -f $srcfile $dstfile &

# Check the size every second until both files are the same size
while [ $dstsize -lt $srcsize ];do
 dstsize=`stat -c %s $dstfile`
 percent=$(((100*$dstsize)/$srcsize))
 echo $percent | dialog --guage "Copying $srcfile to $dstfile" 6 70 0
 sleep 1
done


kushalkoolwal 09-26-2005 11:02 AM

Thanks David. It worked!!! I have one more question which is the extension of my copying process. After I copy by tar.bz2 file to my destination, I am extracting the content of the tar bar by the following command:

tar -xjf debian-install-files.tar.bz2

How can I show the progress bar in the gauge for extraction process?

Thank you once again for all your help.

david_ross 09-27-2005 12:04 PM

I don't think you could easlily find the size of files within the archive so unless you know that before I don't think it would be very easy.

kushalkoolwal 09-27-2005 06:01 PM

Yes, the tar ball that I am trying to decompress and extract have the same number of files and directory everytime. Now is it possible with this information?

Kushal

david_ross 09-28-2005 12:22 PM

I would imagine so - just count the number of files extracted compared with the total number in the archive or if you want to do it with the file sizes just stat the files.

kushalkoolwal 09-28-2005 01:10 PM

Quote:

Originally posted by david_ross
I would imagine so - just count the number of files extracted compared with the total number in the archive or if you want to do it with the file sizes just stat the files.
Sorry for being such a pest. But I really don;t know how to implement this i..e how to count number of files extracted and how to find the total number of files in the archieve.
Can you again give me a pseudo code please?

Thank you very much..


All times are GMT -5. The time now is 07:13 PM.