1

I am developing a GUI search program. I need help to handle filenames with spaces. I just can't figure it out. Here is my code:

#!/bin/bash

USERNAME=$(whoami)

INPUT=$(zenity --entry)

updatedb --require-visibility 0 -o ~/.locate.db

FILEMATCHES=$(locate --database ~/.locate.db -i -b -c "$INPUT")
echo "$FILEMATCHES matches found"

if [ $FILEMATCHES == 0 ]; then
    echo "no file matches found"
    exit 1
fi

FOUNDFILES=($(locate --database ~/.locate.db -i -b "$INPUT"))

x=0

while [ $x -lt $FILEMATCHES ]
do
    if [ $x == 0 ]
        then
        echo -n "--list --title FILE-SELECTOR --column serialno. --column file --radiolist TRUE "${FOUNDFILES[$x]}" --width 1000 --height 600" >> /home/$USERNAME/tempzen.txt
        x=$((x+1))
    else
        echo " FALSE ${FOUNDFILES[$x]}" >> /home/$USERNAME/tempzen.txt
        x=$((x+1))
    fi
done

ZEN=$(cat /home/$USERNAME/tempzen.txt)
echo $ZEN

SELECTEDFILE=$(zenity $ZEN)
if [ $? -eq 0 ] 
then
    zenity --progress --pulsate --timeout 2
fi
echo "You have selected $SELECTEDFILE"

EXTENSION=$(file --mime-type $SELECTEDFILE)
echo $EXTENSION
if [ "$SELECTEDFILE" == "" ]
    then
        echo "You didn't select any file to open..."
        rm /home/$USERNAME/tempzen.txt
        exit 1
fi

if [ "$SELECTEDFILE" != "" ] && [ "$EXTENSION" == "$SELECTEDFILE: inode/x-empty" ] || [ "$EXTENSION" == "$SELECTEDFILE: text/plain" ] || [ "$EXTENSION" == "$SELECTEDFILE: application/xml" ]
    then
        gedit $SELECTEDFILE
elif [ "$SELECTEDFILE" != "" ] && [ "$EXTENSION" == "$SELECTEDFILE: image/jpg" ] || [ "$EXTENSION" == "$SELECTEDFILE: image/jpeg" ] || [ "$EXTENSION" == "$SELECTEDFILE: image/gif" ] || [ "$EXTENSION" == "$SELECTEDFILE: image/png" ] || [ "$EXTENSION" == "$SELECTEDFILE: image/bmp" ]
    then
        xdg-open $SELECTEDFILE
elif [ "$SELECTEDFILE" != "" ] && [ "$EXTENSION" == "$SELECTEDFILE: text/html" ]
    then
        firefox $SELECTEDFILE
elif [ "$SELECTEDFILE" != "" ] && [ "$EXTENSION" == "$SELECTEDFILE: application/pdf" ]
    then
        gnome-open $SELECTEDFILE
fi

rm /home/$USERNAME/tempzen.txt

it works perfectly for files with no saces, but for example, i have a file named very very latest (with all those spaces). So, when I input that input file name is stored as

zenity --list --title FILE-SELECTOR --column serialno. --column file --radiolist TRUE /home/jeevan/very

So only the filename up to very is stored, so the next file selection output window is also a mess:

file selector with name broken at space

I want the output to be

 zenity --list --title FILE-SELECTOR --column serialno. --column file --radiolist TRUE /home/jeevan/very very latest

And in the GUI it should be /home/jeevan/very very latest.

  • 1
    a) Use find instead of locate, or b), set IFS=$"\n", since locate prints filenames separated by newlines. – muru Jul 13 '15 at 16:19
  • Or put quotes around the filename (note that if you use double-quotes, you'll need to escape them). – saiarcot895 Jul 13 '15 at 16:30
  • @muru but when i store them in array, lets assume, there are two files, one is very very latest and the other is very.conf . then if i use IFS new line it will consider both the files as same file name right? also if i use space in IFS it will take only upto very .. how to solve this problme? – jeevanreddymandali Jul 13 '15 at 16:33
  • hi saiarcot895 i tried it but then the output in the gui is like "home/jeevan/very" :( – jeevanreddymandali Jul 13 '15 at 16:36
  • I tested double quotes and it works fine with spaces, run this file='/home/jeevan/very very latest';firefox "$file" ; – bistoco Jul 13 '15 at 17:31
1

There are two problems. The IFS for the output of locate, and the IFS for zenity.

IFS is a shell variable containing a string of characters, any of which will break a string into words. The usual value is 'space tab newline'

Both are fixed by setting IFS to a newline immediately before running locate.

export IFS='
'
FOUNDFILES=($(locate --database ~/.locate.db -i -b "$INPUT"))

The second problem is fixed by exporting this value of IFS into the environment, so that it also affects zenity. This means you also need to modify the creation of tempzen.txt to use newlines as word separators.

if [ $x == 0 ]
    then
    echo -e "--list\n--title\nFILE-SELECTOR\n--column\nserialno.\n--column\nfile\n--radiolist\nTRUE\n${FOUNDFILES[$x]}\n--width\n1000\n--height\n600" >> /home/$USERNAME/tempzen.txt
    x=$((x+1))
else
    echo -e "FALSE\n${FOUNDFILES[$x]}" >> /home/$USERNAME/tempzen.txt
    x=$((x+1))
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.