LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-13-2012, 10:41 AM   #1
ktfreak
LQ Newbie
 
Registered: Nov 2010
Posts: 14

Rep: Reputation: 0
echo commandline


Hello guys,

I want to run a command line in a script where the variable strCommandLine contains a chain of piped commands (ls, grep, awk and so on).

Here is a simplified example:
strCommandLine=$(ls -1ltr)
echo $strCommandLine

My problem now is that all printed data are merged into only one single line that looks something like that:
user1 folder1 user2 folder2 user3 folder3 (simplified)

How can I make my script printing the data out like running the command from shell prompt manually, which looks something like this:
ls -1ltr
user1 folder1
user2 folder2
user3 folder3
(simplified)

Any suggestions?

thanks
 
Old 01-13-2012, 10:53 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Hello,

You could use something like the following:
Code:
echo $strCommandLine | sed 's/ \+/\n/g'
Cheers,

Josh
 
Old 01-13-2012, 10:55 AM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
simply use quaotations

Quote:
echo "$strCommandLine"
 
Old 01-13-2012, 12:05 PM   #4
ktfreak
LQ Newbie
 
Registered: Nov 2010
Posts: 14

Original Poster
Rep: Reputation: 0
thanks

Yes, using quotations works

...But I think I need to give a little more background.

My actual command line is this:
strCommandLine=zgrep -i '] jobs: #finished' ${ArgFilenamesList[$y]}
echo "$strCommandLine"

I just used ls command to give a simple example, but ls is too simple maybe.

In my command line I'm using single quotes and a filename variable and I think this is my problem. If I now run echo "$strCommandLine" I'll get an error message saying "-i: not found [No such file or directory]"

What am I doing wrong?
 
Old 01-13-2012, 12:52 PM   #5
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
try
Quote:
grep -E
. instead of using -i and use double quotes and not single quotes.Single quotes negate the use of using variables

Last edited by cbtshare; 01-13-2012 at 12:53 PM.
 
Old 01-14-2012, 11:38 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You also need to use proper quoting when setting the variable.

Your string includes spaces and single quotes, in addition to the array variable. Fortunately in this case, a single set of double-quotes is all that's needed to handle the whole thing, as double-quotes escape quotes and vice-versa.

Code:
strCommandLine="zgrep -i '] jobs: #finished' ${ArgFilenamesList[$y]}"
It's vital in scripting to understand how the shell handles arguments and whitespace:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


Also be aware that it's not wise to actually store and run commands from variables. You should generally use functions for that.

http://mywiki.wooledge.org/BashFAQ/050

Last edited by David the H.; 01-14-2012 at 11:41 AM.
 
1 members found this post helpful.
Old 01-22-2012, 12:03 PM   #7
ktfreak
LQ Newbie
 
Registered: Nov 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks guys for helping me,

the links on mywiki.wooledge.org were very helpful, so I could find a solution myself.

And this is my solution I'm using:

strCommandLine=( zgrep -i '] jobs: #'"$strJobResult" ${ArgFilenamesList[$y]} )
( "${strCommandLine[@]}" )

The variable strJobResult contains the corresponding result to look for, e. g. "finished" or "aborted".
 
  


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
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 08:02 PM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 09:25 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 03:14 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 03:59 PM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 02:13 PM


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