0

I am relatively new to bash scripting. I have a linux executable that takes one user input from the command line. That is, I can run the executable like this: ./executable [input]. I want to run the executable multiple times with different inputs, that is, something like this:

$ ./executable 16
$ ./executable 32
....

I want to use a bash script for my purpose. This is my attempt (which is simply incorrect):

for i in 1 2 3 ... 10
do
  temp = 16 * $i
  ./executable temp
done

How would I get this done?

  • Suraaj. Try to edit the description and make more clear what exactly you want. It is not clear yet. – ruud Jun 04 '20 at 07:54
  • @ruud, is it better now? – Suraaj K S Jun 04 '20 at 08:05
  • 2
    Welcome to the site. If you are new to shell scripting, may I recommend GreyCat&Lhunath's Bash Guide for reference, and shellcheck - also available as standalone tool - for debugging scripts? – AdminBee Jun 04 '20 at 08:06
  • equal sign with spaces does compare (not assign), your calculation can be done with arithmetic expansion or with external tool like expr, glob should be escaped \* and to call a value of variable you need $ sign – alecxs Jun 04 '20 at 08:09
  • @pLumo this question isn't about integer mathematics, it's about iterating over a range. That answer is not a good fit, because it doesn't include the basic syntax for iterating over a range. – Philip Couling Jun 06 '20 at 07:48

1 Answers1

3
for((i=16;i<=160;i=i+16)); do
  ./executable "$i"
done
Hauke Laging
  • 90,279
  • @pLumo Just habit, I guess. Plus the given structure. Who expects potential for extended thinking in a task like this... – Hauke Laging Jun 04 '20 at 09:28
  • Seems that for i in {1..10}; do ./executable $((i*16)); done is easier to read? Perhaps it's just me – Chris Davies Jun 05 '20 at 21:08
  • @roaima Yeah, I should have put more thinking into this. I thought I just fix the OP's code rather than thinking about a better approach. But the temp was completely useless. One could argue that mine saves one computation over yours. 8-) I guess I would even prefer a pure counting loop head and do the "real math" elsewhere for better understanding. That's probably why I did not come to this on my own. But: I really don't like brace expansion with numbers. – Hauke Laging Jun 05 '20 at 22:41