Bash脚本未等待读取
我试图以另一个用户的身份运行命令,并以该用户的身份读取输入。由于某种原因,脚本没有在Bash脚本未等待读取,bash,Bash,我试图以另一个用户的身份运行命令,并以该用户的身份读取输入。由于某种原因,脚本没有在read命令上暂停,我不确定原因。下面是脚本的一个示例: #!/bin/bash username='anthony' sudo -H -u "$username" bash << 'END_COMMAND' # Commands to be run as new user # ------------------------------- echo "#! Running as new use
read
命令上暂停,我不确定原因。下面是脚本的一个示例:
#!/bin/bash
username='anthony'
sudo -H -u "$username" bash << 'END_COMMAND'
# Commands to be run as new user
# -------------------------------
echo "#! Running as new user $USER..."
echo "#! Gathering setup information for $USER..."
echo -n "Enter your name and press [ENTER]: "
read name
echo -n "Enter your email and press [ENTER]: "
read email
END_COMMAND
echo "Done"
#/bin/bash
用户名class='anthony'
sudo-H-u“$username”bashread
正在从stdin读取,而bash
正在从sudo继承其stdin,后者来自herdeoc。如果你希望它来自其他地方,你需要明确。例如:
bash 3<&0 << 'END_COMMAND'
...
read <&3
...
bash3你能让这个脚本检查它是否以sudo运行,如果不是,用sudo执行它自己吗
#!/bin/bash
username='anthony'
if [[ -z "${SUDO_USER}" ]]; then
exec sudo -H -u "${username}" -- $0
fi
echo "
# Commands to be run as new user
# -------------------------------
"
echo "#! Running as new user $USER..."
echo "#! Gathering setup information for $USER..."
echo -n "Enter your name and press [ENTER]: "
read name
echo -n "Enter your email and press [ENTER]: "
read email
sudo -H -u "$username" bash << 'END_COMMAND'
...
read email < /dev/tty
...
#!/bin/bash
username='anthony'
if [[ -z "${SUDO_USER}" ]]; then
exec sudo -H -u "${username}" -- $0
fi
echo "
# Commands to be run as new user
# -------------------------------
"
echo "#! Running as new user $USER..."
echo "#! Gathering setup information for $USER..."
echo -n "Enter your name and press [ENTER]: "
read name
echo -n "Enter your email and press [ENTER]: "
read email