1

I'm trying to use a input variable $1 in a path

if [ -d "$1" ]; then
   echo "The Directory Exists"  
   files=`find /cygdrive/c/2014-03-19 -name "*ellow.txt"`
   for file in $files; do
   etc...

Now instead of using the date, I'd like to use $1 (as the users inputted variable) for which directory they'd like to use. /cygdrive/c/$1. But I don't think this works inside back ticks. Any suggestions? I've tried assigning $1 a variable name but it's still the same issue of it taking it literally. How could I use a variable in this path?

Cheers lads.

8
  • Have you tried it inside the backticks? You might be surprised ;) Commented Mar 19, 2014 at 19:54
  • 1
    You have a completely different bug here, by the way -- try finding files whose names (or directory names) contain spaces and see what happens. See also the first entry in mywiki.wooledge.org/BashPitfalls Commented Mar 19, 2014 at 19:56
  • 1
    The safe way to use find would be, instead: while IFS='' read -r -d '' file; do ...; done < <(find ... -print0) Commented Mar 19, 2014 at 19:57
  • 1
    By the way, it needs to be "/cygdrive/c/$1", with the double quotes. Otherwise, the name can be split into multiple find arguments rather than passed as a single one. Commented Mar 19, 2014 at 20:00
  • 1
    @CharlesDuffy, please create an answer with your cogent points. Commented Mar 19, 2014 at 20:01

2 Answers 2

2

Expansions certainly do occur in these contexts. That said, there's care to be taken in using find correctly; it's easy to do things that break with unusual filenames, whether such filenames are created by software bugs or malicious intent.

if [ -d "/cygdrive/c/$1" ]; then
    while IFS='' read -r -d '' file; do
      something_with "$file"
    done < <(find "/cygdrive/c/$1" -name '*ellow.txt' -print0)
else
    echo "$1 is not a directory" >&2
fi

References:

Sign up to request clarification or add additional context in comments.

5 Comments

Ah I didn't see your answer here and posted something similar. +1
Can you explain the rationale behind setting IFS? I get the rest of it.
@Emmet, setting IFS to an empty string prevents trailing whitespace from being trimmed. Notably, passing no destination variable name and using REPLY (as @anubhava's answer did) also has that effect.
@Emmet ...by the way, it's actually essential that it be -d '' or -d $'\0' (though I think this second option is mildly misleading about how the parser works), but not -d$'\0' -- the latter parses out to just -d, leaving the next argument's first character to be parsed as the delimiter.
@CharlesDuffy: thanks for that. It's a recipe to add to my snippet bag. The "oddball" filenames issue has never bitten me, but there's never any harm doing something the right way.
1

Yes, variables are expanded inside backticks:

> test=foo
> echo `echo $test`
foo

So your script should work as written.

It's generally considered better practice, though, to use $() (unless you need compatibility with older platforms that don't support that construct). This is nearly equivalent to backticks, but it allows nesting (and is a little clearer).

EDIT: Although this is the answer to the question as asked, Charles Duffy has pointed out a couple other problems with the script as it stands. In general, these issues only affect files with "odd" names (those including spaces and globbing characters), but if there is any possibility that your script will ever be run in such a context, then these issues should be taken into account. (And if this is more than just a one-off single-session function definition, then, yes, there's a possibility that it will be re-used in a context you don't expect.)

1 Comment

There are bigger best-practices issues here -- $(find ...) is just as bad as using backticks; both string-split and glob-expand your filenames when trying to iterate over individual results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.