Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I prefer to do my sh commands in shorthand, And I prefer to be in POSIX. Truthfully, if the script can run on Dash, that is good enough. Which means using the test command [, not the extended [[.

I have an example from an autostart script, which I'm not sure about.

if which dbus-launch >/dev/null && test -z "$DBUS_SESSION_BUS_ADDRESS"; then
   eval `dbus-launch --sh-syntax --exit-with-session`
fi

So my guess is that if dbus-launch and $DBUS_SESSION_BUS_ADDRESS is empty, then do the eval. How do I write this in shorthand? I appreciate any help on this subject.

share|improve this question
1  
To confirm, what is the command flow that you're actually wanting? I don't fully understand from your question. –  Chris Down Dec 14 '11 at 23:19
    
I want the same command flow as before, just written in dash compatible shorthand. So instead of if ... then ... fi, I much prefer [ ... ] && ... for example "test -z "$DBUS_SESSION_BUS_ADDRESS" is comparable to [ -z "$DBUS_SESSION_BUS_ADDRESS" ]. I just don't know how to do a long nested example like this. –  TechZilla Dec 14 '11 at 23:31

1 Answer 1

up vote 2 down vote accepted

The snippet you posted is POSIX-legal and should run fine on dash as is. I don't understand the point of expunging ifs and replacing them with one-liners, as it doesn't actually accomplish much besides occasionally making code harder to read. Nevertheless, in this case, since it's just a single line, you can simply delete the if, ; then, and fi and replace the ; then with a &&, i.e.

which dbus-launch >/dev/null && test -z "$DBUS_SESSION_BUS_ADDRESS" && 
eval `dbus-launch --sh-syntax --exit-with-session`

should be functionally equivalent to the snippet in the question. Also, if this is what you mean by "shorthand", I don't think it's a universal or standard term and it doesn't save too many characters of typing. Converting an if compound command to a list would be a more accurate description.

share|improve this answer
    
truthfully, i find the && and || easiest to read, its only really a problem for people who don't write shell. so I also could write it like this, command -v dbus-launch > /dev/null && [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && eval `dbus-launch --sh-syntax --exit-with-session` but is the eval really necessary? How is it different from command -v dbus-launch > /dev/null && [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && dbus-launch --sh-syntax --exit-with-session –  TechZilla Dec 16 '11 at 1:25
    
There seems to be some confusion here. While && and || with { }can replace certain if constructs with care and some awkwardness in syntax, this has very little to do with eval. The dbus-launch --sh-syntax will print environment variable assignments to its standard output, e.g. to set DBUS_SESSION_BUS_ADDRESS, and the eval is what actually runs those assignments in the current shell. Dropping the eval will mean those assignments never run, which means programs you run won't know where to find the dbus session bus, which means things will probably break. –  jw013 Dec 16 '11 at 2:28
    
O got it, but whats the difference between backticks and eval? like this command -v dbus-launch > /dev/null && [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && `dbus-launch --sh-syntax --exit-with-session` –  TechZilla Dec 16 '11 at 4:53
    
Command substitution happens after variable assignments. Without eval you'll get a message like DBUS_SESSION_BUS_ADDRESS=whatever_its_contents_should_be: no such command. You need eval to interpret the command substitution output the way the shell would, or else the shell will just attempt to execute the output directly. –  jw013 Dec 16 '11 at 5:03
    
Thank you for helping, I really appreciate it! –  TechZilla Dec 16 '11 at 5:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.