0

I’m using a case loop to allow me to either press 1 to accept the value in $fn3 ,press 2 to accept the value in $fn4 or press 3 to input my own value.

These are being used in a find statement. If I press 3 and enter a part of a file name, $search1 echoes the data I typed in correctly and all is ok. If I press either 1 or 2 the variable $search echoes the correct value of either $fn3 or $fn4 but when I assign search1 with *$search*, echo search1 now holds the list of files in my current folder and not the vale of $fn3 or $fn4.

The $search1 variable is being used in a find command later in the script. What's causing the files to appear in the variable?

case $ans in
    1)     search=$fn3;;
    2)     search=$fn4;;
    3)     read search;;
    q*|Q*) quit 0;;
    *)     echo "please enter a number between 1 and 3";;
esac

echo $search    #use to see what variable search holds 

# $search hold part of the file name im going to search for,i  add * for searching for anything which has the search vale in its name.

search1=*$search* 

echo $search1    #use to see what variable search1 
arco444
  • 22,002
  • 12
  • 63
  • 67
  • Not really sure what you're trying to do but if you quote as: `search1='*$search*'` your issue will disappear. `*` is being expanded to all the files in your working directory by the shell. – arco444 Mar 31 '15 at 11:37
  • Depending on how you are using `case` to select the answer, you may want to take a look at the `select` statement: `select name [ in word ] ; do list ; done` – David C. Rankin Mar 31 '15 at 14:52

2 Answers2

1

Your variable is actually being set correctly, the problem is occurring because you when you used it, you didn't put double-quotes around it. Specifically, when the shell parses the command echo $search1, it substitutes the value of $search1 (which includes wildcards), and then performs word splitting (not important here) and wildcard expansion (which gives a list of matching files in the current directory), hands the result to the echo command, and that prints the list of matching files (rather than the actual value of the variable).

This same problem (and others too!) will occur in other contexts due to unquoted variables. For example, if you use search1 in a find command without quoting it properly (find /path/to/dir -name $search1), it'll expand the wildcards before passing the value to find, and then find will do something other than what you wanted based on that.

There are a few contexts where it's ok to leave variable references unquoted, like on the right-hand side of an assignment (search1=*$search* is actually ok), but it's easier to just always double-quote (e.g. search1="*$search*") than to keep track of when it's safe to leave the quotes off.

Speaking of which, case $ans in will cause problems if ans contains spaces and/or wildcards. Double-quote that too.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
0

All i'm trying to do is search for duplicate music files which have slightly different names that’s already on my hard drive and in separate folders. The music names could be 01 . flashdance-what a feeling.mp3 or what a feel (remix)#flashdance.mp3 The case in statement $fn3 is assigned say what a feeling $fn4 is assigned say Flashdance read is from the value i type in echo $search #display on screen and then the following find command searches a specific folder for the data thats in the variable "search" work_dir is the folder which the search has to take place in and will change.

(find "${work_dir}" -iname $search -type f -maxdepth 10 -exec ls -lh {} \; 2> /dev/null | awk '{print $5, $9, $10 , $11 , $12 , $13 , $14 , $15 , $16 , $17 ,$18 , $19 , $20 , $21 , $22 , $23 , $24"\n\n" }') . I have tried the above suggestions but it still displays my current folder and not just the search variable. the above command works fine it i use the read command but when search is assigned form either the $fn3 or $fn4 variable it does not work.Is there a better way of doing this.

  • It looks like you need double-quotes around `*$search*` -- without them, the shell performs wildcard expansion before passing the argument list to `find`, so all it sees is the list of matches in the current directory. – Gordon Davisson Mar 31 '15 at 19:04
  • I have done "*$search*" but still no joy – Andrew Sams Mar 31 '15 at 20:20