-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
  • 66,199
  • 35
  • 114
  • 250
Josh07T
  • 11
  • 2
  • 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,702
  • 8
  • 35
  • 60
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
  • 35,316
  • 26
  • 130
  • 258
  • Why the downvotes? – pfnuesel Apr 08 '16 at 04:25
  • 1
    @pfnuesel, I would say strategic downvotes by someone giving a very strange answer. Your answer is perfectly fine; I started out the same but expanded it a little further. – Wildcard Apr 08 '16 at 04:27
  • no strategic, the exp must be executed. – cutzero Apr 08 '16 at 04:30
  • omg i give up... – cutzero Apr 08 '16 at 04:36
  • @cutzeroLonGdueZBOODacrovinungh, see edited answer. Happy now? – Wildcard Apr 08 '16 at 04:36
  • no he wants exp to be executed, he cleary says "I want the value T exp 1 exp2 exp3 to execute " – cutzero Apr 08 '16 at 04:38
  • @cutzeroLonGdueZBOODacrovinungh, well he accepted my answer. If you have a different question perhaps you should ask it. There's no such thing as "executing an expression"; only a command. You could just say `expr1;expr2;expr3` to execute each of these *as a command* — but what on earth would it mean to "execute an expression"? This isn't Lisp. – Wildcard Apr 08 '16 at 04:41
  • "eval takes its arguments, concatenates them separated by spaces, and executes the resulting string as Bash code" you can say what you want... – cutzero Apr 08 '16 at 04:47
  • *sigh* @cutzero this is covered elsewhere. **See [Why should eval be avoided in Bash, and what should I use instead?](http://stackoverflow.com/q/17529220/5419599)** – Wildcard Apr 08 '16 at 05:04
  • i upvote since the edit – cutzero Apr 08 '16 at 05:10