BashScripts.org
http://bashscripts.org/forum/

dashes in filename interpreted as invalid option
http://bashscripts.org/forum/viewtopic.php?f=16&t=1815
Page 1 of 1

Author:  Khryme [ Tue Apr 29, 2014 1:11 pm ]
Post subject:  dashes in filename interpreted as invalid option

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

Author:  Watael [ Tue Apr 29, 2014 8:47 pm ]
Post subject:  Re: dashes in filename interpreted as invalid option

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

Author:  Khryme [ Thu May 01, 2014 8:40 am ]
Post subject:  Re: dashes in filename interpreted as invalid option

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"?

Author:  Watael [ Thu May 01, 2014 12:19 pm ]
Post subject:  Re: dashes in filename interpreted as invalid option

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.

Author:  Khryme [ Sat May 03, 2014 2:41 am ]
Post subject:  Re: dashes in filename interpreted as invalid option

thank you so much for your support! :)

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com