Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to execute Code in batch under Linux

Shulou Source: shulou.com Published: 2022-06-01 11:42:33 09月26日 Update

This article mainly introduces how to execute the code in batches under Linux. It is very detailed and has a certain reference value. Friends who are interested must finish it!

The method of batch execution of code under Linux. Complex can be written in snakemake definition rules, simple shell can be written directly.

Sample code

What you want to achieve:

1, there are 0-9, 10 folders

2. Go to the folder and execute the run.sh script. Because it takes a long time to run, you want to use nohup run.sh & to put it in the background, and save the running log nohup.out at the same time.

1for I in [0-9] *; # 1

2 do

3 cd $I; # 2

4 nohup run.sh &; # 2

5 cd.. /

6done

Error

Because I used the & symbol in the middle, I entered the background ahead of time and couldn't get into the next loop.

If not &, then it is executed sequentially and cannot be carried out in batches.

1for I in [0-9] *; # 1

2 do

3 cd $I; # 2

4 nohup run.sh; # 2

5 cd.. /

6done

Success Code 1for I in [0-9] *; # 1

2 do

3 cd $I; # 2

4 echo "nohup run.sh &" | bash; # 2

5 cd.. /

6done

Here, nohup one-line command, first print with echo, complete the command nohup run.sh &, and then use | bash to execute the previous command, which will achieve what I want to achieve.

Code written in one line

1for I in [0-9] *; do cd $I; echo "nohup run.sh &" | bash; cd.. /; done

The above is all the contents of the article "how to execute Code in batch under Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Code commands one line content background files folders articles run complex successful no value interest at the same time guys buddies that is ways methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft Shulou Technology Xiaomi MariaDB