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

如何在Linux脚本中添加用户输入框以动态修改路径

Bash Script to Automate Subject Directory Setup

Got it, let's build a script that handles exactly what you need—no manual code edits required! This will prompt the user for their subject number, automatically replace all those placeholder underscores, and set up your directory structure smoothly.

#!/bin/bash

# Prompt user for the subject number with your custom message
read -p "What is the subject number: emsint____FSL " subj_num

# Define the full subject directory name by replacing the placeholder
subj_dir="emsint${subj_num}FSL"

# Navigate to the root data directory (with error checking)
cd /mnt/netshare/imaging/emoProc_MS_Intv_NMSS/data || { echo "Error: Could not access root data directory"; exit 1; }

# Create the main subject directory (no error if it already exists)
mkdir -p "$subj_dir"

# Fixing the path from your original snippet (assuming this was a typo—adjust if needed!)
cd "$subj_dir" || { echo "Error: Could not access subject directory"; exit 1; }

# Create session folders
mkdir -p session1 session2

# Navigate to session1 and create task subdirectories
cd session1 || { echo "Error: Could not access session1 directory"; exit 1; }
mkdir -p control exper1 exper2

echo "✅ Directory setup complete for subject emsint${subj_num}FSL!"

Quick Breakdown of Key Bits:

  • User Input: The read -p command displays your custom prompt and captures the subject number directly into the subj_num variable.
  • Placeholder Replacement: We build the full directory name once with subj_dir="emsint${subj_num}FSL" so we don't repeat the replacement logic everywhere.
  • Error Safety: The || { ... exit 1; } checks stop the script and alert you if any directory navigation fails—great for catching typos in paths!
  • mkdir -p: This flag creates directories (and any missing parent folders) without throwing errors if the directory already exists, so you don't accidentally overwrite anything.

Note: I adjusted one path from your original snippet (/mnt/netshare/imaging/emoProc_MS_Intv_NMSS_data_emsint____FSL) to match the logical structure of your first cd command. If that path was intentional, just replace the second cd "$subj_dir" line with cd /mnt/netshare/imaging/emoProc_MS_Intv_NMSS_data_${subj_dir} instead.

内容的提问来源于stack exchange,提问作者E. Kess

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:11:37