Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Let's say I have three commands that I run like this:

a && b && c

This will run b after a completes, and c after b completes.

What if I did this:

a && b

While this is running, I want to append c as a conditional command. Can it be done?

share|improve this question
    
Is this Linux or MacOSX? If in doubt tag it with the used shell. –  Bobby Feb 14 '11 at 11:17
    
I'm using OSX, but it should be the same as Linux. –  tony_sid Feb 14 '11 at 11:20
    
No, MacOSX is Unix and Linux is Linux. Additionally there's a ton of different shells out there for Linux, which might provide different solutions. –  Bobby Feb 14 '11 at 11:27
1  
@Bobby There are also different shells on OSX, but given no contrary information, bash is a good assumption. –  Daniel Beck Feb 14 '11 at 11:30
add comment

3 Answers

up vote 2 down vote accepted

I would do this:

  • a && b
  • press ctrl-Z to suspend the current pipeline
  • bg 1 to run it in background
  • wait %1 && c

wait waits for the specified job to complete, and returns its result status, so that the above behaves like a && b && c would have.

You can also define an alias or function, eg alias bgwait='bg 1 && wait %1'. Then, after pressing ctrl-Z, all you have to type is bgwait && c. Supporting multiple jobs is left as an exercise for the reader.

share|improve this answer
add comment

The result of the last operation is stored in $?. 0 means no errors occurred, anything else is a problem.

You can e.g. use it like this

if [ $? -eq 0 ]; then c; fi

Make it a function or similar in your .bash_profile and you can run it a bit more comfortably.

share|improve this answer
    
So how would I type this command if a program is already running? Would I open up another terminal? –  tony_sid Feb 15 '11 at 4:41
    
@OSXJedi you just type it in (assuming your currently running program has no input). It'll get executed after your program quits, not different than typing it afterwards, except that you don't need to wait for b before leaving the machine. –  Daniel Beck Feb 15 '11 at 5:53
add comment

For NT, you can type your new command and hit ENTER while a current command is running.

So for example,

c:\blah>dir /s

where blah is big enough that you'll have time to type while stuff scrolls onto the screen.

now all this stuff scrolls on the screen

and while it is appearing, type mem

then after dir /s is complete, mem will run.

share|improve this answer
    
The question was about conditional commands (&&). –  grawity Feb 14 '11 at 11:25
    
And it was also not about Windows. –  Bobby Feb 14 '11 at 11:29
    
Hang on. When I answered it, he had still not said what OS. So I took it as a general question and gave an answer of what'd work for NT. And secondly. if you think about it, my answer is true of "conditional commands". It's exactly it. If he runs a && b, and he wants it to act as if he'd done a && b && c, but he didn't put the && c in there. He can do just as i've said. And it works conditionally in the same manner. Does it not? –  barlop Feb 14 '11 at 11:38
2  
he may have meant conditional in terms of the command returning something true/false. but in that instance perhaps he'd see if it's true/false while it's running (which is when he plans to run the command anyway), and then he'd decide whether to run the command. I think he might've just meant running one commmand after another. –  barlop Feb 14 '11 at 12:26
    
@barlop: But the tags were also there. Though, a little bit confusing but excluding Windows. –  Bobby Feb 14 '11 at 12:49
show 2 more comments

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.