mac 双击启动脚本

16次阅读
没有评论

 

这个错误是因为终端会话启动后立即结束了。这通常发生在直接双击运行脚本时。我来为您提供几种针对 zsh 的解决方案:

方案1:创建正确的 zsh 脚本

创建 ~/bin/fileserver 文件:

#!/bin/zsh

# 配置
SERVER_DIR="/Users/zhaochangqing/Documents/softLib/file"
DEFAULT_PORT="8033"

# 主函数
main() {
    local PORT=${1:-$DEFAULT_PORT}

    # 检查目录
    if [[ ! -d "$SERVER_DIR" ]]; then
        echo "错误: 目录不存在 - $SERVER_DIR"
        return 1
    fi

    # 检查 http-server
    if ! command -v http-server &> /dev/null; then
        echo "安装 http-server..."
        npm install -g http-server
    fi

    # 检查端口占用
    if lsof -Pi :$PORT -sTCP:LISTEN -t >/dev/null; then
        echo "端口 $PORT 已被占用,打开现有服务..."
        open "http://localhost:$PORT"
        return 0
    fi

    echo "🚀 启动HTTP文件服务器"
    echo "📁 目录: $SERVER_DIR"
    echo "🌐 端口: $PORT"
    echo "📢 服务地址: http://localhost:$PORT"
    echo "⏹️  按 Ctrl+C 停止服务器"
    echo "----------------------------------------"

    # 切换到目录并启动服务
    cd "$SERVER_DIR"
    http-server -p $PORT
}

# 运行主函数
main "$@"

设置执行权限并运行:

chmod +x ~/bin/fileserver
~/bin/fileserver

方案2:使用 Terminal 命令(推荐)

创建 start_fileserver.command 文件:

#!/bin/zsh

# 改变到脚本所在目录
cd "$(dirname "$0")"

# 配置
SERVER_DIR="/Users/zhaochangqing/Documents/softLib/file"
PORT="8033"

echo "🚀 启动HTTP文件服务器..."
echo "📁 目录: $SERVER_DIR"
echo "🌐 端口: $PORT"

# 检查目录
if [ ! -d "$SERVER_DIR" ]; then
    echo "❌ 错误: 目录不存在"
    echo "按任意键退出..."
    read -k1
    exit 1
fi

# 检查并安装 http-server
if ! command -v http-server &> /dev/null; then
    echo "📦 安装 http-server..."
    npm install -g http-server
fi

# 检查端口
if lsof -Pi :$PORT -sTCP:LISTEN -t >/dev/null; then
    echo "⚠️  端口已被占用,打开现有服务..."
    open "http://localhost:$PORT"
    echo "按任意键退出..."
    read -k1
    exit 0
fi

echo "✅ 启动服务器..."
echo "📢 服务地址: http://localhost:$PORT"
echo "----------------------------------------"

# 启动服务
cd "$SERVER_DIR"
http-server -p $PORT

# 服务停止后等待用户操作
echo ""
echo "服务器已停止,按任意键关闭窗口..."
read -k1

设置执行权限:

chmod +x start_fileserver.command

然后双击 start_fileserver.command 文件运行。

方案3:zsh 别名命令(最简单)

在 ~/.zshrc 中添加:

# HTTP服务器快捷命令
alias fileserver='cd /Users/zhaochangqing/Documents/softLib/file && http-server -p 8033'
alias fileserver-help='
echo "🚀 HTTP文件服务器命令:"
echo "  fileserver      - 启动文件服务器"
echo "  fileserver 8080 - 使用指定端口启动"
echo "  fileserver-stop - 停止文件服务器"
'

fileserver-stop() {
    local PORT=${1:-8033}
    echo "停止端口 $PORT 的HTTP服务器..."
    pkill -f "http-server.*-$PORT"
}

使用方式:

# 重新加载配置
source ~/.zshrc

# 启动服务器
fileserver

# 查看帮助
fileserver-help

# 停止服务器
fileserver-stop

方案4:使用 osascript 创建应用

创建 FileServer.app

#!/bin/zsh

# 创建应用目录结构
APP_DIR="$HOME/Applications/FileServer.app"
mkdir -p "$APP_DIR/Contents/MacOS"
mkdir -p "$APP_DIR/Contents/Resources"

# 创建启动脚本
cat > "$APP_DIR/Contents/MacOS/FileServer" << 'EOF'
#!/bin/zsh

SERVER_DIR="/Users/zhaochangqing/Documents/softLib/file"
PORT="8033"

# 检查目录
if [ ! -d "$SERVER_DIR" ]; then
    osascript -e 'display dialog "目录不存在: '"$SERVER_DIR"'" buttons {"确定"} default button 1 with icon stop'
    exit 1
fi

# 检查 http-server
if ! command -v http-server &> /dev/null; then
    osascript -e 'display dialog "需要安装 http-server,将在终端中安装..." buttons {"确定"} default button 1'
    open -a Terminal -e "npm install -g http-server"
fi

# 启动服务
osascript -e 'tell application "Terminal" to do script "cd '"$SERVER_DIR"' && http-server -p '"$PORT"'"'
sleep 2
open "http://localhost:$PORT"

osascript -e 'display dialog "HTTP文件服务器已启动!\n\n服务地址: http://localhost:'"$PORT"'\n目录: '"$SERVER_DIR"'" buttons {"确定"} default button 1'
EOF

chmod +x "$APP_DIR/Contents/MacOS/FileServer"

# 创建 Info.plist
cat > "$APP_DIR/Contents/Info.plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleExecutable</key>
    <string>FileServer</string>
    <key>CFBundleIdentifier</key>
    <string>com.yourcompany.fileserver</string>
    <key>CFBundleName</key>
    <string>FileServer</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
</dict>
</plist>
EOF

echo "✅ 已创建 FileServer.app"
echo "位置: $APP_DIR"

推荐使用方案2

方案2(.command文件)的优点:

  • 双击即可运行
  • 保持终端窗口打开
  • 显示详细的输出信息
  • 支持用户交互
  • 简单易用

使用方法:

  1. 创建 start_fileserver.command 文件
  2. 复制方案2的代码进去
  3. 在终端中运行:chmod +x start_fileserver.command
  4. 双击文件运行

这样就不会出现”session ended very soon”的错误了。

正文完
 0
bdspAdmin
版权声明:本站原创文章,由 bdspAdmin 于2025-11-28发表,共计3543字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)