简体   繁体   中英

Run severel scripts from a folder one after another

I wrote 50 shell scripts named 00abc.sh , 00bcd.sh , 00cde.sh , 01def.sh , 02efg.sh , ..., 09xyz.sh . The higher the number, the higher the priority. Every script is in the same folder.

How can I write a wrapper which runs each script one after another, beginning with the last script ( 09xyz.sh ) and ending at the first ( 00abc.sh )? I don't want to hardcode the script names.

try

for f in `ls -1 |sort -rn` ; do
    if [ -x "$f" ];then
        $f
    fi
done

You can list all scripts in your folder with something like this, which checks that the file is executable, and that it is not a directory:

for f in *; do [ -x "$f" ] && [ ! -d "$f" ] && echo "$f"; done

You can sort the output, and use the -r option to reverse the sort order. So you could get a list of your scripts in the right order with

for f in *; do [ -x "$f" ] && [ ! -d "$f" ] && echo "$f"; done | sort -r

To actually run the found scripts, you can use eval :

for f in *; do [ -x "$f" ] && [ ! -d "$f" ] && echo "$f"; done | sort -r | while read runit; do ./$runit; done

(Add echo before ./$runit to check first what would be run)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM