help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] for File in Folder_Variable_Name Expansion


From: Roger
Subject: Re: [Help-bash] for File in Folder_Variable_Name Expansion
Date: Mon, 16 Dec 2013 04:33:58 -0900
User-agent: Mutt/1.5.21 (2010-09-15)

> On Sun, Dec 15, 2013 at 08:50:20PM -0800, Eduardo A. Bustamante López wrote:
>On Sun, Dec 15, 2013 at 01:28:24PM -0900, Roger wrote:
>>
>> $ FOLDER="${HOME}/Books/Learning?C/cse142/Lecture1/Files/slides/"; for file 
>> in "${FOLDER}"/*.jpg; do echo "${file}"; done
>So you have a directory named "Learning?C" ? Also, did you make sure
>that there are actually .jpg files in that directory (not .JPG or
>.jpeg, note the extra "e" or the case sensitivity)
>
>>
>>
>> $ FOLDER="${HOME}/Books/Learning?C/cse142/Lecture1/Files/slides/"; 
>> FFOLDER="${FOLDER}/*.jpg"; for file in "${FFOLDER}"; do echo "${file}"; done
>>
>> Output:
>> /home/roger/Books/Learning?C/cse142/Lecture1/Files/slides//*.jpg
>>
>>
>> I've read multiple forum posts, and they've all worked around or suggested 
>> (or deterred) the usage of eval.  (ie. $(eval echo "${FOLDER}") )
>Yep, never use eval unless you are sure of all the corner cases.

(Plus in my blunt opinion, eval's usage just looks like hacking versus easily 
read scripting, including it's output.)

>> I have some ideas, and of them thinking just declaring the "name" for the 
>> "for" command a static value. (ie. "for FILE in /path/to/folder/*.jpeg")
>>
>> (Read the Bash Manual and didn't notice any mention of possible problems 
>> using a concatenate variable name for "name" for the "for" command.)
>No, that pattern (for + glob) is completely normal.


This was the crux of my problem here.  Although the whitespace/space within the 
folder (or filename, or path) was an initial issue until I used a wildcard (or 
globbing, or "*", or "?") for substitution of the whitespace.

This main issue of "for FILE in /PATH/*.jpg" was really difficult, as I always 
prefer using global variables instead of statically assigned data.  (As most 
others likely do too.)  I was thinking this should just work when written as 
"for FILE in /PATH/*.jpg" as it appeared syntactically correct compared to 
other variable usage or other variable assignments.  (ie. printf 
"${HOME}/file.jpg")

Amazingly, all the previous answers posted provided a specific answer to each 
specific problem I encountered with my scripting here, as well as each 
follow-up providing some really clearly interpretable explanations of what I 
was seeing from my perception of the problem.  Without each of the previous 
posts, I would have been still been left with some further questions.  (A 
spectacular thread here, compared to the other explanations posted elsewhere!)

About the only question remaining, is why some bash commands (or functions) do 
not uniformly accept variable expansion similar to using within printf or echo. 
 
(ie. printf "${HOME}/file.jpg")  But suspect every command handles expansion 
based on each commands programmed method of handling a variable expansion.

Looking back over the Bash Manual, I see printf or echo listed under the "SHELL 
BUILTIN COMMANDS" category, and the "for" command categorized under "SHELL 
GRAMMAR > Pipelines".  As such, each category of commands, handles expansion 
differently?


-- 
Roger
http://rogerx.freeshell.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]