< OS 有关 > BaiduPCS-Go 程序的菜单脚本 Script: BaiduPCS-Go.Menu.sh (bdgo.sh) updated on 25Sep.25 使用 cookies登录

该文章已生成可运行项目,

更新原因:

旧的脚本,文章链接:< OS 有关> BaiduPCS-Go 程序的 菜单脚本 Script: BaiduPCS-Go.Menu.sh (bdgo.sh)

前几天用 bdgo 脚本上传文件时报:

“获取用户uk错误, 请确保登录信息包含了STOKEN, 获取UK: 遇到错误, 远端服务器返回错误, 代码: 2, 消息: 请稍后再试, 或更换保存路径”

今天有时间读了一下 read.me 看到:

试了用 bduss+stoken

还是报错,开了 issue :https://github.com/qjfoidnh/BaiduPCS-Go/issues/454https://github.com/qjfoidnh/BaiduPCS-Go/issues/454

建议使用 cookie:

今天算是完整看了一篇使用方法,再一次修改脚本,用 cookies 替代 BDUSS+STOKEN

 参考来自这个部分:

需要的工具:

Chrome/EDGE extension:  get cookies.txt 

在浏览:pan.baidu.com 后,可以用这个插件导出 netscape 格式的 cookie 文件,虽然脚本上多几行,比在 cookie 记录中捡垃圾简单。

更新后的script: bdgo.sh

1. 使用方法

  • CREDENTIALS_FILE=文件 这里是 上面插件导出的 cookies 内容,全部复制到这里。 
  • 默认下载目录 自己调整
  • 默认上传目录 同上

2. 演示

用 dd 生成一个 20mb 文件,见下图在 /root/20mb, 上传到 狗渡 网盘:

可以看到,文件存在于网盘目录:/shared/20mb

3. bash code: bdgo.sh

复制到 bash shell, 会创建脚本文件: /usr/local/bin/bdgo.sh, 并添加执行权力,最后创建 /usr/bin/bdgo linked to 脚本。

cat <<'EOF' > /usr/local/bin/bdgo.sh
#!/bin/bash
# Created by Dave on 28Jan.2025 
# History --
# 0.1 To use the manual structure as https://github.com/qjfoidnh/BaiduPCS-Go/blob/main/README.md listed. 
# 0.4 on 11Jan.2025 Modified file_operations() to avoid .folder 
# 0.5 on 25Sep.2025 Added STOKEN support for new authentication requirements
# 0.6 on 25Sep.2025 Changed authentication method from BDUSS+STOKEN to cookies
# 0.7 on 25Sep.2025 Added Netscape cookie file format support

# Credentials file (now supports Netscape cookie file format):
CREDENTIALS_FILE="/root/scripts/bd.BDUSS"

# Default download directory
DEFAULT_DOWNLOAD_DIR="/root/download"

# Default upload directory
DEFAULT_UPLOAD_DIR="/shared/"

# Clear screen
clear_screen() {
    clear
}

# Pause function
pause() {
    echo
    read -p "按回车键继续..." key
}

