1

My code:

#!/bin/bash

scriptspy =(
    '/scs/sp1.py',
    '/scs/sp2.py',
    '/scs/sp3.py',
    '/scs/sp4.py',
    '/scs/spweb.py',
    '/scs/sp11.py',
    '/scs/spservice.py',
    ....
)

for i in scriptspy;
  do python3.7 $i;
done

My goal is to run a list of files that have python within the directory and not need to run one by one. individually would be python3.7 sp1.py

Obs: I can't execute all .py files in the directory, I need to pass a list to the loop with their name.

2
  • 2
    to iterate over the array's elements you need for i in ${scriptspy[@]}. Your current code for i in scriptspy is iterating over a single string scriptspy. Commented Dec 17, 2019 at 20:03
  • 2
    @sergio The parameter expansion should be quoted. The array isn't properly defined, though, either: scriptspy=(/scs/sp1.py /scs/sp2.py /scs/sp3.py /scs/sp4.py). Commented Dec 17, 2019 at 20:15

4 Answers 4

2

Use a glob:

shopt -s nullglob
for i in /scs/sp[1-4].py; do
    python3.7 "$i"
done

I turn on nullglob here just in case the files don't exist.

As well, it's good practice to always quote variables.

BTW your array creation and dereferencing syntax is wrong. LMK if you want details.

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

12 Comments

In my case I have some scripts inside the directory that I can't execute, because they are dependencies, so I need to pass a list with the name of all of them.
@JB_ Oh, then just use a more precise glob. I updated the answer. I thought you wanted to run all files with a .py extension.
Can you do the reverse? Pass the files as I do not want you to run as an exception?
Because your glob pattern expands in alphanumeric order, and we don't know whether this is the order the OP wanted to have. In the concrete example he gave, he wanted for instance spweb to be executed before sp11, but after sp4. If you would modify your answer to for f in /scs/sp{[1-4],web,11,service}.py, it would be more obvious how the OP could keep his original order.
@user1934428 Ohh, I see what you mean. The question was edited after I posted my answer.
|
1

Simply :

#!/bin/bash

printf '%s\n' *.py | xargs -n1 python3.7

Comments

1

I would get them all done in parallel with GNU Parallel:

parallel python3.7 ::: sp[1-4].py

If you want to see what it would do, without actually doing anything, use:

parallel --dry-run ...

If you only want to run them sequentially, one after the other, use:

parallel -j1 ...

2 Comments

the vps that will run has only one core, I can't use parallelism
It doesn't matter, GNU Parallel will detect that.
-1

This should run python programs in parallel if shell script is not suppose to wait for all of them to be done

declare -a scriptspy=("/scs/sp1.py" "/scs/sp2.py", "/scs/sp3.py","/scs/sp4.py")

for i in "${scriptspy[@]}":
  do (python3.7 "$i" ; echo "script $i done") &
done


4 Comments

This won't run properly. You need to fix the array creation and dereferencing syntax first. LMK when you do and I'll reverse my downvote.
But doesn't the loop wait for the python script to finish to start the other?
Loop doesn't wait because you are running python process in back groud
So what I want to do is not feasible, because it cannot run in parallel, only what is currently in the loop, otherwise it will use 100% machine resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.