shell输出uid小于100的_Shell脚本的第一行空白:解释UID变量的行为

I have two very simple scripts, differing only by the presence of a blank first line:

$ cat test.bash

#!/bin/bash

echo ${UID}

$ cat test_blank.bash

#!/bin/bash

echo ${UID}

Now I run then, with and without nice:

$ ./test.bash

1060

$ ./test_blank.bash

1060

$ nice ./test.bash

1060

$ nice ./test_blank.bash

Please explain why, in the final case, the UID variable is unset. The behavior is the same when replacing nice with sudo or nohup.

解决方案

Observe:

$ bash test_blank.bash

1060

$ dash test_blank.bash

bash produces output but dash, which is the default sh on debian-like systems, does not. This is because bash sets UID but dash does not. (POSIX does not require a shell to set UID.) So, the question becomes which shell executes the script.

When bash sees ./test.sh, it (bash) runs the script. When another command, such as nice, receives the script as an argument and the script does not have a valid shebang as the first line, then the default shell, likely dash, is run.

If you want UID in dash, or any other shell that does not provide it, use the id command:

UID=$(id -u)

Finding out which shell is running a script

To see which shell is running a script, use:

$ cat test2.sh

#!/bin/bash

ps $$

echo UID=${UID}

Under bash:

$ ./test2.sh

PID TTY STAT TIME COMMAND

1652 pts/12 S+ 0:00 bash -rcfile .bashrc

UID=1060

If we invoke it using nice, by contrast, we can see that it is running under /bin/sh and the UID variable is not assigned:

$ nice test2.sh

PID TTY STAT TIME COMMAND

1659 pts/12 SN+ 0:00 /bin/sh test2.sh

UID=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值