Mac系统下如何在R中创建可双击生成指定数量文件夹的Bash脚本
Got it, let's work through why your .sh script isn't behaving like the Windows .bat version. The key differences here are Mac's shell environment, how R is invoked non-interactively, file permissions, and default app associations for .sh files. Here's a step-by-step solution:
1. Understand the Core Issue
On Windows, .bat files run in Command Prompt and can call R.exe directly, but on Mac:
- Double-clicked
.shfiles might open in TextEdit by default instead of Terminal. - You need to use
Rscript(not the interactiveRcommand) to run R code non-interactively. - Shell scripts need executable permissions to run.
- Working directories for double-clicked scripts aren't always what you expect (use absolute paths!).
2. Create a Working Bash Script
Here's a complete, tested script that will do exactly what you need. Save it as create_folders.sh:
#!/bin/bash # Ensure we're using bash shell # Define your R logic inline (or point to a separate .R file) Rscript -e ' # Replace this with your absolute target path (critical for double-click!) Saveloc <- "/Users/your_username/Projects/My_Folders" # Your vector of folder names (length = number of folders created) folder_names <- c("Experiment_1", "Experiment_2", "Control_Group", "Backup") # Create the base directory if it doesn't exist if (!dir.exists(Saveloc)) { dir.create(Saveloc, recursive = TRUE) } # Loop to create each subfolder for (folder in folder_names) { folder_path <- file.path(Saveloc, folder) dir.create(folder_path, recursive = TRUE, showWarnings = FALSE) } # Optional: Print confirmation to Terminal cat("Successfully created", length(folder_names), "folders at", Saveloc, "\n") '
Key Notes for the Script:
- Absolute Path for
Saveloc: Never use relative paths here—double-clicked scripts run from a default working directory (like~/Desktop), so absolute paths ensure R knows exactly where to create folders. RscriptInstead ofR:Ropens an interactive session, butRscriptruns code silently in the background (perfect for double-click use).showWarnings = FALSE: Prevents errors if folders already exist.
3. Give the Script Executable Permissions
Mac won't let you run a shell script unless it has execution rights. Do this:
- Open Terminal.
- Run this command (replace
/path/to/create_folders.shwith your actual file path):chmod +x /path/to/create_folders.sh
4. Make Double-Clicks Run the Script in Terminal
By default, Mac might open .sh files in TextEdit. Fix this:
- Right-click your
create_folders.shfile. - Select Get Info.
- Under Open With, click the dropdown menu and choose Terminal.
- Click Change All... to set Terminal as the default app for all
.shfiles.
5. Alternative: Create a Double-Clickable App (More User-Friendly)
If you want a cleaner experience (no Terminal window popping up, or a dedicated app icon), use Automator:
- Open Automator (found in Applications or via Spotlight).
- Create a new Application.
- In the search bar, find Run Shell Script and drag it into the workflow area.
- Paste the entire
Rscript -e '...'block from the script above into the shell script box. - Save the app (e.g.,
Create_R_Folders.app). Now double-clicking this app will run your R folder-creation logic silently.
Troubleshooting Tips
- If
Rscriptisn't found: Runwhich Rscriptin Terminal to get its full path (e.g.,/Library/Frameworks/R.framework/Versions/Current/Resources/bin/Rscript) and replaceRscriptin your script with this absolute path. - Permission errors: If the target folder is in a restricted area (like
/System), addsudobeforeRscript(you'll need to enter your password when running the script). - Test first: Run the script in Terminal first (
./create_folders.sh) to catch any R syntax errors or path issues before relying on double-click.
内容的提问来源于stack exchange,提问作者FlyingDutch

