0

I'm trying to loop a statement/output in linux. So in my ssh directory if I do ls. There is a file call hello.c which prints Hello James. What i'm trying to do is a for loop that prints it 4 times.

This is what i've tried.

for((int i=1;i<=4;i++));
do
./hello.c
echo $i
done

However nothing is printing.

2 Answers 2

1

Bash as a programming language, is dynamically typed, so there are no type declarations. So you can remove the type declaration from the line bellow:

for((int i=1;i<=4;i++));
     ^^^

i.e. change it to

for((i=1;i<=4;i++));

Additionally, are you sure that you can call the program by saying ./hello.c? This is usually the source file and is not executable.

Sign up to request clarification or add additional context in comments.

2 Comments

Your right I can't call that. There are 3 files in my directory. One of them is a.out,hello and hello.c.
./hello.c doesn't call the output but ./hello does call the output hello James
0

Using the shell command line you type (in the shell prompt)

for((i=1;i<=4;i++)); do echo "hello"; done
# outputs "hello" four times

Whatever you need to create a script, you should edit a file:

echo '#!/bin/bash' > my_script.sh
echo 'for((i=1;i<=4;i++)); do echo "hello"; done' >> my_script.sh

and then change the permissions to make it executable.

chmod +x my_script.sh
./my_script.sh

Make sure to read one of the many tutorials about shell (bash) programming, http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html is a good one.

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.