Re: Bash-script – doesn't work in a launcher

Johnny Rosenberg gurus.knugum at gmail.com
Sat Dec 31 18:12:30 UTC 2011


2011/12/31 Johnny Rosenberg <gurus.knugum at gmail.com>:
> 2011/12/31 Steve Flynn <anothermindbomb at gmail.com>:
>> On 31 December 2011 17:00, Johnny Rosenberg <gurus.knugum at gmail.com> wrote:
>>
>>> Of course the first line of the script is #! /bin/bash. Well, except a
>>> few comment lines above it, but that shouldn't matter, should it?
>>>
>>> So it's basically something like this:
>>>
>>> # Comment
>>> # Continuation of comment
>>> # Another comment
>>> # More comments
>>>
>>> #!/bin/bash
>>
>> The hash-bang line needs to be the first line in the script. Move that
>> to the top of the script, stick in a "set -x" if you want to see
>> what's happening under the covers.
>>
>>
>>
>> --
>> Steve
>
> Thanks, and also thanks to ”J” who wrote a similar reply. Yes, I moved
> it to the top and now it seems to work. It still doesn't work on my
> wife's computer though, I'm not sure why… She runs Ubuntu 10.04, so
> maybe there are some differences there, like older bash version or
> something like that. I'll take a closer look at it now.
>
> Still I wonder why it worked with the #!/bin/bash line not at the top
> when I ran it from a terminal, should it really…? Maybe because bash
> is default in a terminal?
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・ローゼンバーグ

I found why it didn't work for my wife. The script use Zenity for
simple dialogues. She has Ubuntu 10.04 with Zenity 2.30.0, I have
2.32.0 in Ubuntu 10.10. It seems like the --progress option in 2.30.0
does not support --no-cancel, so after removing the --no-cancel line
it worked perfectly on her machine too.

Here's the script, if anyone wants to know. The story behind it is
that we use the Opera web browser and she wanted to copy every image
in the Opera cache folder to another folder, but only images that are
big enough. In this case we tried with 18000 pixels and it seems to
work OK. There will still be more pictures than she wants, but the
manual job will decrease a lot anyway compared to just dragging
everything from the cache folder and its sub folders.
All the files in the Opera cache folder are named like ”oprXXXXX.tmp”
where XXXXX is a mix of upper case letters and numbers, like
”opr2DLE1.tmp”.

ImageMagick needs to be installed it. I'm using it to detect images
and their format (usually GIF, JPEG or PNG, I have seen no other
formats so far).
Zenity is used to show a progress bar and for letting the user create
or select a folder to save the files in.

So here we go:

#!/bin/bash

# Copies image files from the Opera cache folder to a place of your choice.
# An image is not copied unless its size (in pixels) is bigger than MINPIXELS.
#
# ImageMagick and Zenity (at least 2.30.0) needs to be installed.
#
# Johnny Rosenberg
# 2011-12-31

CACHEDIR="${HOME}/.opera/cache"
DESKTOPNAME="Desktop" # The name of your Desktop folder in your language.
DESTDIR="${HOME}/${DESKTOPNAME}"
TMPFILE="${HOME}/OperaTmpFiles"
MINPIXELS=18000

# Select or create a folder for saving the images.
DESTDIR=$(zenity --file-selection \
	--filename="${DESTDIR}/" \
	--title="Välj mapp" \
	--directory \
	--confirm-overwrite)

# Make a list of all tmp-files in the Opera cache folder and its sub folders.
find "${HOME}/.opera/cache/" -name "*.tmp" > "${TMPFILE}"

# Count the files that were found.
FileCount=$(cat "${TMPFILE}" | wc -l)

# Do the actual job.
cat "${TMPFILE}" | while read File; do
	Result=$(identify ${File} 2> /dev/null | head -n 1)
	if [[ $Result ]]; then
		Type=$(echo "${Result}" | awk -F " " '{print $2}')

		Size=$(echo "${Result}" | awk -F " " '{print $3}')
		Width=$(echo "${Size}" | awk -F "x" '{print $1}')
		Height=$(echo "${Size}" | awk -F "x" '{print $2}')
		let Pixels=Width*Height

		if [[ Pixels -gt MINPIXELS ]]; then
			BaseName=${File##*/} # Filnamn.filändelse
			cp "${File}" "${DESTDIR}"/"${BaseName%.*}.${Type,,}"
		fi
	fi

	let FileNo+=1
	let Percentage=100*FileNo/FileCount
	echo $Percentage
done |
zenity --progress \
	--title="Progress" \
	--text="Managing your images files" \
	--percentage=0 \
	--auto-close \
	--no-cancel # remove this line and the ” \” above if Zenity is older
than 2.32.0.

rm "${TMPFILE}"
###################### END OF CODE ####################################

Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ




More information about the ubuntu-users mailing list