BASH - 在 shell 脚本上获取 UID 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39445968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:09:46  来源:igfitidea点击:

BASH - getting UID on shell script does not work

bashshell

提问by Eric Lee

Hi I have a question about bash.

嗨,我有一个关于 bash 的问题。

and I'm new to it.

我是新手。

I made a file named "test.sh" and its contents is

我制作了一个名为“test.sh”的文件,其内容是

#!/bin/bash
set -x
echo $UID
echo "$UID"
echo "$(id -u)"

and the result is blank!! nothing shows up

结果是空白!!什么都没有出现

However, when i just type "echo $UID" on terminal it shows "1011"

但是,当我在终端上输入“echo $UID”时,它显示“1011”

is there anything i missed for bash?

有什么我错过的 bash 吗?

Please help

请帮忙

UPDATED

更新

bash version is 4.3.11 and I typed "sh test.sh" to execute.

bash 版本是 4.3.11,我输入了“sh test.sh”来执行。

and the result is

结果是

+ echo

+ echo

+ id -u
+ echo 1011
1011

thanks!

谢谢!

采纳答案by SLePort

$UIDis a Bash variable that is not set under sh, that may be why it outputs blank lines.

$UID是一个未在 下设置的 Bash 变量sh,这可能就是它输出空行的原因。

Try bash test.shor make your script executable with chmod u+x test.sh, the program defined in shebang will then be used (/bin/bash)

尝试bash test.sh或使您的脚本可执行chmod u+x test.sh,然后将使用在 shebang 中定义的程序 ( /bin/bash)