Bash 从包装器二进制文件调用脚本时未设置变量
Bash variable not set when calling the script from wrapper binary
我有以下代码,
bash 脚本“tryme.sh”:
#!/usr/bin/env bash
PATH=$(/usr/bin/getconf PATH || /bin/kill $$)
PASS="hellthatrocks"
if [ ! -v "" ]; then
echo "no.."
echo "hell..."
exit 1
fi
if test "" = "$PASS" ; then
echo "yeah it is : $PASS"
else
echo "humm..."
fi
exit 0
和包装二进制文件“wrapper.c”:
#include <unistd.h>
int main(int arc, char** arv) {
char *argv[] = { "/bin/bash", "-p", "./tryme.sh", arv[1] , NULL };
execve(argv[0], argv, NULL);
return 0;
}
compiled gcc -o wrapper wrapper.c
export hellthatrocks="hellthatrocks"
问题是当我给出一个模式作为参数时,它在命令行中寻找定义的环境变量,例如:
./tryme.sh ${!h*}
它给了我,“是的,它是 hellthatrocks
”。
但是如果我从包装器中调用脚本,它找不到变量并给出“不...见鬼....”
./wrapper ${!h*}
出了什么问题?
谢谢,
What is going wrong ?
envp
可能在 linux 上被错误地指定为 NULL
,来自手册页:
On Linux, argv and envp can be specified as NULL. In both cases,
this has the same effect as specifying the argument as a pointer to
a list containing a single null pointer. Do not take advantage of
this nonstandard and nonportable misfeature!
你指定它给 NULL
,所以在子进程中环境是空的,所以导出的 hellthatrocks
变量不存在 - 所以检查 [ -v "" ]
和 =hellthatrocks
失败。
我有以下代码,
bash 脚本“tryme.sh”:
#!/usr/bin/env bash
PATH=$(/usr/bin/getconf PATH || /bin/kill $$)
PASS="hellthatrocks"
if [ ! -v "" ]; then
echo "no.."
echo "hell..."
exit 1
fi
if test "" = "$PASS" ; then
echo "yeah it is : $PASS"
else
echo "humm..."
fi
exit 0
和包装二进制文件“wrapper.c”:
#include <unistd.h>
int main(int arc, char** arv) {
char *argv[] = { "/bin/bash", "-p", "./tryme.sh", arv[1] , NULL };
execve(argv[0], argv, NULL);
return 0;
}
compiled gcc -o wrapper wrapper.c
export hellthatrocks="hellthatrocks"
问题是当我给出一个模式作为参数时,它在命令行中寻找定义的环境变量,例如:
./tryme.sh ${!h*}
它给了我,“是的,它是 hellthatrocks
”。
但是如果我从包装器中调用脚本,它找不到变量并给出“不...见鬼....”
./wrapper ${!h*}
出了什么问题?
谢谢,
What is going wrong ?
envp
可能在 linux 上被错误地指定为 NULL
,来自手册页:
On Linux, argv and envp can be specified as NULL. In both cases,
this has the same effect as specifying the argument as a pointer to a list containing a single null pointer. Do not take advantage of this nonstandard and nonportable misfeature!
你指定它给 NULL
,所以在子进程中环境是空的,所以导出的 hellthatrocks
变量不存在 - 所以检查 [ -v "" ]
和 =hellthatrocks
失败。