Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.linux.misc > #14353 > unrolled thread
| Started by | Charlie Gibbs <cgibbs@kltpzyxm.invalid> |
|---|---|
| First post | 2015-04-06 18:35 +0000 |
| Last post | 2015-04-06 23:19 +0000 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.os.linux.misc
Bash if statements fail under xfce launcher? Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2015-04-06 18:35 +0000
Re: Bash if statements fail under xfce launcher? Richard Kettlewell <rjk@greenend.org.uk> - 2015-04-06 20:06 +0100
Re: Bash if statements fail under xfce launcher? Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2015-04-06 23:19 +0000
Re: Bash if statements fail under xfce launcher? Richard Kettlewell <rjk@greenend.org.uk> - 2015-04-07 09:08 +0100
Re: Bash if statements fail under xfce launcher? dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2015-04-06 20:20 +0100
Re: Bash if statements fail under xfce launcher? Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2015-04-06 23:19 +0000
| From | Charlie Gibbs <cgibbs@kltpzyxm.invalid> |
|---|---|
| Date | 2015-04-06 18:35 +0000 |
| Subject | Bash if statements fail under xfce launcher? |
| Message-ID | <mfujm30hc5@news4.newsguy.com> |
I've written a shell script that uses zenity to ask the user for
a parameter, so it can be run from an icon in Xfce rather than
the command line. The if statements don't seem to be properly
evaluated when the script is run through the GUI, although the
script works properly when run from the command line. Here's a
stripped-down example that illustrates the problem:
x=$(zenity --list --text="Select an option" --radiolist --height 250 \
--column "" --column "Option" TRUE default FALSE test)
echo "zenity returned /$x/" >foo
if [ "$x" == "default" ]; then
echo "Default option was selected" >>foo
elif [ "$x" == "test" ]; then
echo "Test option was selected" >>foo
else
echo "This should never happen!" >>foo
fi
When I run it from the command line and select "default", foo contains
zenity returned /default/
Default option was selected
When I run it from the command line and select "test", foo contains
zenity returned /test/
Test option was selected
I've created an icon on the desktop to launch the script. When
I double-click the icon the selection box comes up, but whichever
selection I make, foo contains the selection followed by
This should never happen!
Zenity is setting $x to the desired value, but the if and elif
statements don't recognize it. Why should the if statements fail
when run from the GUI, when they work when run from the command line?
I'm running Debian Jessie - here's the output of uname -a:
Linux dragon 3.14-2-amd64 #1 SMP Debian 3.14.13-2 (2014-07-24) x86_64 GNU/Linux
The properties for my icon run the script from my home directory, which is
also specified as the working directory. The "Use startup notification"
and "Run in terminal" checkboxes are not checked.
Are there any gotchas that I have to watch out for?
--
/~\ cgibbs@kltpzyxm.invalid (Charlie Gibbs)
\ / I'm really at ac.dekanfrus if you read it the right way.
X Top-posted messages will probably be ignored. See RFC1855.
/ \ HTML will DEFINITELY be ignored. Join the ASCII ribbon campaign!
[toc] | [next] | [standalone]
| From | Richard Kettlewell <rjk@greenend.org.uk> |
|---|---|
| Date | 2015-04-06 20:06 +0100 |
| Message-ID | <wwvegnx6pqh.fsf@l1AntVDjLrnP7Td3DQJ8ynzIq3lJMueXf87AxnpFoA.invalid> |
| In reply to | #14353 |
Charlie Gibbs <cgibbs@kltpzyxm.invalid> writes: > I've written a shell script that uses zenity to ask the user for > a parameter, so it can be run from an icon in Xfce rather than > the command line. The if statements don't seem to be properly > evaluated when the script is run through the GUI, although the > script works properly when run from the command line. Here's a > stripped-down example that illustrates the problem: > > x=$(zenity --list --text="Select an option" --radiolist --height 250 \ > --column "" --column "Option" TRUE default FALSE test) > echo "zenity returned /$x/" >foo > if [ "$x" == "default" ]; then > echo "Default option was selected" >>foo > elif [ "$x" == "test" ]; then > echo "Test option was selected" >>foo > else > echo "This should never happen!" >>foo > fi There are two problems with your script. 1. There appears to be no #! line, meaning you have no control over which shell runs it (and evidently your GUI and login shell are making different decisions). 2. You are using == instead of = as the comparison operator. The former is a bashism. It will not work in a non-Bash shell. With an explicit #! line mentioning /bin/bash, it becomes safe to use ==. Without it, or with a #! line mentioning /bin/sh, you must restrict yourself to =. -- http://www.greenend.org.uk/rjk/
[toc] | [prev] | [next] | [standalone]
| From | Charlie Gibbs <cgibbs@kltpzyxm.invalid> |
|---|---|
| Date | 2015-04-06 23:19 +0000 |
| Message-ID | <mfv49k11b7d@news3.newsguy.com> |
| In reply to | #14354 |
On 2015-04-06, Richard Kettlewell <rjk@greenend.org.uk> wrote: > Charlie Gibbs <cgibbs@kltpzyxm.invalid> writes: > >> I've written a shell script that uses zenity to ask the user for >> a parameter, so it can be run from an icon in Xfce rather than >> the command line. The if statements don't seem to be properly >> evaluated when the script is run through the GUI, although the >> script works properly when run from the command line. Here's a >> stripped-down example that illustrates the problem: >> >> x=$(zenity --list --text="Select an option" --radiolist --height 250 \ >> --column "" --column "Option" TRUE default FALSE test) >> echo "zenity returned /$x/" >foo >> if [ "$x" == "default" ]; then >> echo "Default option was selected" >>foo >> elif [ "$x" == "test" ]; then >> echo "Test option was selected" >>foo >> else >> echo "This should never happen!" >>foo >> fi > > There are two problems with your script. > > 1. There appears to be no #! line, meaning you have no control over > which shell runs it (and evidently your GUI and login shell are > making different decisions). > > 2. You are using == instead of = as the comparison operator. The former > is a bashism. It will not work in a non-Bash shell. > > With an explicit #! line mentioning /bin/bash, it becomes safe to use > ==. Without it, or with a #! line mentioning /bin/sh, you must restrict > yourself to =. Thanks for the pointers. I tried adding a #!/bin/bash line and it worked. It also works if I replace == with =. What are the pros and cons of the two options? The #!/bin/bash option will not work on a system that doesn't have bash, or which has it somewhere other than /bin. This isn't a big thing because this script is only going to be used on the one machine, but I like to develop portable habits. By the same token, what about = vs. ==? I've read that == is a bashism, and that = is accepted as a synonym. This sounds like the more portable option, unless there are other considerations. -- /~\ cgibbs@kltpzyxm.invalid (Charlie Gibbs) \ / I'm really at ac.dekanfrus if you read it the right way. X Top-posted messages will probably be ignored. See RFC1855. / \ HTML will DEFINITELY be ignored. Join the ASCII ribbon campaign!
[toc] | [prev] | [next] | [standalone]
| From | Richard Kettlewell <rjk@greenend.org.uk> |
|---|---|
| Date | 2015-04-07 09:08 +0100 |
| Message-ID | <wwv6198742u.fsf@l1AntVDjLrnP7Td3DQJ8ynzIq3lJMueXf87AxnpFoA.invalid> |
| In reply to | #14358 |
Charlie Gibbs <cgibbs@kltpzyxm.invalid> writes: > What are the pros and cons of the two options? The #!/bin/bash option > will not work on a system that doesn't have bash, or which has it somewhere > other than /bin. This isn't a big thing because this script is only going > to be used on the one machine, but I like to develop portable habits. Use #!/bin/sh unless you actually need Bash (some of its exensions are more useful than alternative spellings of ‘=’). > By the same token, what about = vs. ==? I've read that == is a bashism, > and that = is accepted as a synonym. This sounds like the more portable > option, unless there are other considerations. There’s no good reason to use ==. -- http://www.greenend.org.uk/rjk/
[toc] | [prev] | [next] | [standalone]
| From | dave.gma+news002@googlemail.com.invalid (Dave Sines) |
|---|---|
| Date | 2015-04-06 20:20 +0100 |
| Message-ID | <9t2bvbxj52.ln2@perseus.wenlock-data.co.uk> |
| In reply to | #14353 |
Charlie Gibbs <cgibbs@kltpzyxm.invalid> wrote: > I've written a shell script that uses zenity to ask the user for > a parameter, so it can be run from an icon in Xfce rather than > the command line. The if statements don't seem to be properly > evaluated when the script is run through the GUI, although the > script works properly when run from the command line. Here's a > stripped-down example that illustrates the problem: > > x=$(zenity --list --text="Select an option" --radiolist --height 250 \ > --column "" --column "Option" TRUE default FALSE test) > echo "zenity returned /$x/" >foo > if [ "$x" == "default" ]; then > echo "Default option was selected" >>foo > elif [ "$x" == "test" ]; then > echo "Test option was selected" >>foo > else > echo "This should never happen!" >>foo > fi > Zenity is setting $x to the desired value, but the if and elif > statements don't recognize it. Why should the if statements fail > when run from the GUI, when they work when run from the command line? > > I'm running Debian Jessie - here's the output of uname -a: > > Linux dragon 3.14-2-amd64 #1 SMP Debian 3.14.13-2 (2014-07-24) x86_64 GNU/Linux > > The properties for my icon run the script from my home directory, which is > also specified as the working directory. The "Use startup notification" > and "Run in terminal" checkboxes are not checked. > > Are there any gotchas that I have to watch out for? It sounds as though the GUI is using sh (dash on Debian) when the script contains bash-specific extensions. Use single equals signs with the '[' command: if [ "$x" = default ]; then ... if [ "$x" = test ]; then Or replace the entire if...fi block with a case statement: case $x in default) echo "Default option was selected" >>foo ;; test) echo "Test option was selected" >>foo ;; *) echo "This should never happen!" >>foo ;; esac
[toc] | [prev] | [next] | [standalone]
| From | Charlie Gibbs <cgibbs@kltpzyxm.invalid> |
|---|---|
| Date | 2015-04-06 23:19 +0000 |
| Message-ID | <mfv49k01b7d@news3.newsguy.com> |
| In reply to | #14355 |
On 2015-04-06, Dave Sines <dave.gma+news002@googlemail.com.invalid> wrote: > Charlie Gibbs <cgibbs@kltpzyxm.invalid> wrote: > >> I've written a shell script that uses zenity to ask the user for >> a parameter, so it can be run from an icon in Xfce rather than >> the command line. The if statements don't seem to be properly >> evaluated when the script is run through the GUI, although the >> script works properly when run from the command line. Here's a >> stripped-down example that illustrates the problem: >> >> x=$(zenity --list --text="Select an option" --radiolist --height 250 \ >> --column "" --column "Option" TRUE default FALSE test) >> echo "zenity returned /$x/" >foo >> if [ "$x" == "default" ]; then >> echo "Default option was selected" >>foo >> elif [ "$x" == "test" ]; then >> echo "Test option was selected" >>foo >> else >> echo "This should never happen!" >>foo >> fi > >> Zenity is setting $x to the desired value, but the if and elif >> statements don't recognize it. Why should the if statements fail >> when run from the GUI, when they work when run from the command line? >> >> I'm running Debian Jessie - here's the output of uname -a: >> >> Linux dragon 3.14-2-amd64 #1 SMP Debian 3.14.13-2 (2014-07-24) x86_64 GNU/Linux >> >> The properties for my icon run the script from my home directory, which is >> also specified as the working directory. The "Use startup notification" >> and "Run in terminal" checkboxes are not checked. >> >> Are there any gotchas that I have to watch out for? > > It sounds as though the GUI is using sh (dash on Debian) when the script > contains bash-specific extensions. > > Use single equals signs with the '[' command: > > if [ "$x" = default ]; then > ... > if [ "$x" = test ]; then > > Or replace the entire if...fi block with a case statement: > > case $x in > default) echo "Default option was selected" >>foo ;; > test) echo "Test option was selected" >>foo ;; > *) echo "This should never happen!" >>foo ;; > esac Thanks for the tips. That did the trick. Beats the hell out of Microsoft support... :-) -- /~\ cgibbs@kltpzyxm.invalid (Charlie Gibbs) \ / I'm really at ac.dekanfrus if you read it the right way. X Top-posted messages will probably be ignored. See RFC1855. / \ HTML will DEFINITELY be ignored. Join the ASCII ribbon campaign!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.os.linux.misc
csiph-web