如何在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
相关产品推荐
相关产品推荐

