-1

I'm trying to write a shell script using bash for the following problem:

Write a loop to go through three values (A B C) and displays each of these values to the screen (hint use a ‘for’ loop).

I figured out it would be something like this but I'm not sure, so any advice would be much appreciated.

For (( EXP1; EXP2; EXP3 ))
do
     command1
     command2
     command3
done
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Josh07T
  • 11
  • you want value exp1 to print or exp1 exp2 exp3 to execute ? – cutzero Apr 08 '16 at 04:07
  • Thanks for replying! I want the value T exp 1 exp2 exp3 to execute – Josh07T Apr 08 '16 at 04:08
  • @cutzeroLonGdueZBOODacrovinungh, read the problem description again; it is unambiguously a duplicate. The fact that the OP doesn't know the echo command doesn't change that. "...go through three values...display each of these values to the screen..." – Wildcard Apr 08 '16 at 04:20

2 Answers2

0

That should be as simple as:

for i in A B C; do
    echo "$i"
done
pfnuesel
  • 5,837
0
for i in Alpha Beta Charlie; do
  echo "$i"
done

You don't need eval and you don't need ls.

Alternatively, you could just print each of these directly, with a newline character after each:

printf '%s\n' Alpha Beta Charlie

Please don't use eval unless you absolutely must. (Hint: Unless you get seriously complicated, you never need it.) Using eval when you don't have to just leads to security holes.


Your initial question is very clear:

Write a loop to go through three values (A B C) and displays each of these values to the screen (hint use a ‘for’ loop).

However from your comment perhaps you want to execute some commands in a loop:

I want the value T exp 1 exp2 exp3 to execute

If you want to execute commands in a loop, just write the commands and put them in a loop:

for i in {1..3}; do
  echo Hip Hip
  echo 'HOORAY!'
done

The first line of this could just as easily be for i in A B C; do and it would do the same thing. You don't use the variable $i anywhere in your loop, but you don't have to. It will still execute three times.

Wildcard
  • 36,499