Register
It is currently Fri Oct 31, 2014 10:29 am

dashes in filename interpreted as invalid option


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Tue Apr 29, 2014 1:11 pm   

Joined: Tue Apr 29, 2014 11:27 am
Posts: 3
Hi, I have a problem with my script: its aim is to find a avi file with a particular extension, and to generate a link in the tmp directory.
Here's the code:

Code:
#!/bin/bash
Path=$(ls /media/)
for media in $Path; do
        if [ $media != "SETTINGS" ]; then
                files=$(find /media/$media/ -print -type f | tr ' ' '#')
                for video in $files; do
                        extension=${video##*.}
                        if [ $extension == avi ]; then
                                video=$(echo $video | sed 's/#/\\\ /g')
                                echo "File found: "$video
                                ln -s $video /tmp/video
                                exit 0

                        fi
                done
        fi
done
exit 0



The script works fine only if the name of the file doesn't include dashes (-).

If the file contains (-) the script gives me the following error:
Code:
ln: invalid option -- '\'


seems that the script interpretes the dash like an option. How can I fix this?

Thank you in advance, and sorry for my english :D

Best Regards


Top
 Profile  
 PostPosted: Tue Apr 29, 2014 8:47 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 551
hi,

put variables inside quotes.
don't use `ls' inside scipts, and don't use a `for' loop to iterate over any command's output, because of potential spaces inside filenames/output.
instead, use globs :
Code:
#!/bin/bash
shopt -s nullglob globstar
for f in /path/**/*.avi
do
   : whatever with file "$f"
done


Top
 Profile  
 PostPosted: Thu May 01, 2014 8:40 am   

Joined: Tue Apr 29, 2014 11:27 am
Posts: 3
Watael wrote:
hi,

put variables inside quotes.
don't use `ls' inside scipts, and don't use a `for' loop to iterate over any command's output, because of potential spaces inside filenames/output.
instead, use globs :
Code:
#!/bin/bash
shopt -s nullglob globstar
for f in /path/**/*.avi
do
   : whatever with file "$f"
done


It works like a charm! Thank you so much for you quick answer. :D
As you can see, I'm a noob with bash. What's the aim of the option "globstar"?


Top
 Profile  
 PostPosted: Thu May 01, 2014 12:19 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 551
from man bash :
Quote:
globstar
If set, the pattern ** used in a pathname expansion context will match all files and
zero or more directories and subdirectories. If the pattern is followed by a /,
only directories and subdirectories match.


Top
 Profile  
 PostPosted: Sat May 03, 2014 2:41 am   

Joined: Tue Apr 29, 2014 11:27 am
Posts: 3
thank you so much for your support! :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP