'How can I execute a python file whose name is passed as a string in a shell script?

I have a script run.sh with the following contents.

for ARGUMENT in "$@"
do
    KEY=$(echo $ARGUMENT | cut -f1 -d=)
    VALUE=$(echo $ARGUMENT | cut -f2- -d=)   
    
    case "$KEY" in
    
            gpu_num)            gpu_num=${VALUE} ;;
            data)               data=${VALUE} ;;
        *)   
    esac  
done

CUDA_VISIBLE_DEVICES=${gpu_num} python train.py --data $data

I can then run the train.py python file by executing the following command.

./run.sh gpu_num=0 data="kit"

How can I also pass the python file to be executed as a string? For example

CUDA_VISIBLE_DEVICES=${gpu_num} python ${python_file} --data $data

and

./run.sh gpu_num=0 python_file="test.py" data="kit"


Solution 1:[1]

for ARGUMENT in "$@"
do
    KEY=$(echo $ARGUMENT | cut -f1 -d=)
    VALUE=$(echo $ARGUMENT | cut -f2- -d=)   
    
    case "$KEY" in
    
            gpu_num)            gpu_num=${VALUE} ;;
            data)               data=${VALUE} ;;
            python_file)        python_file=${VALUE} ;;
        *)   
    esac  
done

CUDA_VISIBLE_DEVICES=${gpu_num} python $python_file --data $data

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CY-OD