需求:开发支持命令历史与Vi式编辑的循环Bash脚本
Alright, let's put together a custom Bash shell wrapper that gives you command history and Vi-style editing for your photo workflow scripts. This will run in a loop until you hit Ctrl+D, just like a regular shell, and let you execute your existing scripts with all the editing conveniences you want.
Custom Bash Shell Wrapper for Photo Workflow Scripts
Step 1: The Core Script
Here's the complete script that implements all your requirements. Save it as photo-shell.sh:
#!/bin/bash # Configure command history persistence and behavior export HISTFILE="$HOME/.photo_workflow_history" export HISTSIZE=1000 # Keep last 1000 commands export HISTCONTROL=ignoreboth:erasedups # Ignore duplicates and commands starting with space # Enable Vi-style command line editing set -o vi # Custom prompt for your photo workflow shell PROMPT="photo-workflow> " # Main loop: read and execute commands until Ctrl+D while true; do # Read input with readline support (for history/editing) read -e -p "$PROMPT" cmd_input # Exit loop if Ctrl+D is pressed (read returns non-zero exit code) if [ $? -ne 0 ]; then echo -e "\nExiting photo workflow shell..." break fi # Skip empty input lines if [ -z "$cmd_input" ]; then continue fi # Execute the command eval "$cmd_input" # Immediately save history to file (so it persists even if the shell crashes) history -a done
Step 2: How It Works
Let's break down the key parts:
- History Configuration: We set a dedicated history file (
~/.photo_workflow_history) so your photo workflow commands don't mix with your regular Bash history.HISTSIZEcontrols how many commands are kept, andHISTCONTROLcleans up redundant entries. - Vi-Mode Editing:
set -o viturns on Vi-style editing. PressEscto switch to command mode, where you can use Vi shortcuts like:k/jto scroll through command historyddto delete the current lineyyto yank (copy) the line/to search through history
- Input Loop: The
while trueloop keeps running until you press Ctrl+D.read -eenables readline support (which is what gives us history and editing capabilities), and-psets your custom prompt. - Command Execution:
eval "$cmd_input"runs your photo workflow scripts just like you'd run them in a regular shell. We also addhistory -aafter each command to save the history immediately, so you don't lose it if the shell exits unexpectedly.
Step 3: Setup & Usage
- Make the script executable:
chmod +x photo-shell.sh - Run the shell:
./photo-shell.sh - Now you can execute your photo workflow scripts directly in this shell, use Vi-style editing, and access your command history with arrow keys or Vi shortcuts. Press Ctrl+D when you're done to exit.
Bonus Tips
- If your photo workflow scripts aren't in your
PATH, you can either add their directory toPATH(addexport PATH="/path/to/your/scripts:$PATH"to the top of the script) or call them with their full path. - To customize the prompt, just change the
PROMPTvariable to something you prefer (e.g.,PROMPT="📸> "for a fun camera icon).
内容的提问来源于stack exchange,提问作者Rex Block
相关产品推荐
相关产品推荐