# Read credentials (now supports cookies format and Netscape cookie file)
read_credentials() {
    if [ ! -f "$CREDENTIALS_FILE" ]; then
        echo "Error: Credentials file not found: $CREDENTIALS_FILE"
        echo "Please create the file with cookies format or Netscape cookie file"
        echo "Example: BDUSS=...; STOKEN=...; PSTM=...; other_cookies=..."
        return 1
    fi
    
    # Check if file is Netscape cookie file format
    if grep -q "# Netscape HTTP Cookie File" "$CREDENTIALS_FILE"; then
        echo "Detected Netscape cookie file format"
        echo "Converting to cookies string..."
        
        # Extract cookies from Netscape format and convert to cookies string
        COOKIES=""
        while IFS=$'\t' read -r domain flag path secure expiry name value; do
            # Skip comments and empty lines
            [[ "$domain" =~ ^# ]] && continue
            [[ -z "$domain" ]] && continue
            
            # Only include cookies for baidu domains
            if [[ "$domain" =~ baidu\.com ]] || [[ "$domain" =~ pan\.baidu\.com ]]; then
                if [ -n "$name" ] && [ -n "$value" ]; then
                    COOKIES="${COOKIES}${name}=${value}; "
                fi
            fi
        done < "$CREDENTIALS_FILE"
        
        # Remove trailing space and semicolon
        COOKIES=$(echo "$COOKIES" | sed 's/; $//')
        
        if [ -n "$COOKIES" ]; then
            echo "✓ Converted Netscape cookies to string format"
            echo "✓ COOKIES: ${COOKIES:0:100}..."
            return 0
        else
            echo "❌ No valid cookies found in Netscape file"
            return 1
        fi
        
    # Check if file contains cookies format  
    elif grep -q "BDUSS=" "$CREDENTIALS_FILE" && grep -q ";" "$CREDENTIALS_FILE"; then
        # Standard cookies format: BDUSS=value; STOKEN=value; other=value;
        COOKIES=$(grep -v "^#" "$CREDENTIALS_FILE" | grep -v "^$" | head -1 | tr -d '\r\n')
        echo "Detected standard cookies format"
        echo "✓ COOKIES: ${COOKIES:0:50}..."
        return 0
        
    elif grep -q "STOKEN=" "$CREDENTIALS_FILE" && grep -q "BDUSS=" "$CREDENTIALS_FILE" && ! grep -q ";" "$CREDENTIALS_FILE"; then
        # Legacy BDUSS=value / STOKEN=value format - convert to cookies
        BDUSS=$(grep "^BDUSS=" "$CREDENTIALS_FILE" | cut -d'=' -f2- | tr -d '\r\n')
        STOKEN=$(grep "^STOKEN=" "$CREDENTIALS_FILE" | cut -d'=' -f2- | tr -d '\r\n')
        COOKIES="BDUSS=$BDUSS; STOKEN=$STOKEN;"
        echo "Detected legacy BDUSS+STOKEN format, converting to cookies format"
        echo "✓ Converted to cookies format"
        return 0
        
    elif ! grep -q "=" "$CREDENTIALS_FILE" && ! grep -q "# Netscape" "$CREDENTIALS_FILE"; then
        # Very old format: BDUSS only - convert to cookies
        BDUSS=$(cat "$CREDENTIALS_FILE" | tr -d '\r\n')
        COOKIES="BDUSS=$BDUSS;"
        echo "Detected very old BDUSS-only format, converting to cookies format"
        echo "⚠ Warning: No STOKEN found, some functions may be limited"
        return 0
    else
        echo "Error: Invalid credentials file format"
        echo "Supported formats:"
        echo "1. Netscape cookie file (exported from browser)"
        echo "2. Cookies string: BDUSS=value; STOKEN=value; ..."
        echo "3. Legacy: BDUSS=value (separate lines)"
        return 1
    fi
}

get_remote_file_info() {
    local path="$1"
    BaiduPCS-Go meta "$path"
}

get_local_file_info() {
    local path="$1"
    if [ -f "$path" ]; then
        # Use stat -c for Linux, -f for BSD/macOS
        if stat -c %s "$path" >/dev/null 2>&1; then
            echo "File size: $(stat -c %s "$path")"
            echo "Modified time: $(stat -c %Y "$path")"
        else
            echo "File size: $(stat -f %z "$path")"
            echo "Modified time: $(stat -f %m "$path")"
        fi
        return 0
    else
        return 1
    fi
}

check_before_download() {
    local remote_path="$1"
    local local_path="$2"
    
    # Check for excluded directories
    if ! check_excluded_directories "$remote_path"; then
        return 1
    fi
    
    echo "Checking remote file: $remote_path"
    # Check if remote file exists
    if ! get_remote_file_info "$remote_path" > /dev/null 2>&1; then
        echo "Error: Remote file does not exist"
        return 1
    fi
    
    # If local file exists, check size
    if [ -f "$local_path" ]; then
        local remote_size=$(get_remote_file_info "$remote_path" | grep "文件大小" | awk '{print $2}')
        # Get local file size using appropriate stat command
        local local_size
        if stat -c %s "$local_path" >/dev/null 2>&1; then
            local_size=$(stat -c %s "$local_path")
        else
            local_size=$(stat -f %z "$local_path")
        fi
        
        echo "Remote file size: $remote_size"
        echo "Local file size: $local_size"
        
        if [ "$remote_size" = "$local_size" ]; then
            echo "File already exists and size matches, skipping download"
            return 1
        else
            echo "File exists but size differs, will re-download"
            return 0
        fi
    fi
    
    return 0
}

check_excluded_directories() {
    local path="$1"
    if [[ "$path" == *"/.git"* ]] || [[ "$path" == *"/.cache"* ]] || [[ "$path" =~ /\.[^/]*$ ]]; then
        echo "Warning: Path contains excluded directories (.git, .cache or directories starting with '.')"
        return 1
    fi
    return 0
}

check_before_upload() {
    local local_path="$1"
    local remote_path="$2"
    
    # Check for excluded directories
    if ! check_excluded_directories "$local_path"; then
        return 1
    fi
    
    echo "Checking local file: $local_path"
    # Check if local file exists
    if ! [ -f "$local_path" ]; then
        echo "Error: Local file does not exist"
        return 1
    fi
    
    # Get remote file info
    if get_remote_file_info "$remote_path" > /dev/null 2>&1; then
        # Get local file size using appropriate stat command
        local local_size
        if stat -c %s "$local_path" >/dev/null 2>&1; then
            local_size=$(stat -c %s "$local_path")
        else
            local_size=$(stat -f %z "$local_path")
        fi
        
        local remote_size=$(get_remote_file_info "$remote_path" | grep "文件大小" | awk '{print $2}')
        
        echo "Local file size: $local_size"
        echo "Remote file size: $remote_size"
        
        if [ "$remote_size" = "$local_size" ]; then
            echo "Remote file already exists and size matches, skipping upload"
            return 1
        else
            echo "Remote file exists but size differs, will re-upload"
            return 0
        fi
    fi
    
    return 0
}

show_main_menu() {
    clear_screen
    echo "=== BaiduPCS-Go 主菜单 ==="
    echo "1. 账号管理"
    echo "2. 文件操作" 
    echo "3. 分享/转存"
    echo "4. 回收站"
    echo "5. 系统设置"
    echo "0. 退出"
    echo "===================="
}

show_account_menu() {
    clear_screen
    echo "=== 账号管理 ==="
    echo "1. 使用凭证文件登录"
    echo "2. 切换账号"
    echo "3. 退出账号" 
    echo "4. 显示当前账号"
    echo "5. 显示账号列表"
    echo "6. 检查凭证文件格式"
    echo "7. 创建凭证文件模板"
    echo "8. 从cURL命令提取Cookie"
    echo "9. 返回主菜单"
    echo "0. 退出"
    echo "===================="
}

show_file_menu() {
    clear_screen
    echo "=== 文件操作 ==="
    echo "1. 列出文件"
    echo "2. 切换目录"
    echo "3. 下载文件/目录"
    echo "4. 上传文件/目录"
    echo "5. 创建目录"
    echo "6. 删除文件/目录"
    echo "7. 复制文件/目录"
    echo "8. 移动/重命名"
    echo "9. 返回主菜单"
    echo "0. 退出"
    echo "===================="
}

show_share_menu() {
    clear_screen
    echo "=== 分享/转存 ==="
    echo "1. 分享文件/目录"
    echo "2. 列出已分享"
    echo "3. 取消分享"
    echo "4. 转存分享文件"
    echo "9. 返回主菜单"
    echo "0. 退出"
    echo "===================="
}

show_recycle_menu() {
    clear_screen
    echo "=== 回收站 ==="
    echo "1. 列出回收站"
    echo "2. 还原文件/目录"
    echo "3. 清空回收站"
    echo "9. 返回主菜单"
    echo "0. 退出"
    echo "===================="
}

show_config_menu() {
    clear_screen
    echo "=== 系统设置 ==="
    echo "1. 显示配置"
    echo "2. 修改配置"
    echo "3. 恢复默认配置"
    echo "9. 返回主菜单"
    echo "0. 退出"
    echo "===================="
}

# Create credentials file template
create_credentials_template() {
    clear_screen
    echo "=== 创建凭证文件模板 ==="
    echo "Creating credentials file template: $CREDENTIALS_FILE"
    
    if [ -f "$CREDENTIALS_FILE" ]; then
        echo "Warning: Credentials file already exists!"
        read -p "Do you want to overwrite the existing file? (y/n): " confirm
        if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
            echo "Operation cancelled"
            return
        fi
    fi
    
    # Create directory if it doesn't exist
    mkdir -p "$(dirname "$CREDENTIALS_FILE")"
    
    # Create template file with multiple format options
    cat > "$CREDENTIALS_FILE" << 'EOF'
# BaiduPCS-Go Credentials File - Multiple Format Support
# Choose ONE of the following methods:

# METHOD 1: Standard Cookies String Format (Recommended)
# Paste your complete cookies string from browser here (all values in one line)
# BAIDUID=your_baiduid_value; BIDUPSID=your_bidupsid_value; PSTM=your_pstm_value; BDUSS=your_bduss_value; STOKEN=your_stoken_value; PANPSC=your_panpsc_value;

# METHOD 2: Netscape Cookie File Format (Easiest)
# You can directly paste the exported cookie file from browser extensions
# The script will automatically convert Netscape format to cookies string
# 
# To export cookies in Netscape format:
# 1. Install "cookies.txt" browser extension
# 2. Go to https://pan.baidu.com and login
# 3. Use extension to export cookies for pan.baidu.com
# 4. Replace this entire file content with the exported content
# 
# Example Netscape format:
# # Netscape HTTP Cookie File
# .baidu.com	TRUE	/	FALSE	1779794376	BDUSS	your_bduss_value
# .baidu.com	TRUE	/	FALSE	1761415338	STOKEN	your_stoken_value
# ... (more cookie lines)

# METHOD 3: Manual Cookie Collection
# How to get complete cookies string manually:
# 1. Login to https://pan.baidu.com
# 2. Open Developer Tools (F12) -> Network tab  
# 3. Refresh the page or perform any action
# 4. Click on any request to pan.baidu.com
# 5. In Request Headers, find "Cookie:" line
# 6. Copy the entire cookie string after "Cookie: "
# 7. Paste it above (uncomment and replace METHOD 1 line)

# IMPORTANT: Remove all comment lines above and keep only your cookie data!
EOF
    
    echo "✓ Credentials file template created: $CREDENTIALS_FILE"
    echo ""
    echo "Please edit this file using ONE of these methods:"
    echo "  1. Paste cookies string: BDUSS=...; STOKEN=...; ..."
    echo "  2. Paste Netscape cookie file (easiest with browser extensions)"
    echo "  3. Use browser's Developer Tools to copy Cookie header"
    echo ""
    echo "Tip: Method 2 (Netscape format) is easiest with 'cookies.txt' extension"
    echo "Note: Do not share this file, it contains your login credentials!"
}

# Check credentials file format
check_credentials_format() {
    clear_screen
    echo "=== 检查凭证文件格式 ==="
    
    if [ ! -f "$CREDENTIALS_FILE" ]; then
        echo "❌ Credentials file not found: $CREDENTIALS_FILE"
        echo ""
        read -p "Do you want to create a credentials file template? (y/n): " create_template
        if [ "$create_template" = "y" ] || [ "$create_template" = "Y" ]; then
            create_credentials_template
        fi
        return
    fi
    
    echo "📁 Credentials file: $CREDENTIALS_FILE"
    echo "📄 File content preview:"
    echo "----------------------------------------"
    
    # Detect file format
    if grep -q "# Netscape HTTP Cookie File" "$CREDENTIALS_FILE"; then
        echo "FORMAT: Netscape Cookie File detected"
        echo ""
        
        # Count cookies and show sample
        cookie_count=$(grep -v "^#" "$CREDENTIALS_FILE" | grep -v "^$" | grep -c "baidu.com")
        echo "Total cookies for baidu.com domains: $cookie_count"
        echo ""
        echo "Sample cookies:"
        
        # Show key cookies with partial values
        while IFS=$'\t' read -r domain flag path secure expiry name value; do
            [[ "$domain" =~ ^# ]] && continue
            [[ -z "$domain" ]] && continue
            
            if [[ "$domain" =~ baidu\.com ]] && [[ -n "$name" ]]; then
                if [[ "$name" == "BDUSS" ]] || [[ "$name" == "STOKEN" ]] || [[ "$name" == "BIDUPSID" ]] || [[ "$name" == "PSTM" ]]; then
                    if [ ${#value} -gt 20 ]; then
                        echo "  $name=${value:0:20}... (${#value} chars)"
                    else
                        echo "  $name=$value"
                    fi
                fi
            fi
        done < "$CREDENTIALS_FILE"
        
    else
        # Show file content but hide sensitive information  
        while IFS= read -r line; do
            # Skip comments and empty lines for display
            if [[ "$line" =~ ^# ]] || [[ -z "$line" ]]; then
                echo "$line"
                continue
            fi
            
            # Check for cookies format with multiple values
            if [[ "$line" =~ ";" ]] && [[ "$line" =~ "=" ]]; then
                # Count cookie values
                cookie_count=$(echo "$line" | grep -o "=" | wc -l)
                echo "COOKIES: ${line:0:100}... (${#line} chars total, $cookie_count values)"
            elif [[ "$line" =~ ^BDUSS= ]]; then
                bduss_value="${line#BDUSS=}"
                if [ ${#bduss_value} -gt 10 ]; then
                    echo "BDUSS=${bduss_value:0:10}...(${#bduss_value} chars)"
                else
                    echo "$line"
                fi
            elif [[ "$line" =~ ^STOKEN= ]]; then
                stoken_value="${line#STOKEN=}"
                if [ ${#stoken_value} -gt 10 ]; then
                    echo "STOKEN=${stoken_value:0:10}...(${#stoken_value} chars)"
                else
                    echo "$line"
                fi
            else
                echo "$line"
            fi
        done < "$CREDENTIALS_FILE"
    fi
    
    echo "----------------------------------------"
    
    # Validate format and check required cookies
    echo ""
    echo "🔍 Format validation:"
    
    if grep -q "# Netscape HTTP Cookie File" "$CREDENTIALS_FILE"; then
        echo "✅ FORMAT: Netscape cookie file format"
        
        # Check for required cookies in Netscape format
        if grep -q $'\t''BDUSS'$'\t' "$CREDENTIALS_FILE"; then
            echo "✅ BDUSS: Found in Netscape file"
        else
            echo "❌ BDUSS: Missing (required)"
        fi
        
        if grep -q $'\t''STOKEN'$'\t' "$CREDENTIALS_FILE"; then
            echo "✅ STOKEN: Found in Netscape file"
        else
            echo "⚠️ STOKEN: Missing (may affect functionality)"
        fi
        
        if grep -q $'\t''BIDUPSID'$'\t' "$CREDENTIALS_FILE"; then
            echo "✅ BIDUPSID: Found in Netscape file"
        else
            echo "ℹ️ BIDUPSID: Not found (recommended)"
        fi
        
        if grep -q $'\t''PSTM'$'\t' "$CREDENTIALS_FILE"; then
            echo "✅ PSTM: Found in Netscape file"
        else
            echo "ℹ️ PSTM: Not found (recommended)"
        fi
        
        # Count total cookies for baidu domains
        total_cookies=$(grep -v "^#" "$CREDENTIALS_FILE" | grep -v "^$" | grep -c "baidu.com")
        echo "ℹ️ Total baidu.com cookies: $total_cookies"
        
        if [ "$total_cookies" -ge 5 ]; then
            echo "✅ COMPLETENESS: Good (sufficient cookies for authentication)"
        elif [ "$total_cookies" -ge 2 ]; then
            echo "⚠️ COMPLETENESS: Basic (may work but recommend more cookies)"
        else
            echo "❌ COMPLETENESS: Insufficient (need more cookies)"
        fi
        
    elif [[ $(grep -v "^#" "$CREDENTIALS_FILE" | grep -v "^$" | head -1) =~ ";" ]] && [[ $(grep -v "^#" "$CREDENTIALS_FILE" | grep -v "^$" | head -1) =~ "=" ]]; then
        # Standard cookies format validation (same as before)
        cookies_line=$(grep -v "^#" "$CREDENTIALS_FILE" | grep -v "^$" | head -1)
        echo "✅ FORMAT: Standard cookies format detected"
        
        # Check for required cookies
        if [[ "$cookies_line" =~ BDUSS= ]]; then
            echo "✅ BDUSS: Found"
        else
            echo "❌ BDUSS: Missing (required)"
        fi
        
        if [[ "$cookies_line" =~ STOKEN= ]]; then
            echo "✅ STOKEN: Found"
        else
            echo "⚠️ STOKEN: Missing (may affect functionality)"
        fi
        
        if [[ "$cookies_line" =~ BIDUPSID= ]]; then
            echo "✅ BIDUPSID: Found"
        else
            echo "ℹ️ BIDUPSID: Not found (recommended)"
        fi
        
        if [[ "$cookies_line" =~ PSTM= ]]; then
            echo "✅ PSTM: Found"
        else
            echo "ℹ️ PSTM: Not found (recommended)"
        fi
        
        # Count total cookies
        cookie_count=$(echo "$cookies_line" | grep -o "=" | wc -l)
        echo "ℹ️ Total cookies: $cookie_count"
        
        if [ "$cookie_count" -ge 5 ]; then
            echo "✅ COMPLETENESS: Good (sufficient cookies for authentication)"
        elif [ "$cookie_count" -ge 2 ]; then
            echo "⚠️ COMPLETENESS: Basic (may work but recommend adding more cookies)"
        else
            echo "❌ COMPLETENESS: Insufficient (need more cookies)"
        fi
        
    elif grep -q "^BDUSS=" "$CREDENTIALS_FILE" && grep -q "^STOKEN=" "$CREDENTIALS_FILE"; then
        echo "⚠️ LEGACY: Old BDUSS+STOKEN format detected"
        echo "   Script will convert this automatically"
    elif grep -q "BDUSS" "$CREDENTIALS_FILE"; then
        echo "⚠️ OLD: Very old format detected"
        echo "   Recommend upgrading to Netscape cookie file or cookies string"
    else
        echo "❌ ERROR: Invalid format detected"
        echo "   Supported formats:"
        echo "   1. Netscape cookie file (recommended)"
        echo "   2. Cookies string format"
    fi
}

# Extract cookies from cURL command
extract_cookies_from_curl() {
    clear_screen
    echo "=== 从cURL命令提取Cookie ==="
    echo "Instructions:"
    echo "1. Go to https://pan.baidu.com and login"
    echo "2. Open Developer Tools (F12) -> Network tab"
    echo "3. Refresh page or perform any action"
    echo "4. Right-click any request -> Copy -> Copy as cURL (bash)"
    echo "5. Paste the cURL command below"
    echo ""
    
    echo "Please paste your cURL command (press Enter twice when done):"
    echo "----------------------------------------"
    
    # Read multi-line input
    curl_command=""
    while IFS= read -r line; do
        if [[ -z "$line" ]]; then
            break
        fi
        curl_command+="$line "
    done
    
    echo "----------------------------------------"
    
    # Extract cookie from cURL command
    if [[ "$curl_command" =~ -H[[:space:]]+[\'\"]*Cookie:[[:space:]]*([^\'\"]*) ]]; then
        cookies="${BASH_REMATCH[1]}"
        # Clean up the cookies string
        cookies=$(echo "$cookies" | sed "s/' -H.*//g" | sed 's/".*//g' | tr -d '\n\r')
        
        echo ""
        echo "✅ Extracted cookies:"
        echo "----------------------------------------"
        echo "$cookies"
        echo "----------------------------------------"
        echo ""
        
        read -p "Do you want to save these cookies to $CREDENTIALS_FILE? (y/n): " save_cookies
        if [ "$save_cookies" = "y" ] || [ "$save_cookies" = "Y" ]; then
            # Backup existing file
            if [ -f "$CREDENTIALS_FILE" ]; then
                cp "$CREDENTIALS_FILE" "$CREDENTIALS_FILE.backup.$(date +%s)"
                echo "✓ Backed up existing credentials file"
            fi
            
            # Create directory if needed
            mkdir -p "$(dirname "$CREDENTIALS_FILE")"
            
            # Save cookies to file
            cat > "$CREDENTIALS_FILE" << EOF
# BaiduPCS-Go Credentials - Extracted from cURL on $(date)
$cookies
EOF
            echo "✅ Cookies saved to $CREDENTIALS_FILE"
            echo ""
            echo "You can now use option 1 to login with these cookies!"
        fi
    else
        echo "❌ No Cookie header found in the cURL command"
        echo "Please make sure you copied a complete cURL command that includes cookies"
        echo ""
        echo "The command should contain something like:"
        echo "  -H 'Cookie: BDUSS=...; STOKEN=...; ...'"
    fi
}

account_operations() {
    while true; do
        show_account_menu
        read -p "请选择操作 [0-9]: " choice
        case $choice in
            1) # Login using credentials file
                clear_screen
                echo "Reading login credentials from file: $CREDENTIALS_FILE"
                if read_credentials; then
                    echo ""
                    echo "Logging in with cookies..."
                    BaiduPCS-Go login -cookies="$COOKIES"
                else
                    echo "Login failed: Unable to read credentials"
                fi
                pause
                ;;
            2) # Switch account 
                clear_screen
                BaiduPCS-Go su
                pause
                ;;
            3) # Logout account
                clear_screen
                BaiduPCS-Go logout
                pause
                ;;
            4) # Show current account
                clear_screen
                BaiduPCS-Go who
                pause
                ;;
            5) # Show account list
                clear_screen
                BaiduPCS-Go loglist
                pause
                ;;
            6) # Check credentials file format
                check_credentials_format
                pause
                ;;
            7) # Create credentials file template
                create_credentials_template
                pause
                ;;
            8) # Extract cookies from cURL command
                extract_cookies_from_curl
                pause
                ;;
            9) # Return to main menu
                return
                ;;
            0) # Exit
                exit 0
                ;;
            *)
                echo "Invalid selection"
                pause
                ;;
        esac
    done
}

file_operations() {
    while true; do
        show_file_menu
        read -p "请选择操作 [0-9]: " choice
        case $choice in
            1) # List files
                clear_screen
                read -p "请输入要列出的目录路径 [默认当前目录]: " path
                if [ -z "$path" ]; then
                    BaiduPCS-Go ls
                else
                    BaiduPCS-Go ls "$path"
                fi
                pause
                ;;
            2) # Change directory
                clear_screen
                read -p "请输入要切换到的目录路径: " path
                BaiduPCS-Go cd "$path"
                pause
                ;;
            3) # Download files/directories
                clear_screen
                read -p "请输入要下载的文件/目录路径: " path
                read -p "请输入本地保存路径 [默认当前目录]: " local_path
                
                # If local path not specified, use filename
                if [ -z "$local_path" ]; then
                    local_path="$(basename "$path")"
                fi
                
                # Ensure we have credentials before downloading
                if ! read_credentials; then
                    echo "Error: Cannot read credentials, download cancelled"
                    pause
                    continue
                fi
                
                # Check if logged in, if not, login first
                if ! BaiduPCS-Go who >/dev/null 2>&1; then
                    echo "Not logged in, logging in first..."
                    BaiduPCS-Go login -cookies="$COOKIES"
                fi
                
                if check_before_download "$path" "$local_path"; then
                    echo "Starting download..."
                    BaiduPCS-Go download "$path"
                fi
                pause
                ;;
            4) # Upload files/directories
                clear_screen
                read -p "请输入要上传的本地文件/目录路径 [Default:/root/scripts/youtube-dl/video_downloads]: " local_path
                if [ -z "$local_path" ]; then
                    local_path="/root/scripts/youtube-dl/video_downloads"
                fi
                
                read -p "请输入要上传到的网盘目录路径 [Default:$DEFAULT_UPLOAD_DIR]: " remote_path
                if [ -z "$remote_path" ]; then
                    remote_path="$DEFAULT_UPLOAD_DIR"
                fi
                
                read -p "是否跳过文件检查直接上传? (y/n) [默认n]: " skip_check
                
                # Ensure we have credentials before uploading
                if ! read_credentials; then
                    echo "Error: Cannot read credentials, upload cancelled"
                    pause
                    continue
                fi
                
                # Check if logged in, if not, login first
                if ! BaiduPCS-Go who >/dev/null 2>&1; then
                    echo "Not logged in, logging in first..."
                    BaiduPCS-Go login -cookies="$COOKIES"
                fi
                
                # Build complete remote path
                if [ -f "$local_path" ]; then
                    if [ "$skip_check" = "y" ] || [ "$skip_check" = "Y" ]; then
                        echo "Skipping file check, starting upload..."
                        BaiduPCS-Go upload "$local_path" "$remote_path"
                    else
                        remote_file="$remote_path/$(basename "$local_path")"
                        if check_before_upload "$local_path" "$remote_file"; then
                            echo "Starting upload..."
                            BaiduPCS-Go upload "$local_path" "$remote_path"
                        fi
                    fi
                else
                    # For directories, check if they contain excluded paths
                    if check_excluded_directories "$local_path"; then
                        echo "Starting directory upload..."
                        # Use find to exclude .git and .cache directories during upload
                        find "$local_path" -type f -not -path "*/\.*" -not -path "*/.git/*" -not -path "*/.cache/*" -exec BaiduPCS-Go upload {} "$remote_path" \;
                    fi
                fi
                pause
                ;;
            5) # Create directory
                clear_screen
                read -p "请输入要创建的目录路径: " path
                BaiduPCS-Go mkdir "$path"
                pause
                ;;
            6) # Delete files/directories
                clear_screen
                read -p "请输入要删除的文件/目录路径: " path
                BaiduPCS-Go rm "$path"
                pause
                ;;
            7) # Copy files/directories
                clear_screen
                read -p "请输入要复制的源文件/目录路径: " src
                read -p "请输入目标路径: " dst
                BaiduPCS-Go cp "$src" "$dst"
                pause
                ;;
            8) # Move/rename
                clear_screen
                read -p "请输入要移动/重命名的源文件/目录路径: " src
                read -p "请输入新路径: " dst
                BaiduPCS-Go mv "$src" "$dst"
                pause
                ;;
            9) # Return to main menu
                return
                ;;
            0) # Exit
                exit 0
                ;;
            *)
                echo "Invalid selection"
                pause
                ;;
        esac
    done
}

share_operations() {
    while true; do
        show_share_menu
        read -p "请选择操作 [0-9]: " choice
        case $choice in
            1) # Share files/directories
                clear_screen
                read -p "请输入要分享的文件/目录路径: " path
                BaiduPCS-Go share set "$path"
                pause
                ;;
            2) # List shared items
                clear_screen
                BaiduPCS-Go share list
                pause
                ;;
            3) # Cancel sharing
                clear_screen
                read -p "请输入要取消的分享ID: " share_id
                BaiduPCS-Go share cancel "$share_id"
                pause
                ;;
            4) # Save shared files
                clear_screen
                read -p "请输入分享链接: " link
                read -p "请输入提取码: " code
                BaiduPCS-Go transfer "$link" "$code"
                pause
                ;;
            9) # Return to main menu
                return
                ;;
            0) # Exit
                exit 0
                ;;
            *)
                echo "Invalid selection"
                pause
                ;;
        esac
    done
}

recycle_operations() {
    while true; do
        show_recycle_menu
        read -p "请选择操作 [0-9]: " choice
        case $choice in
            1) # List recycle bin
                clear_screen
                BaiduPCS-Go recycle list
                pause
                ;;
            2) # Restore files/directories
                clear_screen
                read -p "请输入要还原的文件/目录fs_id: " fs_id
                BaiduPCS-Go recycle restore "$fs_id"
                pause
                ;;
            3) # Empty recycle bin
                clear_screen
                echo "Warning: This operation will empty the recycle bin!"
                read -p "Are you sure you want to empty it? (y/n) " confirm
                if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
                    BaiduPCS-Go recycle delete -all
                fi
                pause
                ;;
            9) # Return to main menu
                return
                ;;
            0) # Exit
                exit 0
                ;;
            *)
                echo "Invalid selection"
                pause
                ;;
        esac
    done
}

