LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-30-2011, 02:44 PM   #1
jim_geek
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Rep: Reputation: Disabled
New Line Char Is not working properly in linux


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.

Last edited by jim_geek; 08-30-2011 at 03:04 PM.
 
Old 08-30-2011, 03:26 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by jim_geek View Post
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
But that isn't the program you used to generate the output you quoted, because it wouldn't generate that output.

You should have done a copy/paste to post the exact program you tested.

In the above, you have
j=$j;
The correct program (and your tested program) must have
j=$i;

In the above, you have
echo $j \n;
but that is neither correct nor what you tested.

You need to suppress the newline, which is done with
echo -n $j
But then you need to add another newline once per outer loop by adding another instruction somewhere.

Quote:
the logic of program is correct
Your program is wrong. I don't know what you think you mean by its "logic" is correct.

Your title "New Line Char Is not working properly in linux" and your claim that "the logic of program is correct" both indicate you prefer to blame the tool rather than look for your mistake. You'll find that attitude is not very effective when asking for help and even less effective when trying to learn enough that you won't need to ask for help.

BTW, I don't know shell scripting myself. For example, I don't know the significance of the ; on some of your lines. Those seem to make no difference if I test your script with/without them in bash. Of course, I also can't tell whether you are using bash. So in answering your post, I didn't use any scripting knowledge, just basic programming logic (independent of language) plus a quick glance at the output from something you should also look at:
Code:
man echo
Possibly someone who actually knows shell scripting will have something constructive to add. I answered because no one else had answered and because the error was obvious enough to answer without shell scripting knowledge.

Last edited by johnsfine; 08-30-2011 at 03:45 PM.
 
1 members found this post helpful.
Old 08-30-2011, 03:55 PM   #3
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Yes, echo -n $j doesn't output newline, and for outputting newline simply echo would do.

On semicolons: they are optional if you have them in the end of the line.

Actually, echo is a shell builtin, so correct place to look for its description is probably man bash (assuming you use bash as your shell, which is very likely), You can find out what shell you use by echo $SHELL.
 
Old 08-30-2011, 06:10 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Good bash tutorials
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

The first line of your script should really specify which shell you want eg
Code:
#!/usr/bin/bash
Yes, it looks like a comment, but it's not really... read the links.

For debugging I recommend
Code:
set -xv
as the 2nd line of your script.

Last edited by chrism01; 08-30-2011 at 06:12 PM.
 
Old 08-30-2011, 11:16 PM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Code:
#! /usr/bin/bash
looks like it uses Solaris path to bash. On most GNU/Linux distributions
Code:
#! /bin/bash
has to be used. It is the path to the script interpreter, so check that the bash executable exists at the path you put.
 
Old 08-31-2011, 12:03 AM   #6
jim_geek
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
sorry johnsfine i am just newbie in the linux and I am not finding any problem with tools.problem is with me only.now that problem is solved thanks every one for helping.you are the people who makes new generation of linux users thank you.chrism01,johnsfine thank you.
 
Old 08-31-2011, 01:05 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@raskin; I did say eg .... for exactly that kind of reason; don't have Linux in front of me.
BTW, there should be no spaces on that invocation line ...
 
Old 08-31-2011, 01:45 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
now that problem is solved thanks every one for helping
And are you going to share the solution so other beginners may get the benefit?

Plus you will need to mark as SOLVED so people know that there is a solution available.
 
Old 08-31-2011, 06:40 AM   #9
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
chrism01: on Linux you can put a space after #!, actually
 
Old 08-31-2011, 07:48 AM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by grail View Post
And are you going to share the solution so other beginners may get the benefit?
Other beginners who find this thread might want to look at the solution posted in the other thread where jim_geek asked this same question.

Last code block in the post at:
http://www.linuxquestions.org/questi...0/#post4457379

A beginner would be better off looking at the individual suggestions in this thread and figuring out how that leads to a full solution. But if you want to just look at a full solution, it is in the other thread.

Last edited by johnsfine; 08-31-2011 at 07:52 AM.
 
Old 08-31-2011, 08:23 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well I am hoping the beginner doesn't look at harry's solution as it does not deliver and needlessly uses extra variables.

Also, MTK's prints an extra line at the beginning.

This is why I was also interested in the OP's solution as they may have corrected these issues
 
Old 08-31-2011, 08:34 AM   #12
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 raskin View Post
Code:
#! /usr/bin/bash
looks like it uses Solaris path to bash. On most GNU/Linux distributions
Code:
#! /bin/bash
has to be used. It is the path to the script interpreter, so check that the bash executable exists at the path you put.
Or even better:

Code:
/usr/bin/env bash
It will automatically find bash if it's in your PATH variable.
 
  


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
Linux Static IP Configured | Not working properly AsadMoeen Linux - Networking 3 10-30-2010 04:38 AM
Programing Question: Reading file line by line.. then char by char in each line adriumroot Programming 8 05-30-2010 01:55 AM
[Perl] chop last char from line noir911 Programming 8 02-20-2007 10:16 PM

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

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