LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 03: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 04:04 PM.
 
Old 08-30-2011, 04:26 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
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 04:45 PM.
 
1 members found this post helpful.
Old 08-30-2011, 04:55 PM   #3
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,899

Rep: Reputation: 68
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, 07:10 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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 07:12 PM.
 
Old 08-31-2011, 12:16 AM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,899

Rep: Reputation: 68
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, 01: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, 02:05 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
@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, 02:45 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 07:40 AM   #9
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,899

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

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
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 08:52 AM.
 
Old 08-31-2011, 09:23 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 09:34 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:38 AM
Programing Question: Reading file line by line.. then char by char in each line adriumroot Programming 8 05-30-2010 02:55 AM
[Perl] chop last char from line noir911 Programming 8 02-20-2007 11:16 PM


All times are GMT -5. The time now is 04:45 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration