-1

find . | grep "B02_10m.tif$"

When executing this snippet in the terminal, I am able to get all the files that ends with B02_10m.tif. However I need this list as input for a c file to make some processing. The input must be a linux variable to pass it as part of the arguments for the script.

./avg outAVG $files Where $files is the variable containing the result of the first snippet.

Is that possible? I Already tried:

files = $(find . | grep "B02_10m.tif$")

Command 'files' not found, did you mean:

  command 'file2' from deb file-kanji
  command 'file' from deb file

Try: sudo apt install <deb name>

And it did not work.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • How is your C program called? Do you give it all the filenames in one go, or can you call it many times, once for each filename? Are you looking for the TIFF files in the current directory only, or in all subdirectories? What shell are you using, bash or zsh? Storing the filenames in a string is most likely not what you want to do. – Kusalananda Dec 03 '19 at 06:58
  • Makefile

    avg: avg.c gcc -o avg avg.c -lm -I/usr/include/gdal -L/usr/lib -lgdal run.sh


    root=/home/user/Dir/ files=$(ls $rootsatelliteimages) ./avg outAVG $files

    • NULLVAL is < 0

    */

    – Roger Almengor Dec 03 '19 at 07:04
  • This is the information found in the original script, but I want to replace the line files=$(ls $root*satelliteimages) for the results that I get when running the command find and the arguments I already presented. – Roger Almengor Dec 03 '19 at 07:05
  • Do consider adding properly formatted code to the actual question. Code in comments is very hard to read. Make sure that you add code exactly the way it is ($root*satelliteimages looks strange). – Kusalananda Dec 03 '19 at 07:14

2 Answers2

1

Assuming that you are using the bash shell:

shopt -s globstar nullglob dotglob

./avg outAVG ./**/*B02_10m.tif

This would call your avg program with all the files in or below the current directory that have filenames ending in B02_10m.tif. It does this by first enabling the ** globbing pattern with shopt -s globstar. This pattern matches "recursively" down into subdirectories. If your TIFF files are in the current directory, you don't need the ** bit of this pattern.

The other two shell options makes sure that globbing patterns matches hidden names and that they expand to nothing at all if there is no match, just to mimic what you're trying to do with find.

If you are using the zsh shell, then you may instead use

./avg outAVG ./**/*B02_10m.tif(.DN)

which would do the same. Again, if you know the directory where the TIFF images are stored, you don't need the ** bit of the pattern but can instead insert the correct path to that directory.

In any sh-like shell, if your files are in the directory named by $dir, you may use

./avg outAVG "$dir"/*B02_10m.tif

This would obviously not do a recursive search down into subdirectories of $dir.


Your approach does not work for two reasons:

  1. You have a syntax error in the assignment to the files variable. There can be no whitespace around the = in an assignment.

  2. Correcting that syntax error, you are combining many separate filenames into a single string. Doing this disregards the fact that filenames may well include spaces and characters usually used as shell wildcards. When you later use $files unquoted, the shell would split the string inte several words on whitespace characters (by default) and then expand all words that looks like shell globbing patterns. This would make your code misbehave if you don't always have very simple filenames.

For more information about these issues, see e.g.

Kusalananda
  • 333,661
0
files1=`find . | grep "B02_10m.tif$"`

can you try this?

  • You should use $( ... ) instead of backticks. And your suggestion will fail badly if any of the matching files contains a space in its name. – Chris Davies Dec 03 '19 at 07:32