【发布时间】:2016-12-12 05:06:24
【问题描述】:
请注意 FI 后代码中的备注! 为了通过浏览器创建用户帐户,我让 php shell_exec 执行 bash 脚本。即使我确定(通过检查/etc/shadow)用户名没有被使用,脚本也说它确实如此。 现在的脚本确实执行了 useradd 命令,并且用户名出现在 /etc/shadow 中。 貌似之前执行了useradd,然后检查用户是否存在?
php
$command = "sudo ./createclientcert.sh $userName $userPass";
if(shell_exec("$command echo $?") == 0){
echo 1;
}
脚本
#!/bin/bash
newclient () {
getent passwd $1 > /dev/null 2&>1
if [ $? -eq 0 ]; then
echo $?
else
useradd $1
echo $1:$2 | chpasswd
fi
# PLEASE TAKE NOTE!! funny thing is that when code (that had nothing to do with the account creation and was to be removed) that came after fi is in place it works well.
}
newclient "$1" "$2"
【问题讨论】:
-
getent passwd $1 > /dev/null 2&>1可能不正确。应该是getent passwd $1 >/dev/null 2>&1 -
另外,
shell_exec("$comm echo $?")不应该是shell_exec("$command echo $?")吗?? -
@sjsam $comm 是错字。对此感到抱歉。确实 2&>1 应该是 2>&1 当我快速 googlecheck 但我从另一个帖子复制并粘贴它时。
-
看起来有点奇怪。你期望最终的输出应该是什么?实际输出是多少? “脚本说它确实”是什么意思?
-
表示用户存在 $? = 0 并且 tgen 确实创建了它存在的帐户。但是请参阅我自己发布的答案,因为我认为这是不可复制的。
标签: php bash shell-exec