LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-30-2007, 10:08 AM   #1
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Rep: Reputation: 0
Bash : add cr to stdout


Hi,
Maybe it is a simple question for you guys but I a newby in bash script programming...
Here it is :

When in a script I do a
Code:
z=$(ls -al 2>&1)
echo $z
var=$?
the output for z give the good result but with no new line or carriage return.

Is there a simple way to output the result of the command and display it with carriage return for each new line?
By the way, I want to retrieve stdrr so that's why I am using 2>&1 and var=$?.

Thanks
 
Old 08-30-2007, 10:20 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Two things:
- The answer is: surround your $(...) construct with double quotes.
- The return status stored in var is that of the echo command. You probably want to have the var= line just under the ls -al line; however, I'm not sure you'll get the return status of ls -al as this command is burried in a subshell...

Yves.
 
Old 08-30-2007, 10:39 AM   #3
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theYinYeti View Post
Two things:
- The answer is: surround your $(...) construct with double quotes.
- The return status stored in var is that of the echo command. You probably want to have the var= line just under the ls -al line; however, I'm not sure you'll get the return status of ls -al as this command is burried in a subshell...

Yves.
Thanks for replying me!
Well I have tested your solution but that's not seems to work...
Ok, here is the code
Code:
        z="$(ls -al 2>&1)"
        var=$?
        echo $z
The output is :
Code:
total 12 drwxr-x--- 2 romain romain 4096 Aug 30 11:22 . drwx------ 10 romain romain 4096 Aug 30 11:22 .. -rwxr-x--- 1 romain romain 1303 Aug 30 11:22 test.sh
What I want is :
Code:
total 12
drwxr-x---   2 romain romain 4096 Aug 30 11:22 .
drwx------  10 romain romain 4096 Aug 30 11:22 ..
-rwxr-x---   1 romain romain 1303 Aug 30 11:22 test.sh
As you can see, the output of ls does not add any carriage return or new line code..

I will check for the stderr stuff later.

Thanks
 
Old 08-30-2007, 10:58 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Yes I forgot that you ALSO have to put double quotes around the variable name as an argument to echo, which gives:
Code:
z="$(ls -al 2>&1)"
var=$?
echo "$z"
Yves.
 
Old 08-30-2007, 11:05 AM   #5
romainp
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
SOLVED : Bash : add cr to stdout

Perfect! That's working.
By the way, I have tested the stderr code and it seems to catch successfully the error code (0 or 1) when the ls function is executed.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
demultiplexing bash stdout stream pobbz Linux - Software 3 06-21-2007 09:14 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
add user bash script noir911 Programming 4 08-13-2005 08:24 AM
bash + manipulate stdout to console dave bean Programming 1 02-21-2005 06:44 AM
How do I add a function to Bash? Gag Halfrunt Linux - Newbie 5 11-25-2004 12:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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