0

I'd like to repeatedly iterate over, alternate between, and/or cycle through multiple commands; in a kind of pattern or loop. The desired end result could be thought of as not totally unlike watch.

I've been experimenting with variations of this basic formula; but something is missing, wrong, or otherwise:

for x in {1..60}; 
do 
    <first command> && 
    sleep 1 && 
    <second command>; 
done

I think it's just reiterating (doing) the <first command> && sixty times, then all subsequent operations just one time, like normal. I've tried a few different things; like using {braces}, 'quotes', "quotes", and so on; in an attempt to group commands together, without success.

  • 1
    The && may be a problem, since it requires a successful exit status. Do you want it to finish the first command, then if the first command was successful, do a sleep 1, or have it sleep 1 while the first command completes in the background regardless of exit codes? – agc Mar 1 '17 at 22:20
0

This works for me:

    for x in {1..60}; 
    do echo "1" &&
    sleep 1 &&
    echo "3"; 
    done

This also works for me:

    for x in {1..60}; 
    do echo "1";
    sleep 1;
    echo "3"; 
    done

What are your other two commands?

0

One simple way to do this is to put each command into an element of an array:

commandlist=('echo "hello"' 'echo "howdy"')

And then iterate through it:

for x in {1..10}; do 
    index=$((x%2))  # % takes the modulus (or remainder) of integer division
    ${foo[$index]}
    sleep 1
done

${foo[0]} expands to echo "hello", which is then executed.

0

This can also be done in bash or zsh in emacs mode via control+o:

$ set -o emacs
$ echo a
a
$ echo b
b
$ echo c
c

Then use history navigation to get to the echo a (or appropriate) command, then mash control+o a bunch of times. The commands from where you navigated to the most recent one should be repeated and looped over again for each control+o.

0

It's alright, I think I figured it out.

I think I just need to add another && sleep 1 between the <second command> and ; done.

Something like:

for x in {1..60}; 
do 
    <first command> && 
    sleep 1 && 
    <second command> &&
    sleep 1; 
done
  • That should have no effect on whether the second command completes, unless perhaps the two commands have some undisclosed resource conflict. – agc Mar 1 '17 at 22:24
  • @agc "..should have no effect on whether the second command completes.." This is an accurate statement. I don't think that was ever an issue, though. – tjt263 Mar 1 '17 at 22:56
  • Why don't you just do a set -e at the beginning & dispense with all those && ? – user218374 Mar 12 '17 at 12:14
  • @RakeshSharma I'm not familiar with what that is, or how it works. I'm just using what I've learned thus far, the way I understand it. Although, I don't really find the use of operators, like &&, ||, and ; to be a hassle or anything; I kind of like the visual cues. – tjt263 Mar 12 '17 at 12:39
  • @RakeshSharma But, by all means, feel free to elaborate on your idea. – tjt263 Mar 12 '17 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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