0

I'm very new to bash and I'm trying to run script in python I have. When the script runs, the first thing it prompt is y/n question, and I would like to automate this part so I can run it automatically without this part.

Based on this post, I have tried to autofill it with the following script:

(.venv) reut@whatever:~/git/my_amazing_script$ echo -e "y\ny\ny\ny"; for year in $(seq 20
17 1 2020) ;do python3 ./my_nice_script.py -start_year $year  -end_year $year ;done

>>>

y
y
y
y

Will process with the following parametersstart_year=2017, end_year=2017,
Please confirm [y/n].

As it can be seen, when it runs, it prints first all the y and then prompt again the y/n question.

I would like my script to run and autofill the y/n so I don't need to type and it can iterate. How can I do that?

Reut
  • 101
  • 1
  • Any reason you can't just pipe `yes` into it? – Toby Speight Oct 20 '22 at 15:31
  • @TobySpeight it takes long time to run and I want to leave it without checking every 20 minutes if it prompted the y/n question and click on y for it to start the next loop – Reut Oct 20 '22 at 15:32
  • why do you need to do it in a loop? – aviro Oct 20 '22 at 15:32
  • 3
    I think what @TobySpeight meant, was to pipe "y" into the python command. `echo "y" | python3 ./my_nice_script.py -start_year $year -end_year $year` – aviro Oct 20 '22 at 15:35
  • 1
    @aviro, I'm pretty sure they meant `yes | python3 ...` – ilkkachu Oct 20 '22 at 15:39
  • 2
    @Reut, what you're doing there is just running `echo` before the Python script, without any redirections. It'll just print to the terminal, same as you ran `echo hello` any other time. You're looking for the pipe operator `|`. – ilkkachu Oct 20 '22 at 15:40
  • See [What are the shell's control and redirection operators?](https://unix.stackexchange.com/q/159513/170373) – ilkkachu Oct 20 '22 at 15:40
  • @Reut, that's what `yes` is for - read its man page. In your example, you would write `yes | python3 ./my_nice_script.py …` – Toby Speight Oct 20 '22 at 15:56
  • You want `echo … | for …`, not `echo … ; for …`. Similarly with `yes` you want `yes | …`, not `yes ; …`. – Kamil Maciorowski Oct 20 '22 at 16:10
  • 1
    Is it *your* python script? If so, consider giving it an option to bypass the confirmation. If not, look to see if it already has such an option! – frabjous Oct 20 '22 at 21:05
  • @aviro I have tried your solution and it works for the first iteration but the for the 2nd it stops and doesn't automatically echo "y" – Reut Oct 21 '22 at 13:03

0 Answers0