LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2010, 07:22 AM   #1
AsySyah
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Rep: Reputation: 0
Linux command


how to convert this to for loop


can someone help me please

ctr=1
while test $ctr -le 20
do
tot=0
sctr=1
while test $sctr -le 4
do
echo -n "$ctr "
sctr=`expr $sctr + 1`
tot=`expr $tot + $ctr`
ctr=`expr $ctr + 1`
done
echo -n "$tot "
done

the output will show this
1 2 3 4 10 5 6 78 26 9 10 11 12 42 13 14 15 16 58 17 18 19 20 74
 
Old 09-18-2010, 07:41 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Indentation and [code] tags, please.
 
Old 09-18-2010, 08:08 AM   #3
AsySyah
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
what do you mean by code tags...?? im using vi editor
 
Old 09-18-2010, 08:14 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I'm talking about BBCode tags whaen posting code in the forums.

Without code tags:

int main()
{
printf("Hello, world!");
return 0;
}

With code tags:

Code:
int main()
{
    printf("Hello, world!");
    return 0;
}

Last edited by MTK358; 09-18-2010 at 08:17 AM.
 
Old 09-18-2010, 08:53 AM   #5
AsySyah
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
linux code tags.... in terminal
 
Old 09-18-2010, 08:55 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by AsySyah View Post
what do you mean by code tags...?? im using vi editor
Have a look here to see which are available and how to use them: BB Code List
 
Old 09-18-2010, 09:08 AM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Seems to me that we should do your homework for you.
 
Old 09-18-2010, 09:15 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by AsySyah View Post
linux code tags.... in terminal
They have nothing to do with Linux or terminals. They are bulletin board markup.
 
Old 09-18-2010, 09:20 AM   #9
AsySyah
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
yeah you right its actually my assignment... i`ve been trying it about more that 4 hours but still cant` get it i only can get the until result but cannot get the for result

Question 5
Write a shell script using for and until construct to display the following series:
1 2 3 4 10 5 6 78 26 9 10 11 12 42 13 14 15 16 58 17 18 19 20 74

Given script is using While Construct:
ctr=1
while test $ctr -le 20
do
tot=0
sctr=1
while test $sctr -le 4
do
echo -n "$ctr "
sctr=`expr $sctr + 1`
tot=`expr $tot + $ctr`
ctr=`expr $ctr + 1`
done
echo -n "$tot "
done
 
Old 09-18-2010, 09:24 AM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
1. It's against the rules for us to do your assignments for you. You can read more about bash loops.

2. Do you still not understand [code] tags? When posting, just type "[code]" right before your code, and "[/code]" right after your code. The forum will nicely format it and preserve the indentation.
 
Old 09-18-2010, 03:41 PM   #11
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

What is your problem with this task, exactly? First you need to find out the rule for the output. Then, what have you tried?
 
Old 09-19-2010, 09:01 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well this might point you in the right direction:

http://tldp.org/LDP/abs/html/loops1.html
 
  


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
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 02:14 AM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 06:30 PM
Translating windows pscp command to linux scp command help robward Linux - General 2 01-17-2008 06:02 AM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
unix command to linux command leonidas Linux - General 1 09-10-2004 12:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 AM.

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