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

如何编写Shell脚本实现目录检查、切换或创建(使用nano编辑器)

Shell Script to Handle Directory Checks/Creation & File Existence Checks

Hey there, here's a practical shell script that does exactly what you need—plus I'll walk you through writing it with the nano editor step by step.

The Script Code

First, let's create a flexible script that takes the target directory and file as arguments (so you can reuse it for different paths). Save this as dir_file_check.sh:

#!/bin/bash

# Check if both directory and file arguments are provided
if [ $# -ne 2 ]; then
    echo "Usage: $0 <target_directory> <target_file>"
    exit 1
fi

TARGET_DIR="$1"
TARGET_FILE="$2"

# Check if the directory exists; if not, create it
if [ -d "$TARGET_DIR" ]; then
    echo "Directory $TARGET_DIR exists, switching to it..."
    cd "$TARGET_DIR" || exit 1  # Exit if cd fails (e.g., permission issues)
else
    echo "Directory $TARGET_DIR does not exist, creating it..."
    mkdir -p "$TARGET_DIR" || exit 1  # -p creates parent directories if needed
    cd "$TARGET_DIR" || exit 1
fi

# Check if the specified file does NOT exist
if [ ! -f "$TARGET_FILE" ]; then
    echo "⚠️  File $TARGET_FILE does not exist in $(pwd)"
    # You can add additional actions here, like creating an empty file:
    # touch "$TARGET_FILE"
    # echo "Created empty file $TARGET_FILE"
else
    echo "File $TARGET_FILE exists in $(pwd)"
fi

Let's break down what this does:

  • #!/bin/bash: Declares this as a bash script to ensure consistent behavior across environments
  • Argument check: Makes sure you pass both a directory and file path when running the script (avoids errors from missing inputs)
  • [ -d "$TARGET_DIR" ]: Checks if the target directory already exists
  • mkdir -p "$TARGET_DIR": Creates the directory (and any missing parent directories, if needed) if it doesn't exist
  • cd "$TARGET_DIR" || exit 1: Switches to the target directory, and exits the script immediately if the switch fails (e.g., due to permission issues)
  • [ ! -f "$TARGET_FILE" ]: Checks if the specified file does not exist—you can uncomment the touch lines if you want to automatically create an empty file when it's missing

Writing the Script with Nano

Nano is a simple, terminal-based text editor that's perfect for writing quick scripts. Here's how to use it:

  1. Open nano and create the script file:
    Run this command in your terminal:

    nano dir_file_check.sh
    

    This will open a blank nano editor window ready for input.

  2. Enter the script code:
    Copy the script above and paste it into the nano window (right-click to paste if you're using a GUI terminal, or use Ctrl+Shift+V on most Linux/macOS systems).

  3. Save and exit:

    • Press Ctrl+O (that's the letter O, not zero) to save the file. You'll see a prompt asking for the filename—just press Enter to confirm.
    • Press Ctrl+X to exit the nano editor.
  4. Make the script executable:
    Before you can run it, you need to grant execution permissions:

    chmod +x dir_file_check.sh
    
  5. Run the script:
    Use this format to run it, replacing the example paths with your actual directory and file:

    ./dir_file_check.sh /home/yourname/my_project /home/yourname/my_project/config.txt
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:51:10