LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-06-2005, 01:55 AM   #1
chii-chan
Member
 
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357

Rep: Reputation: 30
Problem with combining bash with zenity --progress


I made this BASH script to read manga/comic directly from .zip/.rar file:

Code:
#!/bin/bash
#iManga-reader-advanced i.e. with error checking. 
#You can use the normal one without error checking.
#You must have gnome (zenity), gqview, unrar and unzip
#programs installed on your system.
#chii-chan 2005

var1=gqview
var2=`ps h -C gqview | awk '{print $5}'`
var3=~/.iManga-reader-tmp

#check if another gqview is running
if [ "$var1" = "$var2" ]; then
	number=`ps h -C $var1 | awk '{print $1}'`
	kill -9 $number
	
	else
		echo "gqview not running"
fi

mkdir $var3

if [ $? == 0 ]; then
	echo "Success"
else
	echo "Directory exists!"
	#remove the temporary folder
	rm -rf $var3
	#It is important to remove and recreate the directory
	#since there are possibly some files inside the directory
	#from script crash.
	mkdir $var3
fi

#check for file types

if [ `echo "$1" | cut -d. -f2- | grep zip` ]; then
	echo "I find zip file!"
	unzip -t "$1"
	if [ $? == 0 ]; then
		(
			unzip "$1" -d $var3
			echo "100"; sleep 0.5
		) |
		zenity --progress --pulsate --auto-close --text="Extracting..." --title="Please wait for a while."
		echo "success"
		
	else
		echo "I'm broken"
		zenity --error --text="Sorry. I can't open this file." --title="Error" --width=200 --height=100
		#remove the temporary folder
		rm -rf $var3
		exit 1
	fi

elif [ `echo "$1" | cut -d. -f2- | grep rar` ]; then
	echo "I find rar file!"
	unrar t "$1"
	if [ $? == 0 ]; then
		(
			unrar x "$1" -d $var3
			echo "100"; sleep 0.5
		) |
		zenity --progress --pulsate --auto-close --text="Extracting..." --title="Please wait for a while."
		echo "success"
	
	else
		echo "I'm broken"
		zenity --error --text="Sorry. I can't open this file." --title="Error" --width=200 --height=100
		#remove the temporary folder
		rm -rf $var3
		exit 1
	fi

else
	echo "I'm broken badly!!!"
	zenity --error --text="Sorry. I can't recognize this file. May be you need to rename the file to something that I can recognize. :)" --title="Error" --width=200 --height=100
	#remove the temporary folder
	rm -rf $var3
	exit 1
fi

#open files with gqview
gqview $var3

#remove the temporary folder
rm -rf $var3

exit 0
Which means with this script I just need to right click in Nautilus and open it automatically. But my problem is with this part:

Code:
if [ `echo "$1" | cut -d. -f2- | grep zip` ]; then
	echo "I find zip file!"
	unzip -t "$1"
	if [ $? == 0 ]; then
		(
			unzip "$1" -d $var3
			echo "100"; sleep 0.5
		) |
		zenity --progress --pulsate --auto-close --text="Extracting..." --title="Please wait for a while."
		echo "success"
		
	else
		echo "I'm broken"
		zenity --error --text="Sorry. I can't open this file." --title="Error" --width=200 --height=100
		#remove the temporary folder
		rm -rf $var3
		exit 1
	fi
...
...
...
fi
I want to do something like this:

Code:
if [ `echo "$1" | cut -d. -f2- | grep zip` ]; then
	echo "I find zip file!"
	(
		unzip "$1" -d $var3
		if [ $? == 0 ]; then
			echo "Success"
			echo "100"; sleep 0.5
		else
			echo "I'm broken"
			echo "100"; sleep 0.5
			zenity --error --text="Sorry. I can't open this file." --title="Error" --width=200 --height=100
			#remove the temporary folder
			rm -rf $var3
			exit 1
		fi
	) | 
	zenity --progress --pulsate --auto-close --text="Extracting..." --title="Please wait for a while."
	echo "success"
...
...
...
fi
My problems are:

1) The 'exit 1' in between ( ) won't work. It will not exit the script. Instead it will continue running 'gqview' even if the outcome is error from .zip file extraction.

2) '( ) | zenity --progress' contruct (in the documentation) is the only way to send "100" percent to zenity --progress so that I can let it autoclose with '--auto-close'.

3) Even if I put 'exit 1' inside there. The script will return 0 since 'zenity --progress' is successfully executed.

4) Having 'unzip -t "$1" solved the problem. But the progress does not show from 'unzip -t "$1", but from 'unzip "$1" -d $var3' instead. So the progress bar is not really correct.

What I want to do with the part are:

1) I want to run 'unzip'.

2) If it returns "0" then, then close 'zenity --progress'.

3) Then continue to the end of the script.

4) Improves the script (for sure).

I'm using zenity version 1.6. Any help would be (really) appreciated.
 
Old 05-06-2005, 02:57 AM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 61
I did not know that sleep can work with decimal values and can wait less
than one second, thanks for sharing that

When you put exec lines between ( and ), it executes the lines in
a subshell (say a shell inside the main program) so exit 1 will
exit the subshell not the main program

You could store the pid at the beginning of the script like :
Code:
mypid=$$
then replace ' exit 1 ' with ' kill $mypid '
 
Old 05-06-2005, 03:12 AM   #3
chii-chan
Member
 
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357

Original Poster
Rep: Reputation: 30
Actually I did:

'killall $0' to solve it, but when a script run, in 'ps aux' it will just put it as 'bash' instead of script name. Thanks for that nice suggestion! Never though of $$.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PROBLEM: pygtk progress bar bendeco13 Programming 3 04-05-2005 02:54 PM
Dialog GAUGE - How to display progress of compile? broken pipe problem? laikos Programming 3 07-03-2004 07:08 PM
Write-combining thiagorobert Linux - Software 2 05-10-2004 03:50 PM
bash: How can I create a progress indicator? apeekaboo Programming 5 06-27-2003 10:41 AM
file progress in xterm titlebar with bash brian0918 Linux - Newbie 3 06-11-2003 06:04 PM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration