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

如何在wget命令中引用变量?执行脚本遇404错误求助

问题描述

有一条wget命令可以正常运行:

wget --header 'Authorization: token GIT_PERSONAL_ACESS_TOKEN' https://raw.githubusercontent.com/.../.../main/repo/packages/.../file.txt -P /opt/tomcat

我把令牌提取出来写了个shell脚本:

read -p "pls input the token:  " token
echo "print out my token: ${token}"
header="Authorization: token ${token}"
header=\'$header\'
echo "print out my header: ${header}"
wget --header "${header}" https://raw.githubusercontent.com/.../.../main/repo/packages/.../file.txt  -P /opt/tomcat 

但执行脚本后返回404 Not Found,wget版本是1.14,求解决办法。

解决思路
  • 问题根源:你在脚本里给header变量手动加了单引号(header=\'$header\'这一行),这会导致wget发送的请求头里包含多余的单引号。原命令里的单引号是shell用来包裹含空格的参数,避免被拆分,并不是请求头内容的一部分。GitHub收到带单引号的Authorization头,会识别为无效,所以返回404。
  • 修复方法:删掉header=\'$header\'这一行,直接使用原始的header变量即可。修复后的脚本如下:
read -p "pls input the token:  " token
echo "print out my token: ${token}"
header="Authorization: token ${token}"
echo "print out my header: ${header}"
wget --header "${header}" https://raw.githubusercontent.com/.../.../main/repo/packages/.../file.txt  -P /opt/tomcat 
  • 验证逻辑:修复后,wget发送的请求头和原命令完全一致,都是Authorization: token [你的令牌],GitHub能正确识别权限,就不会返回404了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 11:45:27