LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash file names with spaces (https://www.linuxquestions.org/questions/programming-9/bash-file-names-with-spaces-821903/)

alex1986 07-24-2010 11:03 AM

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"

unSpawn 07-24-2010 11:14 AM

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?

tonyfreeman 07-24-2010 11:44 AM

Quote:

Originally Posted by alex1986 (Post 4044000)
... 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

alex1986 07-24-2010 11:53 AM

edit: trying what tony wrote

echo "this post" > /dev/null

alex1986 07-24-2010 07:33 PM

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.

tonyfreeman 07-26-2010 09:47 PM

Quote:

Originally Posted by alex1986 (Post 4044354)
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.


All times are GMT -5. The time now is 02:12 AM.