Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

So the script is:

#!/bin/bash

for line in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
   if [[ "$line" = "" || "$line" = " " ]]; then
      exit
   else
      unzip "$line" -d "temp"
   fi
done

It works well and unzips the file. But if the file is named with a space (eg: Leon - 1994.zip) it will not work and the $line will be

1st time: /home/citybong/Downloads/Leon

2nd time: -

3rd time: 1994.zip

Edit: I fount a link http://g-scripts.sourceforge.net/faq.php for the question: "My script can't handle multiple files/files with spaces" but i am kinda newbie to bash scripting and i can't understand their solutions

share|improve this question

3 Answers 3

up vote 6 down vote accepted

IFS is by default set to space, that's why your string is being divided into spaces. Just set it to the newline with the following code:

IFS_BAK=$IFS
IFS="
"

for line in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
    whatever $line
done

IFS=$IFS_BAK
share|improve this answer
1  
Thank you, that solved it ;) –  Leon Vitanos Jan 14 '13 at 14:59

You don't need to mess with IFS:

echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | while read filename; do
    unzip "$filename" -d temp
done

I had a similar problem and it turns out that echo preserves newlines in double quotes, but without quotes changes them to spaces.

share|improve this answer
    
Please add more details to your answer –  Anwar Shah Feb 18 '13 at 9:27

Put single quotes around the variable name, like so:

#!/bin/bash

for line in '$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'; do
   if [[ "$line" = "" || "$line" = " " ]]; then
      exit
   else
      unzip "$line" -d "temp"
   fi
done
share|improve this answer
1  
$line becomes $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS –  Leon Vitanos Jan 14 '13 at 14:33
    
Try double quotes then - I may have the wrong type listed in my response. –  Clive van Hilten Jan 14 '13 at 14:45
    
Xmm with double quotes the $line takes the fullpath of the file but with a new empty line, so the unzipping fails. line = $(echo $line | tr '\n') didn't help. Hakermanias solution solves the problem ;) –  Leon Vitanos Jan 14 '13 at 15:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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