Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash脚本未等待读取_Bash - Fatal编程技术网

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”bash
read
正在从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