基于Python的SLURM脚本:sed遍历索引列表失败问题求助
Key Issues & Solutions
1. Shell Variable Interpolation Failure
Your sed commands used shell variable syntax (${StrTi_List[j]}) instead of directly inserting Python list values. The shell has no access to Python variables, so these expanded to empty strings, resulting in blank lines in your config files.
Fix: Use Python's f-strings to inject the actual values from StrTi_List and Dir_List into the sed command strings.
2. Incorrect Job Submission Command
The line os.system('#SBATCH gchp.runscript') does nothing—#SBATCH is a comment directive in Slurm scripts, not an executable command. You need to use sbatch to submit jobs.
Fix: Replace the line with os.system('sbatch gchp.runscript').
3. Robust Pattern Matching (Optional but Recommended)
To handle leading whitespace in your config files (common in many scripts), update your sed regex to include ^\s* (matches any leading whitespace before the target key).
Corrected Code
Here's the revised loop section of your script:
for j in range(Num_Jobs): current_time = StrTi_List[j] current_dir = Dir_List[j] # Edit runConfig.sh with robust pattern matching sed_cmd1 = rf'sed -i "s|^\s*Start_Time=.*|Start_Time=\"{current_time}\"|" ./runConfig.sh' os.system(sed_cmd1) # Edit HISTORY.rc with robust pattern matching sed_cmd2 = rf'sed -i "s|^\s*EXPID:.*|EXPID:\"{current_dir}\"|" ./HISTORY.rc' os.system(sed_cmd2) # Submit the Slurm job os.system('sbatch gchp.runscript')
Additional Best Practice
To avoid unexpected changes if your config files get modified in a way that breaks pattern matching, use template files and copy them at the start of each loop:
# Add these lines inside the loop before running sed commands os.system('cp runConfig_template.sh runConfig.sh') os.system('cp HISTORY_template.rc HISTORY.rc')
Make sure you create runConfig_template.sh and HISTORY_template.rc as clean copies of your original config files first.
内容的提问来源于stack exchange,提问作者Jordan-Yousef Jbour

