#!/bin/bash
# Bash脚本测试示例
# 变量定义
SCRIPT_NAME="example.sh"
VERSION="1.0.0"
AUTHOR="Test Author"
# 函数定义
print_header() {
echo "====================================="
echo "$SCRIPT_NAME v$VERSION"
echo "Author: $AUTHOR"
echo "====================================="
}
print_usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -h, --help Show this help message"
echo " -v, --version Show version information"
echo " -n, --name NAME Set your name"
}
# 解析命令行参数
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
print_usage
exit 0
;;
-v|--version)
echo "$SCRIPT_NAME v$VERSION"
exit 0
;;
-n|--name)
NAME="$2"
shift
shift
;;
*)
echo "Unknown option: $1"
print_usage
exit 1
;;
esac
done
# 主程序
print_header
if [[ -n "$NAME" ]]; then
echo "Hello, $NAME! Welcome to the Bash example script."
else
echo "Hello, World! Welcome to the Bash example script."
fi
# 系统信息
echo -e "\nSystem Information:"
echo "Hostname: $(hostname)"
echo "OS: $(uname -s) $(uname -r)"
echo "User: $(whoami)"
echo "Current Directory: $(pwd)"
# 文件操作示例
echo -e "\nFile Operations:"
echo "Creating test file..."
touch test_file.txt
echo "Test content" > test_file.txt
echo "Test file created successfully"
echo "File content:"
cat test_file.txt
# 清理
rm test_file.txt
echo -e "\nScript completed successfully!"
"""