Be a Supporter!

Basic Unix Shell Scripting Question

  • 276 Views
  • 4 Replies
New Topic Respond to this Topic
Al6200
Al6200
  • Member since: Dec. 3, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Basic Unix Shell Scripting Question 2011-01-09 13:10:57 Reply

Okay, so I just started doing Unix shell scripting (with Bash).

Let's say I'm in the terminal and I type the command ls. I'll get something like:

myFile1
myFile2
myFile3

The files are printed vertically, with each on its own line.

But if I'm in my shell script and I have the command echo `ls` what I get is:

myFile1 myFile2 myFile3

The files are printed horizontally and wrap onto new lines.

Without writing some weird custom code that replaces spaces with newlines or something along those lines, is there a way to call commands in a shell script and display the result vertically?

Thanks.


"The mountain is a quarry of rock, the trees are a forest of timber, the rivers are water in the dam, the wind is wind-in-the-sails"

-Martin Heidegger

BBS Signature
kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to Basic Unix Shell Scripting Question 2011-01-09 14:05:19 Reply

ls -l to output the files vertically, then use cut or awk to get the first column

Al6200
Al6200
  • Member since: Dec. 3, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Basic Unix Shell Scripting Question 2011-01-09 14:21:46 Reply

Thanks, but the actual command I'm working with is grep. For grep -l means to just list the filenames. I just mentioned ls because it has the same problem.


"The mountain is a quarry of rock, the trees are a forest of timber, the rivers are water in the dam, the wind is wind-in-the-sails"

-Martin Heidegger

BBS Signature
kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to Basic Unix Shell Scripting Question 2011-01-09 14:30:49 Reply

grep -l | sed -e 's/ /\n/' ?

Al6200
Al6200
  • Member since: Dec. 3, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Basic Unix Shell Scripting Question 2011-01-09 14:59:34 Reply

Your logic is right but for some reason pipes don't seem to work inside shell scripts. Even less doesn't work.

My code stores $result as the grep string and then calls,

result="$result | sed s/' '/'\n'/"

echo `$result`

grep . * | sed s/' '/'\n'/ works fine in the console, but when I call it in the shell script it complains with "|, no such file or directory. sed no such file or directory, ..."

For some reason it doesn't recognize the pipe.


"The mountain is a quarry of rock, the trees are a forest of timber, the rivers are water in the dam, the wind is wind-in-the-sails"

-Martin Heidegger

BBS Signature