CentOS 7中mkdir命令在Bash脚本内无法工作的问题求助
Hey Arun, sorry to hear you're stuck with this mkdir problem—let's break down the most common reasons this happens and how to fix them!
1. Check Your Working Directory & Paths
The #1 culprit here is usually mismatched working directories. When you run mkdir manually, you're probably in the right folder, but your script might be executing from a different location (like your home directory, or where you called the script from).
- Try adding
echo "Current working directory: $(pwd)"at the top of your script to confirm where it's running. - Always use absolute paths for your target directories instead of relative ones. If you need to use a relative path, add a
cd /path/to/your/base/directoryat the start of your script to lock in the right location.
2. Double-Check Variable Quoting
If your input file has directory names with spaces, special characters (like &, *), or even leading/trailing spaces, unquoted variables will break mkdir. For example:
- Bad:
mkdir $dir_name(splits "My Folder" into two arguments:MyandFolder) - Good:
mkdir "$dir_name"(preserves the entire string as one path)
Make sure every time you reference a variable from your input file, you wrap it in double quotes.
3. Forgot the -p Flag for Recursive Creation
If you're trying to create nested directories (like docs/reports/2024), plain mkdir will fail if the parent directories (docs or docs/reports) don't exist. When you run it manually, you might be using mkdir -p without realizing it—add the -p flag to your script's mkdir command to create all parent directories automatically:
mkdir -p "$full_dir_path"
4. Hidden Characters in Input Lines
If your input file was created on Windows, it might have Windows-style line endings (\r\n instead of Unix's \n). This adds an invisible \r character to the end of each directory name, which makes mkdir try to create a path with that hidden character (e.g., mydir\r instead of mydir).
Fix this by:
- Using
dos2unix your_input_file.txtto convert the file to Unix line endings. - Or, improve your line-reading loop to strip unwanted characters:
while IFS= read -r line; do # Remove any trailing whitespace/newline characters dir_name=$(echo "$line" | xargs) mkdir -p "$dir_name" done < your_input_file.txt
5. Permission Issues (Less Likely, But Worth Checking)
Even if mkdir works manually, your script might be running as a different user (e.g., if you're executing it with sudo, or via a cron job). Add echo "Running as user: $USER" to your script to confirm the user context. If permissions are the issue, you might need to adjust the target directory's permissions or run the script with appropriate privileges.
Debugging Tip
Add set -x at the very top of your script—this will print every command the script executes, exactly as it's run. You'll be able to see if the mkdir command is using the wrong path, has extra characters, or is missing flags.
Here's a quick example of a robust version of your script that addresses most of these issues:
#!/bin/bash # set -x # Uncomment for debugging INPUT_FILE="your_directories.txt" TARGET_ROOT="/path/to/your/base/folder" while IFS= read -r line; do # Skip empty lines [[ -z "$line" ]] && continue # Clean up the line (remove extra whitespace/hidden chars) clean_dir=$(echo "$line" | xargs) # Build full absolute path full_path="$TARGET_ROOT/$clean_dir" # Create directory with verbose output mkdir -vp "$full_path" # Check if creation succeeded if [[ $? -ne 0 ]]; then echo "ERROR: Failed to create $full_path" fi done < "$INPUT_FILE"
Give these steps a try—chances are one of them will fix your issue!
内容的提问来源于stack exchange,提问作者user9249103

