Ubuntu: how to handle file names with spaces


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  

see, filename only upto vey is stored, so the next file selection output windows is also a mess:

enter image description here

please help me to get that full name in there :(

I want the output as

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

and in that gui, it should be, /home/jeevan/very very latest.


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  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »