You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Python的SLURM脚本:sed遍历索引列表失败问题求助

Fixes for Your GCHP Job Submission Script

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').

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 21:10:28