LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Closed Thread
  Search this Thread
Old 08-30-2011, 03:08 PM   #1
jim_geek
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Rep: Reputation: Disabled
Problem with avoiding new line


hello i am new to linux(mandriva-konsole) and just starting the shell scripting.i am getting the following output for
1
2
1
3
2
1
4
3
2
1

but i want the following output
1
12
123
1234

the program is

for ((i=0;i < 5;i++))
do
j=$j;
while [ $j -ne 0 ]
do
echo $j \n;
j=$(($j-1));
done
done

the logic of program is correct but only the output has different pattern for display.i suspect problem with echo $j \n; its not giving output for whole loop in one line.because i should get only four line op with pattern given above.please reply quickly.thanks in advance.
 
Old 08-30-2011, 04:32 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
First of all, use code tags.

Quote:
Originally Posted by jim_geek View Post
for ((i=0;i < 5;i++))
do
j=$j;
while [ $j -ne 0 ]
do
echo $j \n;
j=$(($j-1));
done
done
"echo $j \n" clearly won't work. Bash doesn't interpret backslash escape sequences like C does. The are only interpreted that way in a single-quotes string preceeded by a "$" character, like this:

Code:
$'\n'
Also, echo automatically adds a newline, so it isn't necessary in the first place.

Finally, What's th point of the "j=$j" line? I think you mean "j=$i". This is the corrented code:

Code:
for (( i=0; i<5; i++))
do
    for (( j=1; j<=i; j++ ))
    do
        echo -n $j
    done
    echo
done
 
1 members found this post helpful.
Old 08-30-2011, 04:38 PM   #3
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
Another way closer to your original code:

Code:
j=1
for ((i=1;i < 5;i++))
do
 a=1
 while [ "$a" -le "$j" ]
 do
  echo -n $a;
  a=$(($a+1));
 done
 j=$(($j+1));
done

Last edited by harry edwards; 08-30-2011 at 04:40 PM.
 
1 members found this post helpful.
Old 08-30-2011, 06:44 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.


http://www.linuxquestions.org/questi...-linux-900297/
 
  


Closed Thread



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
Avoiding rootfstype=jffs2? littlebigman Linux - Software 0 05-18-2011 08:35 AM
Avoiding to do a # rm -r /etc as root diegoandresalvarez Linux - General 24 02-22-2009 10:30 AM
ndiswrapper 'unknown line at line...' problem sparrott Linux - Hardware 1 04-16-2006 09:14 AM
Avoiding pitfalls... tracedroute Slackware - Installation 6 05-09-2004 04:54 PM
Disconnecting a directory, and avoiding this problem. wallaba Linux - Newbie 1 12-23-2002 01:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:09 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