How to create a script on Ubuntu

Asked

Viewed 76 times

-2

I need to make a script to run the command below on the terminal several times:

opencv_createsamples -img toras/toras_00001.jpg -bg negativas/negativas.txt -info positivas1/positivas1.txt -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 100 -bgcolor 0

What will change each time the command runs are these parameters:

-img toras/toras_00001.jpg
-info positivas1/positivas1.txt

Where it’s 1, turn 2, 3, 4, 5... up to 492

Does anyone have any idea how to do?

Making a comparison with C++, I believe it’s something like:

for(int i = 1; i <= 492; i++)
{
    opencv_createsamples -img "toras/toras_0000"+to_string(i)+".jpg" -bg negativas/negativas.txt -info "positivas"+to_string(i)+"/positivas"+to_string(i)+".txt" -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 100 -bgcolor 0
}

But I haven’t figured out how to write something like this in bash...

  • 1

    https://pastebin.com/0HY1crw7

  • Thank you so much for your help. Pardon ignorance, but how did you do it? My question is very basic for someone to negatively??

  • 1

    I used the command seq to generate a sequence of 0 to 492; To form the value 00001, I used the printf, to the Zero-Padding. In this command, define that it will have to complement with the number "0" until completing 4 digits (0001, 0010, 0100, 1000). P.S.: O for works just like in Golang. It will go through all the values generated by seq

  • 1

    About the negative votes, I don’t know why (I didn’t negatively). Perhaps people did not understand the question and resolved to negatively; or perhaps it is for other reasons. I recommend the link https://pt.meta.stackoverflow.com/search?q=qnegativo. About the closing vote, I honestly did not understand. The question is within the scope.

  • I got it, very good your reasoning for generating the number, I hadn’t thought of that. I had thought about using conditionals, but it would be more work, and I have no idea how to program in bash. I only made a modification on: filename=$(printf %04d $i), I replaced 4 by 5. It worked perfectly, thank you very much!

1 answer

1


Hail!

First of all you have significant zeros in your file nomenclature:

-img toras/toras_00001.jpg

It’s five digits, so it’s important to keep the five digits when your bow goes over 10, 100. Then, the first step, after the repeat loop, is to define a variable where your 5-digit number is stored.

Then you make the call of the commands by varying the full number.

for i in {1..492}
do
    n=`printf "%05.0f" $i`
    opencv_createsamples -img "toras/toras_$n.jpg" -bg "negativas/negativas$i.txt" -info positivas1/positivas1.txt -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 100 -bgcolor 0
done 
  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.