如何编写Shell脚本实现目录检查、切换或创建(使用nano编辑器)
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 existsmkdir -p "$TARGET_DIR": Creates the directory (and any missing parent directories, if needed) if it doesn't existcd "$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 thetouchlines 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:
Open nano and create the script file:
Run this command in your terminal:nano dir_file_check.shThis will open a blank nano editor window ready for input.
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 useCtrl+Shift+Von most Linux/macOS systems).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+Xto exit the nano editor.
- Press
Make the script executable:
Before you can run it, you need to grant execution permissions:chmod +x dir_file_check.shRun 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

