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'm trying to do multiple commands after a condition, so for example...

[ $VAR ] || echo "Error msg" ; echo "exit"

and the Inverse

[ -z $VAR ] &&  echo "Error msg" ; echo "exit"

I know that won't work as intended, I actually knew how previously and forgot how to do this. I'm fully aware of the many alternatives, such as using if's or bracketing via () and {} . Using () will create a sub process which wouldn't exit a running script. Using {} will work, but I know a more readable alternative exists.

I have done this with a :, and it was perfect!!! I just can't remember now for the life of me, and I lost that previously written script.

If anyone knows how to write this with the :'s, I would really appreciate any help!

share|improve this question
    
After seeing this question and an earlier one of yours, I have to wonder what exactly your idea of "readability" is :P. I tend to agree with @Gilles' opinion that if; then; fi is the most readable way to do this. –  jw013 Dec 28 '11 at 1:43
    
I personally find them more verbose. I feel nicerobot offered a very readable alternative. But really maybe I like them for their writability, but they just seem so more to the point. –  TechZilla Dec 29 '11 at 19:57
add comment

3 Answers

up vote 5 down vote accepted

I tend to use something like this, which I consider nicely readable:

[ -z $VAR ] && {
  echo "Error msg"
  exit ${LINENO}
} >&2

For :, the only thing I can imagine is that you somehow defined a function but I have no idea how that would translate into a block that allows multiple commands to execute.

From man bash:

: [arguments]
        No effect; the command does nothing beyond expanding arguments and
        performing any specified redirections. A zero exit code is returned.

So the only possibility I see is if you had redefined : to be something else. I'll be interested in seeing any possibilities as to what that could be.

share|improve this answer
    
I already knew how to write this with {}'s, but I appreciate the effort. The way it was done with :'s was the most elegant and concise method I have ever seen! No functions, or anything other than some carefully placed :'s &&'s and ||'s –  TechZilla Dec 27 '11 at 5:28
    
@TechZilla Are you certain it would have allowed the same execution flow as the statement i posted? –  nicerobot Dec 27 '11 at 5:37
2  
@TechZilla I'm intrigued by what it could be but, IMO, it's not a good idea to use clever, "non-obvious" code. Scripting can already be obtuse enough without introducing uncommon syntax into the mix. Additionally, besides just being an interesting exercise or to show-off, it seems the only use for such a technique is obfuscation, regardless of how "elegant and concise" it seems to you. If you can't remember how to do it, you might also forget what it means so even if you script only for yourself, you're bound to make your life more complicated by trying to be clever. Just my 2¢ –  nicerobot Dec 27 '11 at 16:47
1  
I voted up your answer and last comment, for all the help you have offered me. You also make a very true point in your 2¢, but god forbid if I ever see that code again.... Believe me, You would respect the method if I could find it. –  TechZilla Dec 27 '11 at 20:48
1  
gave you the answer for helping, I'm going to write them like your example specified. –  TechZilla Dec 29 '11 at 3:00
show 2 more comments

The more readable alternative is

if [ "$VAR" ]; then
  echo "Error msg" 1>&2
  exit 1
fi

I know that's not what you're looking for. But it's what you should write. Using && and || here is somewhat cryptic. I have no idea what you are remembering (or misremembering), but whatever it is, if no one here can even guess what it is, it sure isn't something that qualifies as “readable”.

By the way, note the quotes around $VAR. Always put double quotes around variable substitutions, unless you know why you need to leave the quotes out and why it's safe to do so. (You don't need quotes inside [[ … ]], but you sure do inside [ … ]).

eval is a sneaky way to group commands, but using it does not qualify as readable.

Another way to produce output inside another command is to do it inside a command substitution. Again, this does not qualify as readable.

[ "$VAR" ] && exit 1$(echo "error message" >&2)

Without some form of grouping construct (whether it's braces or some other compound form like if/then/fi or while/do/done), you can't execute more than one command in the main shell process; all but one will be in a subshell.

share|improve this answer
add comment

try this [[$var]] && echo "something" || echo "something";

share|improve this answer
2  
Your not grasping the question, its not really specific to echo. Its really about executing multiple commands after an AND or OR. Specifically not using {}, () or if's. I did it before with some non-obviously placed :, can't remember how, searching like mad for that blog post. Still appreciate the help. –  TechZilla Dec 27 '11 at 6:54
    
i just tried this ` [[$var]] && echo "something" || echo "something";` here you can replace echo by any other command. –  harish.venkat Dec 27 '11 at 9:34
    
functionally that is the old standby, "AND do this if 0, OR do this if 1" Been doing those for years, this is a different problem. its more like ... "OR do this if 1, AND if you did that last OR do this also" –  TechZilla Dec 27 '11 at 20:40
    
Also using the [[]] is not the same as [], even though you can do the same and more. Its actually much simpler to use, and possibly if test is not built in the shell... But it is a bashism, and need to at least be portable enough for dash. Linux specific is ok, even 95% POSIX is ok if It can be run in dash. You can write the same functionality as [[$VAR]] like [ $VAR ] instead. –  TechZilla Dec 27 '11 at 20:45
add comment

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.