6.sh script

#!/bin/bash #1 - output file name #2 - the target folder #3 - extension find $2 -name '*.$3' > $1 

Called find from terminal works

 find /media/ubuntu -name '*.txt' > txtFile.txt 

In the script, find does not work, writes 0 bytes to the file:

 6.sh txtFiles.txt /media/ubuntu txt 
  • one
    probably worth replacing '*.$3' with \*.$3 - aleksandr barakin
  • @alexanderbarakin Helped, I will note that you need to remove the quotes, I first just tried to screen * - Nikita
  • @alexanderbarakin Thank you) You can give an answer, or I myself will answer after some time, so that without an answer did not lie. - Nikita

1 answer 1

according to the posix standard , inside the string enclosed in "single" quotes ' , the "literal" value of each character is stored.

and, for example, instead of $3 value of the third parameter passed to the script is not substituted: the string is used “as is”.

you can replace the "single" quotes with "double" " :

 $ ... "*.$3" ... 

or you can remove the quotes altogether, but then, in order to avoid the shell interpreting the resulting expression *xyz as regular for the substitution of file / directory names , you need to “escape” the meta symbol * :

 $ ... \*.$3 ... 
  • It seems to me, or some single quote is not so ... - mymedia
  • set -f to prevent glob. man sh . - 0andriy