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

5
  • 1
    What did you try for yourself/ Commented Mar 14, 2017 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 / Batch parameters Have fun. Try it, post your code, and we'll help you get further. Though, are you in Windows? Commented Mar 14, 2017 at 14:31
  • Something like 'for %%i in (path to directory \ *.jpg) do threshold.exe %%i' this should work I guess.. Commented Mar 14, 2017 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. Commented Mar 14, 2017 at 15:00
  • In the future, by the way, try to follow 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. Commented Mar 14, 2017 at 15:12

2 Answers 2

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

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

3 Comments

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.
(...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).
@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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.