1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Run python script inside bash script

Discussão em 'Python' iniciado por Stack, Setembro 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have created a bash script to handle the analysis of my project data. In the bash script, I have a function called run_gromacs_command.

    COMMAND_FILE="gromacs_cmds.txt"
    LOGFILE="gromacs_log.txt"
    # Check if the command file exists
    if ! [ -e "$COMMAND_FILE" ]; then
    echo "There is no command file. Creating one!!!"
    touch "$COMMAND_FILE"
    fi
    # Read the command file into an array
    readarray -t rows <"$COMMAND_FILE"
    run_gromacs_command() {
    local CMD_STR="$*"
    local vars=""

    # Check if the command has been run before
    for row in "${rows[@]}"; do
    if [[ "$CMD_STR" == "$row" ]]; then
    vars="$CMD_STR"
    break
    fi
    done
    if [[ "$vars" == "$CMD_STR" ]]; then
    echo "Command: ${vars} has already been executed. Skipping."
    zenity --warning --title="Warning" --text="Performing auto skipping: \"$vars\" check the gromacs_cmd.txt for more details" --timeout 1
    else
    echo "Executing command: $CMD_STR"
    "$@" 2>&1 | tee -a "$LOGFILE"

    if [ "${PIPESTATUS[0]}" -ne 0 ]; then
    echo "Command failed. Passing to run_or_skip logic."
    run_or_skip "$@"
    else
    echo "$CMD_STR" | tee -a "$COMMAND_FILE"
    echo "Command succeeded!"
    fi
    fi
    }

    run_or_skip() {
    local CMD_STR="$*"
    echo "Re-running failed command: $CMD_STR"

    # Use "$@" here as well!
    "$@" 2>&1 | tee -a "$LOGFILE"

    if [ "${PIPESTATUS[0]}" -ne 0 ]; then
    ERROR_MSG="Error: Command failed again: $CMD_STR"
    echo "$ERROR_MSG" | tee -a "$LOGFILE"
    # ... (the rest of your error/zenity logic is fine) ...
    else
    echo "$CMD_STR" | tee -a "$COMMAND_FILE"
    fi
    }


    Basically, i pass command like this

    `'run_gromacs_command ${gromacs} make_ndx -f $GRO_FILES -o index_first.ndx <<<$'q\n''`


    The provided example works fine. When i try to pass python call, it doesnot append to log file and it also does not show any print statement in the bash shell while executing or after execution. The python command as follows:

    `echo "Starting Python analysis script..."
    run_gromacs_command python md_simulation_python_analysis.py \
    --traj_file "${simulation_name}_traj_${out_name}.xtc" \
    --pdb_file "${simulation_name}_${out_name}.pdb" \
    --pc1 "pc1_${out_name}.xvg" \
    --pc2 "pc2_${out_name}.xvg" \
    --no_gui --do_pca --do_salt --do_dihed --do_rama \
    --do_helix --do_q --do_dist --do_pc_conversion --do_free_energy
    if [[ $? -ne 0 ]]; then
    echo "The run_gromacs_command function reported an error. Exiting."
    exit 1
    fi`


    sometimes it executes and most of the time, it crashes the system. Funny thing is, when I ran this command directly into bash terminal, it runs fine and I get the print statement from python script.

    MY OBJECTIVE: I want to run python script call from bash script and I want to pass the print statement into bash terminal and log the same and want to avoid crashing the system.

    Continue reading...

Compartilhe esta Página