Bash for in loop that executes wrongly when no file found -


i have bash "for in" loop looks pdf files in directory , prompt them (simplified example)

#!/bin/bash pic in "$input"/*.pdf     echo "found: ${pic}" done 

this script works when there pdf files in $input directory, however, when there no pdf files in directory, :

found: /home/.../input-folder/*.pdf

is expected behaviour ? how can deal with in loop ? need use ls or find ?

i tried , without quotes around "$input". there no spaces in files names , directory names.

many ideas.

this expected behavior. according bash man page, in pathname expansion section:

after word splitting, unless -f option has been set, bash scans each word characters *, ?, , [. if 1 of these characters appears, word regarded pattern, , replaced alphabetically sorted list of file names matching pattern. if no matching file names found, , shell option nullglob not enabled, word left unchanged.

as result, if no matches "$input"/*.pdf found, loop executed on pattern itself. in next sentence of man page:

if nullglob option set, , no matches found, word removed.

that's want! do:

#!/bin/bash shopt -s nullglob pic in "$input"/*.pdf     echo "found: ${pic}" done 

(but aware may change behavior of other things in unexpected ways. example, try running shopt -s nullglob; ls *.unmatchedextension , see happens.)


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -