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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-25-2006, 10:09 PM
|
#1
|
|
LQ Newbie
Registered: Jul 2006
Posts: 2
Rep:
|
bash script question
hi,
sorry if this is in the wrong place, I was having trouble figuring out where the appropriate forum for this post is.
I am toying around with bash scripts. all I am trying to do at the moment is write a script that sucesfully iterates through every file in a given directory. for now I am just echo-ing the files to the command line. I know there is no need for a bash script to do this, but I am just trying to make sure the loop does what I want it to.
which it doesn't. if the files in the directory have spaces in them, they get output on separate lines. worse yet, if the input directroy has spaces in its path, it tries to find files as if they were separate directories.
for instance:
Quote:
shakermaker@localhost ~ $ ls test
four.txt one two.txt three.txt
shakermaker@localhost ~ $ scripts/iterate.x test
test
four.txt
one\
two.txt
three.txt
|
Quote:
shakermaker@localhost ~ $ scripts/iterate.x /media/torrents/2\ Many\ DJ\'s/
/media/torrents/2 Many DJ's/
ls: /media/torrents/2: No such file or directory
ls: Many: No such file or directory
ls: DJ's/: No such file or directory
|
my script is as follows:
Code:
#!/bin/bash
#iterates through a directory, printing each file to the console
MIN_ARGS=1
E_OPTERROR=65
if [ $# -lt $MIN_ARGS ]
then
echo "Usage: `basename $0` path"
exit $E_OPTERROR
fi
FILEPATH=$1
echo $FILEPATH
for i in `ls -b $FILEPATH`;
do
echo $i
done
any help?
|
|
|
|
08-25-2006, 11:15 PM
|
#2
|
|
Member
Registered: Sep 2003
Location: USA
Distribution: Ubuntu Linux
Posts: 103
Rep:
|
It seems that your problem deals with having spaces in filenames.
The solution to this is simply an extra step which I have come to think of and use over the past some years.
Put simply, you could output your statements (literally: echo "echo \"$filename\"" >> temp_file) and have that output to a temporary file. This temporary file should look like:
Code:
echo "file1.txt"
echo "file with space.txt"
echo "file3.txt"
Now have this temporary file output and executed by bash:
Code:
cat temp_file | bash
This takes care of spaces, by treating each line as an entity rather than each word. This is yet another reason to not have spaces in filenames. I only use the aforementioned technique for my music, which must have spaces.
|
|
|
|
08-26-2006, 02:59 AM
|
#3
|
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
The return value of ls is split at whitespace to form a list of separate values, and the loop will iterate over the values. One solution is to use "ls -1" instead, which will print a single filename on each line, and tell Bash to consider the newline character as the only field sepearator. You can do this by specifying "IFS=$'\n'" before the loop. But note that setting IFS might sometimes have unwanted effects in other situations.
Another option would be to pipe the output of "ls -1" into a loop like
Code:
while read; do echo "$REPLY"; done
Furthermore, for a similar reason, you should use
to print the filenames, as an argument with whitespace will be considered as a list of separate arguments otherwise.
|
|
|
|
08-26-2006, 01:52 PM
|
#4
|
|
LQ Newbie
Registered: Jul 2006
Posts: 2
Original Poster
Rep:
|
thanks so much! this helps a lot, I used your suggestions to succesfully work around the issues I mentioned earlier.
now I'm having a problem with filenames that have appostrophes in them. ls -b doesn't escape them apparently. how do people generally deal with spaces and special characters in filenames? there must be a common way of working around this, because I would imagine it is a common problem.
I really need to just fix my filenames (curse you windows! teaching such bad habits), but that doesn't solve the problem, it just prevents it from occurring. a good script will deal with any possible file name.
|
|
|
|
08-26-2006, 02:54 PM
|
#5
|
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
I'd suggest not to use the -b switch at all, but to use double quotes around the filename wherever it appears. Then you should be fine.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:43 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|