6

I just can not get it right.

Script takes two arguments, target and command. Valid targets are specified within an array. If target is 'all', script should iterate through all targets.

#!/bin/bash
# recur.sh

targets=('aaa' 'bbb' 'ccc' 'ddd')

if [ "$1" == "all" ] ; then
    for i in $targets ; do
        echo $2" --> "$i
        ./$0 $i $2
    done
    exit 0
fi

echo "Target "$1" just received command '"$2"'"

exit 0

I expect the following output:

$ recur all boggle
boggle --> aaa
Target aaa just received command 'boggle'
boggle --> bbb
Target bbb just received command 'boggle'
boggle --> ccc
Target ccc just received command 'boggle'
boggle --> ddd
Target ddd just received command 'boggle'

But the script exits at the first iteration:

$ recur all boggle
boggle --> aaa
Target aaa just received command 'boggle'
4
  • What is your question?
    – cuonglm
    Jul 3, 2014 at 14:36
  • Just added expected result. I can not achieve that. Jul 3, 2014 at 14:40
  • Why is recursion needed here? Is your actual script doing more than just echoing the different "targets"? What is the output you're getting now?
    – Joseph R.
    Jul 3, 2014 at 14:48
  • I run java processes in screens, I push commands to those java consoles, also restart, kill, stop them. I did not want to put on the whole script, it would distract people from the problem I need help with. In other words the echo "Target "$1" just received command '"$2"'" part is complex. Jul 3, 2014 at 14:51

3 Answers 3

7

The problem isn't in the recursion, it's in the looping over the items. If you try this as above, you don't get what you expect:

$ targets=(aaa bbb ccc ddd)
$ for i in $targets; do echo $i; done
aaa

To loop over the array, you need to generate a list of the items in the array, as per,eg, this link:

So you have

#!/bin/bash
# recur.sh

targets=('aaa' 'bbb' 'ccc' 'ddd')

if [ "$1" == "all" ] ; then
    for i in ${targets[@]}; do
        echo $2" --> "$i
        ./$0 $i $2
    done
    exit 0
fi

echo "Target "$1" just received command '"$2"'"

exit 0
2
  • @stéphane-chazelas , feel free to add comments to this answer, or suggest other improvements to the OPs code, but the bash script I pasted here was intentionally the minimal change to the OPs script to make it work, and while the aside about other shells is a perfectly good point, the answer isn't incorrect without it. Jul 3, 2014 at 16:57
  • However, the answer is incorrect with bugs and bad practises without the fixes I suggested. I agree those errors were in the question in the first place, and it's your answer, you do whatever you want with it, but my view would be to make sure answers always contain correct code if possible, as people may end up copy-pasting code from it (while they wouldn't do that from the question) Jul 3, 2014 at 18:41
2

Others already pointed out, the problem with the syntax used when accessing a list. But that's not the only problem with your script. You are also using variables without quoting. When using $variablename without quotes, you are going to get unexpected results, if that variable contains any characters, which could be interpreted by the shell.

Here is how I would change your script to avoid such problems:

#!/bin/bash
# recur.sh

targets=('aaa' 'bbb' 'ccc' 'ddd')

if [ "$1" == "all" ] ; then
    for i in "${targets[@]}"; do
        echo "$2 --> $i"
        "$0" "$i" "$2"
    done
    exit 0
fi

echo "Target $1 just received command '$2'"

exit 0

You should make it a habit to always put usage of variable inside double-quotes, unless you have a very good reason not to.

0
1

The problem, is that you have not looped over all elements in array, just one element, because you used $targets instead of ${targets[@]}.

Try this fix:

targets=('aaa' 'bbb' 'ccc' 'ddd')

if [ "$1" == "all" ] ; then
    for i in ${targets[@]} ; do
        echo $2" --> "$i
        echo "Target "$i" just received command '"$2"'"
    done
fi

exit 0

You don't need recursion here.

0

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.