LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2010, 11:03 AM   #1
alex1986
Member
 
Registered: Nov 2005
Location: Ottawa, Canada
Distribution: none yet
Posts: 96

Rep: Reputation: 19
Bash file names with spaces


I have a laptop that I am in through SSH. The laptop does not have an Xwindow system so I am using the program fbi to open an image on my laptop screen from my SSH connection:

fbi -T 8 picture.jpg #this opens the image on the laptops tty8 terminal

I've found that making a for loop does not work with files that contain a space in the name. Something to due with a bug that they call a "feature" that stops the first variable at the first whitespace.

Using a "while" loop is not exactly what i require either seeing as I want to be able to view each image in the directory on screen and tag it accordingly, before it jumps off to the next image, and I'm not sure how to add a pause to a while loop.

Thoughts?

TL;DR
How do I make a Bash script and loop Variables handle files like "files that contain spaces.jpg"
 
Old 07-24-2010, 11:14 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
By terminating with a null char as in 'find /path/to/pictures -type f -iname \*.jpg -print0 | xargs -iX -0 fbi -T 8 'X';' or proper use of quoting?

Last edited by unSpawn; 07-24-2010 at 11:16 AM. Reason: //Less is more. Sometimes.
 
Old 07-24-2010, 11:44 AM   #3
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
Quote:
Originally Posted by alex1986 View Post
... Something to due with a bug that they call a "feature" that stops the first variable at the first whitespace...
It really is NOT a bug or a feature ... it's a default behavior common to ALL command prompts that are not using auto-completion ... and yes this applies to the bloody command line on friggin windows ;-)

Anyway ...

When I find filenames with spaces I use the IFS="
" environment variable to get around this. For example:

Code:
#!/bin/bash

IFS="
"

for i in $(ls -1)
do
   echo "filename = $i"
done
... will output:

Code:
bash test.sh 
Filename =  a file name that starts with a space.txt
Filename = another filename with spaces.txt
Filename = bloody file with space.txt
Filename = test.sh
-- Tony
 
Old 07-24-2010, 11:53 AM   #4
alex1986
Member
 
Registered: Nov 2005
Location: Ottawa, Canada
Distribution: none yet
Posts: 96

Original Poster
Rep: Reputation: 19
edit: trying what tony wrote

echo "this post" > /dev/null

Last edited by alex1986; 07-24-2010 at 11:55 AM.
 
Old 07-24-2010, 07:33 PM   #5
alex1986
Member
 
Registered: Nov 2005
Location: Ottawa, Canada
Distribution: none yet
Posts: 96

Original Poster
Rep: Reputation: 19
Found what I was looking for:

http://hacktux.com/bash/file

this is a cut out of what I did to solve it:


#!/bin/bash
function pause(){
read -p "press enter to continue"
}
for FILE in *.jpg; do
echo "open $FILE"
pause
fbi -T 8 "$FILE"
echo "send switch signal and enter tagging code here"
done
echo game over

earlier I was trying to do some sort of ALL=`ls -1 *.jpg and trying to get the for loop to read the $ALL variable 1 line at a time. DO NOT DO THAT IN THIS CASE.

thread solved.
 
1 members found this post helpful.
Old 07-26-2010, 09:47 PM   #6
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
Quote:
Originally Posted by alex1986 View Post
for FILE in *.jpg
Wow ... this is the best way. I learned something new here as well. As long as you have quotes around $FILE down in your for loop, then spaces in file names don't matter. My suggestion about redefining IFS is not as good as this.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - replace all spaces in file, folder names babag Programming 24 04-20-2008 12:17 AM
how to handle file names with spaces in them bahadur Programming 3 03-23-2005 05:15 AM
du or wc and file names with spaces bramadams Slackware 2 01-27-2005 11:43 AM
Spaces in file names JohnKFT Slackware 3 11-09-2004 03:44 PM
Bash crashes ? File names with () and spaces Danodare Slackware 1 02-27-2004 02:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 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
Open Source Consulting | Domain Registration