Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run all shell scripts in folder

Tags:

linux

bash

shell

I have many .sh scripts in a single folder and would like to run them one after another. A single script can be executed as:

bash wget-some_long_number.sh -H 

Assume my directory is /dat/dat1/files

How can I run bash wget-some_long_number.sh -H one after another?

I understand something in these lines should work:

for i in *.sh;...do ....; done

like image 227
code123 Avatar asked Dec 10 '16 18:12

code123


1 Answers

Use this:

for f in *.sh; do   bash "$f"  done 

If you want to stop the whole execution when a script fails:

for f in *.sh; do   bash "$f" || break  # execute successfully or break   # Or more explicitly: if this execution fails, then stop the `for`:   # if ! bash "$f"; then break; fi done 

It you want to run, e.g., x1.sh, x2.sh, ..., x10.sh:

for i in `seq 1 10`; do   bash "x$i.sh"  done 

To preserve exit code of failed script (responding to @VespaQQ):

#!/bin/bash set -e for f in *.sh; do   bash "$f" done 
like image 184
Kirill Bulygin Avatar answered Sep 21 '22 02:09

Kirill Bulygin