FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 12-31-2011, 04:00 PM
Johnny Rosenberg
 
Default Bash-script – doesn't work in a launcher

I made a bash script that I want to run by clicking an icon on my
upper panel in Ubuntu 10.10. The script runs perfectly in a terminal,
but it doesn't do much if I start it by clicking the icon in my panel.
I added the icon by creating a launcher the usual way.

So I changed the launcher from ”program” to ”program in terminal” to
be able to see what happens, if anything. I then found that it
produces error messages at every ”let” and ”[[”, which I guess are
bash specific, so it seems like sh is trying to run my script, not
bash.

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 script, including some bash specific things like ”let a=b*c” and
”if [[ something ]]; then”.

As I said, the script works perfectly from a terminal, no problems at all.
When launching it from a launcher, it seems like all bash specific
commands suddenly are unknown to the system, why is that? I guess I am
missing something, but what?


Kind regards

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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-31-2011, 04:11 PM
J
 
Default Bash-script – doesn't work in a launcher

On Sat, Dec 31, 2011 at 12:00, Johnny Rosenberg <gurus.knugum@gmail.com> wrote:
> I made a bash script that I want to run by clicking an icon on my
> upper panel in Ubuntu 10.10. The script runs perfectly in a terminal,
> but it doesn't do much if I start it by clicking the icon in my panel.
> I added the icon by creating a launcher the usual way.
>
> So I changed the launcher from ”program” to ”program in terminal” to
> be able to see what happens, if anything. I then found that it
> produces error messages at every ”let” and ”[[”, which I guess are
> bash specific, so it seems like sh is trying to run my script, not
> bash.
>
> 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 script, including some bash specific things like ”let a=b*c” and
> ”if [[ something ]]; then”.

the #! needs to be the very first line in the script. If it's placed
anywhere else, it'll be ignored.

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-31-2011, 04:18 PM
Steve Flynn
 
Default Bash-script – doesn't work in a launcher

On 31 December 2011 17:00, Johnny Rosenberg <gurus.knugum@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

When one person suffers from a delusion it is insanity. When many
people suffer from a delusion it is called religion.

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-31-2011, 04:39 PM
Johnny Rosenberg
 
Default Bash-script – doesn't work in a launcher

2011/12/31 Steve Flynn <anothermindbomb@gmail.com>:
> On 31 December 2011 17:00, Johnny Rosenberg <gurus.knugum@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
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-31-2011, 05:08 PM
Leo Noordhuizen
 
Default Bash-script – doesn't work in a launcher

Johnny,
You answered your own question I think.
Leo

On Sat, Dec 31, 2011 at 6:39 PM, Johnny Rosenberg <gurus.knugum@gmail.com> wrote:


2011/12/31 Steve Flynn <anothermindbomb@gmail.com>:

> On 31 December 2011 17:00, Johnny Rosenberg <gurus.knugum@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

ジョニー・*ーゼンバーグ



--

ubuntu-users mailing list

ubuntu-users@lists.ubuntu.com

Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-31-2011, 05:12 PM
Johnny Rosenberg
 
Default Bash-script – doesn't work in a launcher

2011/12/31 Johnny Rosenberg <gurus.knugum@gmail.com>:
> 2011/12/31 Steve Flynn <anothermindbomb@gmail.com>:
>> On 31 December 2011 17:00, Johnny Rosenberg <gurus.knugum@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
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-31-2011, 05:15 PM
Johnny Rosenberg
 
Default Bash-script – doesn't work in a launcher

2011/12/31 Leo Noordhuizen <leo.noordhuizen@gmail.com>:
> Johnny,
>
> You answered your own question I think.
>
> Leo

Yes, I really did… :P

I tested it right after I posted my question and found out before
reading any replies, so I was somewhat embarrassed there for a while,
since I should obviously tested it BEFORE posting, then there wouldn't
be any need for posting at all…


Kind regards

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

>
> On Sat, Dec 31, 2011 at 6:39 PM, Johnny Rosenberg <gurus.knugum@gmail.com>
> wrote:
>>
>> 2011/12/31 Steve Flynn <anothermindbomb@gmail.com>:
>> > On 31 December 2011 17:00, Johnny Rosenberg <gurus.knugum@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
>> ジョニー・*ーゼンバーグ
>>
>> --
>> ubuntu-users mailing list
>> ubuntu-users@lists.ubuntu.com
>> Modify settings or unsubscribe at:
>> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
>
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-31-2011, 05:17 PM
Avi Greenbury
 
Default Bash-script – doesn't work in a launcher

Johnny Rosenberg wrote:
> 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?

Exactly. When you don't specify an interpreter to use, generally the
current shell is used, which is probably bash.

A line beginning #! anywhere other than the top of the script is simply
a comment; there's nothing parsing it to work out what to do.


--
Avi

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-31-2011, 05:21 PM
Johnny Rosenberg
 
Default Bash-script – doesn't work in a launcher

2011/12/31 Avi Greenbury <lists@avi.co>:
> Johnny Rosenberg wrote:
>> 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?
>
> Exactly. When you don't specify an interpreter to use, generally the
> current shell is used, which is probably bash.
>
> A line beginning #! anywhere other than the top of the script is simply
> a comment; there's nothing parsing it to work out what to do.

That's very good to know. I thought it would recognise it wherever it
was, as long as nothing was done above it, but now I know better.
Thanks.


Kind regards

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

>
>
> --
> Avi

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 12-31-2011, 07:25 PM
J
 
Default Bash-script – doesn't work in a launcher

On Sat, Dec 31, 2011 at 13:21, Johnny Rosenberg <gurus.knugum@gmail.com> wrote:
> 2011/12/31 Avi Greenbury <lists@avi.co>:
>> Johnny Rosenberg wrote:
>>> 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?
>>
>> Exactly. When you don't specify an interpreter to use, generally the
>> current shell is used, which is probably bash.
>>
>> A line beginning #! anywhere other than the top of the script is simply
>> a comment; there's nothing parsing it to work out what to do.
>
> That's very good to know. I thought it would recognise it wherever it
> was, as long as nothing was done above it, but now I know better.
> Thanks.

Also, something to keep in mind for cases where you aren't calling a
specific shell using the #! line in a script...

check to see if /bin/sh and /bin/bash are symlinks to other shells.
For example:

bladernr@minime:~$ ll /bin/sh
lrwxrwxrwx 1 root root 4 2011-05-06 00:45 /bin/sh -> dash*
bladernr@minime:~$ ll /bin/bash
lrwxrwxrwx 1 root root 609232 2011-05-18 13:55 /bin/bash*

dash and sh behave differently from each other and from bash, so if
you write a script for bash specifically, but run using /bin/sh or
/bin/dash, you may experience some confusing, unintended
consequences...

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 09:38 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org