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

如何通过tcsh脚本并行运行多目录任务?

Extending Your tcsh Script for Parallel Multi-Directory Processing

Hey there! Let's fix up your script to handle directories a, b, and c with identical logic, and run all three tasks in parallel. Here's the revised version:

#!/bin/tcsh -f

# Define a reusable function to handle a single directory
process_dir() {
    set target_dir = $1
    set original_pwd = $PWD

    # Create directory, enter it, and set up symlink if it doesn't exist
    if (! -d $target_dir) then
        mkdir $target_dir
        cd $target_dir
        ln -sfn temp.txt
    endif

    # Navigate to the target directory and execute the job
    cd $original_pwd/$target_dir
    job << END
# some tool specific settings
END

    # Return to the original working directory
    cd $original_pwd
}

# Loop through a, b, c and run each task in the background (parallel)
foreach dir (a b c)
    process_dir $dir &
end

# Wait for all background tasks to finish before exiting
wait

Key Changes & Explanations:

  • Reusable Function: We wrapped the directory setup and job execution into a process_dir function. This lets us reuse the exact same logic for a, b, and c without duplicating code.
  • Parallel Execution: Adding & at the end of the process_dir call sends each task to run in the background. This means all three jobs will execute simultaneously instead of one after another.
  • Fix Directory Check: The original script had an invalid check if (!a) — we replaced it with ! -d $target_dir to properly test if the directory exists.
  • Wait for Completion: The final wait command ensures the main script doesn't exit until all background jobs have finished running. This is crucial if you need the script to only exit once all work is done.

内容的提问来源于stack exchange,提问作者RMK

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:58:56