0

with tcsh, I want to print "Hello" for each x,y, z, e, and f. But with the following script, it only prints x. Can someone tell me how to print "Hello" also for y, z, e, and f?

#! /bin/tcsh -f
set arr=(x y z e f)

set j = 0
foreach i ($arr)
echo $i
   while ($j < 5)

     echo "Hello"
     @ j++
    end
end 

The result is:
x
Hello
Hello
Hello
Hello
Hello
y
z
e
f

1
  • you are only initializing the 'j' iterator variable to 0 one time. You need. to reinitialize the. variable to 0 inside the foreach loop. Commented May 16, 2022 at 21:21

1 Answer 1

1

Move the initialization of the 'j' variable inside the foreach loop:

#! /bin/tcsh -f
set arr=(x y z e f)

foreach i ($arr)
    echo $i
    set j = 0
    while ($j < 5)
            echo "Hello"
            @ j++
    end
end 

Output:

$ ./s.sh
x
Hello
Hello
Hello
Hello
Hello
y
Hello
Hello
Hello
Hello
Hello
z
Hello
Hello
Hello
Hello
Hello
e
Hello
Hello
Hello
Hello
Hello
f
Hello
Hello
Hello
Hello
Hello
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.