1

I'm trying to loop source ./a.sh in the code below so that I can try all the possible pincodes for another bash file, how do I do that?

this is my current code:

#!/bin/bash

touch output.txt

for pincode in {0000..9999}; do
    echo "$pincode" | source ./a.sh > output.txt
done

I'm trying to add a while loop here but I don't know how, I also tried the following code:

#!/bin/bash

touch output.txt

for pincode in {0000..9999}; do
    echo "$pincode"
done | source ./a.sh

but again, I couldn't get the source ./a.sh to loop(im a beginner)

Mofi
  • 46,139
  • 17
  • 80
  • 143
Clientic
  • 13
  • 1
  • what's inside `a.sh`? – chovy May 19 '23 at 14:22
  • its just a shell script that asks for the pincode of 4 digits – Clientic May 19 '23 at 14:26
  • 1
    why do you think you need to `source` that script? Try your firoutput.txst code without `ource`. Seems like it should work (either way, actually). Also, I think you should move your output to the "edge" of the loop, i.e. `done > output.txt`. Good luck. – shellter May 19 '23 at 14:33
  • hi, added the output.txt after the loop, works now! thanks a lot :) – Clientic May 19 '23 at 14:38
  • Pipelines set up a subshell. `source` is used to run code in the _existing_ shell. So even if you could make `source` generally "work", it wouldn't be doing anything useful -- the very act of setting up a pipeline defeats the point of using `source` (instead of doing a normal out-of-process execution) in the first place. Unless you're using `source` so you can access un-exported variables, but that's silly -- just export them. – Charles Duffy May 19 '23 at 15:37

0 Answers0