LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-18-2004, 05:33 PM   #1
socket9001
LQ Newbie
 
Registered: Dec 2003
Location: Israel
Distribution: Slackware
Posts: 21

Rep: Reputation: 15
bash script


here's a problem i've encouterd when trying to make a simple bash script...

Code:
for i in `ls` 
do
       echo $i
done
if there will be a file name with a spaces in it in the current directory, the file name will break at the position of the space into a new file name....

for example, lets say i have three files in a directory with this names:
1. "file one"
2. "file2"
3. "file number 3"

this will be the output of the loop above:

file
one
file2
file
number
3

as you can see the file name breaks where there are spaces...
does any one have a solution?
 
Old 02-18-2004, 06:02 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 60
How about this....

Code:
for i in ls "*"
do
       echo $i
done
 
Old 02-19-2004, 03:29 AM   #3
socket9001
LQ Newbie
 
Registered: Dec 2003
Location: Israel
Distribution: Slackware
Posts: 21

Original Poster
Rep: Reputation: 15
same result...

#!/bin/sh

for i in `ls *`
do
echo $i
done

still braeks the file names....

if i use `ls "*"` i get a syntax error...
 
Old 02-19-2004, 04:16 AM   #4
codeape
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 62

Rep: Reputation: 15
# ls -l | cut -c 57-

This prints the directory listing from the 57th character of each line. You may have to tweak this on your machine to get the correct character (56?).

This gets you one empty line at the top though that you may or may not need to strip. I don't know what your ultimate goal with this is...

You could then pipe the output to an array or a file and do all sorts of cool things with it
 
Old 02-19-2004, 09:32 AM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 60
Quote:
for i in `ls *`
That's not what I had.
and this isn't either....
Quote:
if i use `ls "*"` i get a syntax error...
This is what I had for i in ls "*"
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script Linh Programming 4 04-21-2004 06:19 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-06-2003 12:58 AM
bash script brian0918 Programming 7 06-12-2003 07:06 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 06:34 AM

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

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