config_operations() {
    while true; do
        show_config_menu
        read -p "请选择操作 [0-9]: " choice
        case $choice in
            1) # Show configuration
                clear_screen
                BaiduPCS-Go config
                pause
                ;;
            2) # Modify configuration
                clear_screen
                echo "Common configuration options:"
                echo "1) Set download directory: config set -savedir $DEFAULT_DOWNLOAD_DIR"
                echo "2) Set download parallel count: config set -max_parallel <number>"
                echo "3) Set simultaneous download file count: config set -max_download_load <number>"
                echo
                read -p "请输入完整的配置命令: " cmd
                BaiduPCS-Go $cmd
                pause
                ;;
            3) # Reset to default configuration
                clear_screen
                echo "Warning: This operation will restore all default configurations!"
                read -p "Are you sure you want to restore? (y/n) " confirm
                if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
                    BaiduPCS-Go config reset
                fi
                pause
                ;;
            9) # Return to main menu
                return
                ;;
            0) # Exit
                exit 0
                ;;
            *)
                echo "Invalid selection"
                pause
                ;;
        esac
    done
}

# Main loop
while true; do
    show_main_menu
    read -p "请选择操作 [0-5]: " choice
    case $choice in
        1) # Account management
            account_operations
            ;;
        2) # File operations
            file_operations
            ;;
        3) # Share/save operations
            share_operations
            ;;
        4) # Recycle bin operations
            recycle_operations
            ;;
        5) # System configuration
            config_operations
            ;;
        0) # Exit
            echo "Thank you for using, goodbye!"
            exit 0
            ;;
        *)
            echo "Invalid selection"
            pause
            ;;
    esac
done
EOF
chmod +x /usr/local/bin/bdgo.sh
ln -s /usr/local/bin/bdgo.sh /usr/bin/bdgo

本文章已经生成可运行项目
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值