shell解析长参数

//以下代码来自于GPT3.5

#!/bin/bash

# Define the long options
LONGOPTS="help,input:,output:"

# Parse the options
PARSED=$(getopt --options=hi:o: --longoptions="$LONGOPTS" --name "$0" -- "$@")

# Evaluate the parse results
eval set -- "$PARSED"

# Loop through the options and arguments
while true; do
    case "$1" in
        -h|--help)
            echo "Usage: $0 [OPTIONS]"
            echo "Options:"
            echo "  -h, --help           Print this help message"
            echo "  --input=FILE         Specify input file"
            echo "  --output=FILE        Specify output file"
            exit 0
            ;;
        -i|--input)
            INPUT_FILE="$2"
            shift 2
            ;;
        -o|--output)
            OUTPUT_FILE="$2"
            shift 2
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Unknown option: $1"
            exit 1
            ;;
    esac
done

# Print the parsed options
echo "Input file: $INPUT_FILE"
echo "Output file: $OUTPUT_FILE"

你可能感兴趣的:(demo程序,bash,linux,开发语言,shell)