1

I have a program named threshold. I have a set of images in a directory named images.

|-- images
|   -----img_1.jpg
|   -----img_2.jpg
|   -----img_3.jpg
|   ------
|   ------img_n.jpeg
|-- threshold.exe
|-- myscript

How to write a bash script, so that it accepts the directory name as the argument and pass each of the files in the directory individually as argument to program threshold.exe.

If I execute,

$./myscript images 

The final execution should be like this.

./threshold.exe img_1.jpg
./threshold.exe img_2.jpg
....
....
./threshold.exe img_n.jpg

Compo
  • 36,585
  • 5
  • 27
  • 39
Navin Prashath
  • 145
  • 2
  • 10
  • 1
    What did you try for yourself/ – Inian Mar 14 '17 at 14:23
  • Upon running, your batch file will have "internal" variables available to you. Assuming a batch file "DoIt.bat". A call like this `doit.bat "c:\folder path\to\wherever"` will park the `"c:\folder path\to\wherever"` argument to variable %1. Therefore, you would then query the filelist of %1 and pass each file to `threshold @file`. [ForFiles](https://ss64.com/nt/forfiles.html) / [Batch parameters](https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true) Have fun. Try it, post your code, and we'll help you get further. Though, are you in Windows? – blaze_125 Mar 14 '17 at 14:31
  • Something like 'for %%i in (path to directory \ *.jpg) do threshold.exe %%i' this should work I guess.. – Navin Prashath Mar 14 '17 at 14:40
  • BTW, `./threshold img_1.jpg` won't work, because `threshold` is in the parent directory, not the current directory. You want either `./threshold images/img_1.jpg` or some equivalent to `../threshold img_1.jpg`. – Charles Duffy Mar 14 '17 at 15:00
  • In the future, by the way, try to follow http://stackoverflow.com/help/mcve -- posting a question that describes not just what you want to do, but what you've tried, how it failed, and *the shortest amount of code that results in that same failure*, constructed such that someone else (not just you) can run it and see the same result. – Charles Duffy Mar 14 '17 at 15:12

2 Answers2

1

Assuming you are in a Windows environment.

forfiles /p %1 /m *.jpg /c "cmd /c threshold.exe @file"

-For each file in path (/p) %1

-Where %1 is the passed parameter, aka the folder you want to search into

-Run command (/c) cmd /c threshold.exe @file

-Where @file represents the path to the jpg file

blaze_125
  • 2,262
  • 1
  • 9
  • 19
  • Given as the OP is asking a "bash" question rather than a "batch" question, and used `./` to launch their program, that doesn't look like a safe assumption. – Charles Duffy Mar 14 '17 at 14:58
  • (...though the `.exe` extension is conflicting a bit, it's not a sure thing -- CLR executables run with Mono on your Unixen or PE files run with Wine still are generally installed with extensions intact). – Charles Duffy Mar 14 '17 at 14:59
  • @CharlesDuffy, based on the same queues I'm pretty sure too that he's not running Windows. Hopefully this info can still help, him or someone looking for a similar answer. – blaze_125 Mar 14 '17 at 15:07
1
#!/bin/bash
for dir; do
  for f in "$dir"/*; do
    ./threshold.exe "$f"
  done
done

This behaves as follows:

  • First, iterates over command-line arguments, treating each as a directory
  • Next, iterates over files within the current directory
  • Then, calls your executable for each file

Note that this will be of the form ./threshold.exe images/img_1.jpg rather than of the form ./threshold img_1.jpg -- this is necessary so that we're running ./threshold.exe from the directory that actually contains threshold.exe, and still providing a valid relative path to your file.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441