How to Run each file in the directory by adding the file name in RUN_Comand provided

42 Views Asked by At

I have bunch of .sv files in the following directory /home/vs/vs_work2/Miss.Chim_DEPBand I want to run the following command with each file name from the above directory

make sim TB=depb/sim/utb/epb_uvm_tb.json TC= **USE_FILE_NAME_From_ABOVE_Directory** 

and keep the result of each file after the run into a separate file along with its files name that it ran with.

This what I tried

#!/bin/bash
FILES=/home/vs/vs_work2/Miss.Chim_DEPB/*.sv

NUM_OF_FILES= ls /home/vs/vs_work2/Miss.Chim_DEPB/*.sv | wc -l   # this will lst number of files in that directory

declare -a vendec[NUM_OF_FILES]  # declared array "-a" is used for index array

arraylength=${#vendec[@]}


for f in $FILES
do
  #echo -n "" > vendec[*].txt  #to empty file
  
  vendec[i]
  
  echo "$f" >> vendec.txt
  
  
done
1

There are 1 best solutions below

0
damienfrancois On

You can run the following one-liner:

ls /home/vs/vs_work2/Miss.Chim_DEPB/*.sv | xargs -I {} bash -c "make sim TB=depb/sim/utb/epb_uvm_tb.json TC={} && echo {} >> vendec.txt"

It will list all .sv files in /home/vs/vs_work2/Miss.Chim_DEPB/ and then, for each file, it will run the make sim TB=depb/sim/utb/epb_uvm_tb.json TC={} command with the {} placeholder being replaced with a file from the directory, and if that succeeds, it will write the name of the file at the end of the vendec.txt file.

Note that you can use the -P option of xargs to have multiple runs of the make sim .... command in parallel.