LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-10-2018, 01:52 PM   #1
beginner101
LQ Newbie
 
Registered: Feb 2018
Posts: 1

Rep: Reputation: Disabled
what is the output of this shell script


value=0
for i in 1 2
do
for j in 1 2 3 4 5 do
value=`expr $i + $j`
echo -n "$value "
done echo done
 
Old 02-10-2018, 01:55 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
will I get an 'A+' and a cookie if I tell you?
put it into a bash script, run it then tell me.
 
Old 02-10-2018, 03:40 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
that's a whole new variation on the "do my homework" theme.
 
Old 02-18-2018, 12:40 AM   #4
treemouse
Member
 
Registered: Jan 2017
Posts: 30

Rep: Reputation: Disabled
Quote:
Originally Posted by beginner101 View Post
value=0
for i in 1 2
do
for j in 1 2 3 4 5 do
value=`expr $i + $j`
echo -n "$value "
done echo done
Give someone a fish, you'll feed them for a day. Teach someone to fish, you'll feed them for a lifetime.

What you have here is code with two loops. The first loop (the outer loop) is for the variable i. It will run twice, once with i=1 and once with i=2. The second loop (the inner loop) is for the variable j. Each time the inner loop is run, it will run five times, for j=1 j=2 j=3 j=4 and j=5.

The inner loop is inside the outer loop, so it will run five times for the first run of the outer loop, and five times again for the second run of the outer loop. So the code that's inside the inner loop will run ten times total. Each time it runs, it will add the current values of i and j, and print the result.

The first five times will be during the first run of the outer loop:
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 1, j = 4
i = 1, j = 5
Then the outer loop will increment i to 2, and run again, leading to another 5 runs of the inner loop
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 2, j = 4
i = 2, j = 5

Now that I've explained the code to you, do you think you can figure out what output it's going to give you?

Let me know if you have any questions! I'm not going to give you the answer, but I am happy to help you learn!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
process the output of one shell script in another one samasara Linux - Newbie 25 10-11-2013 07:09 PM
Using shell command output as input in shell script - how to do? EnderX Linux - Newbie 2 06-30-2010 12:46 PM
shell script output to file drum2jc Linux - Software 1 05-30-2006 06:54 PM
Newbie No output from shell script nl1at Programming 2 04-04-2006 09:33 AM
shell script output redirection goral.j Programming 3 01-27-2005 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration