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

需求:开发支持命令历史与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. HISTSIZE controls how many commands are kept, and HISTCONTROL cleans up redundant entries.
  • Vi-Mode Editing: set -o vi turns on Vi-style editing. Press Esc to switch to command mode, where you can use Vi shortcuts like:
    • k/j to scroll through command history
    • dd to delete the current line
    • yy to yank (copy) the line
    • / to search through history
  • Input Loop: The while true loop keeps running until you press Ctrl+D. read -e enables readline support (which is what gives us history and editing capabilities), and -p sets 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 add history -a after each command to save the history immediately, so you don't lose it if the shell exits unexpectedly.

Step 3: Setup & Usage

  1. Make the script executable:
    chmod +x photo-shell.sh
    
  2. Run the shell:
    ./photo-shell.sh
    
  3. 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 to PATH (add export 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 PROMPT variable to something you prefer (e.g., PROMPT="📸> " for a fun camera icon).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:29:09