* Tests [ 0 -eq $UID ] don't work as in bash
@ 2016-09-07 14:44 Andrey Voropaev
2016-09-07 15:55 ` Paul Smith
0 siblings, 1 reply; 3+ messages in thread
From: Andrey Voropaev @ 2016-09-07 14:44 UTC (permalink / raw)
To: dash
Hello!
I'm sorry if this is FAQ question, but googling didn't report it, so
I'm asking here.
In our scripts we often use constructs like
##############
if [ 0 -eq $UID ]
then
echo "This script must not be run by root user!"
exit 1
fi
#################
This works fine in bash, but dash fails with warning " [: -ne:
argument expected". Is this intended feature? How to work around it in
dash then?
Best regards
Andrei Voropaev
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Tests [ 0 -eq $UID ] don't work as in bash
2016-09-07 14:44 Tests [ 0 -eq $UID ] don't work as in bash Andrey Voropaev
@ 2016-09-07 15:55 ` Paul Smith
2016-09-07 20:02 ` Stephane Chazelas
0 siblings, 1 reply; 3+ messages in thread
From: Paul Smith @ 2016-09-07 15:55 UTC (permalink / raw)
To: Andrey Voropaev, dash
On Wed, 2016-09-07 at 16:44 +0200, Andrey Voropaev wrote:
> if [ 0 -eq $UID ]
The variable UID is not defined to be automatically set by the shell in
POSIX; having it set is a bash extension. dash doesn't set it
automatically for you.
You'll have to set it yourself:
UID=$(id -u)
Also you should quote variable references, in general:
if [ 0 -eq "$UID" ]
Be sure that if your scripts rely on bash-isms you start them with
#!/bin/bash and if you don't want them to rely on bash-isms, you start
them with #!/bin/sh.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Tests [ 0 -eq $UID ] don't work as in bash
2016-09-07 15:55 ` Paul Smith
@ 2016-09-07 20:02 ` Stephane Chazelas
0 siblings, 0 replies; 3+ messages in thread
From: Stephane Chazelas @ 2016-09-07 20:02 UTC (permalink / raw)
To: Paul Smith; +Cc: Andrey Voropaev, dash
2016-09-07 11:55:00 -0400, Paul Smith:
> On Wed, 2016-09-07 at 16:44 +0200, Andrey Voropaev wrote:
> > if [ 0 -eq $UID ]
>
> The variable UID is not defined to be automatically set by the shell in
> POSIX; having it set is a bash extension. dash doesn't set it
> automatically for you.
>
> You'll have to set it yourself:
>
> UID=$(id -u)
>
> Also you should quote variable references, in general:
>
> if [ 0 -eq "$UID" ]
>
> Be sure that if your scripts rely on bash-isms you start them with
> #!/bin/bash and if you don't want them to rely on bash-isms, you start
> them with #!/bin/sh.
[...]
Note that both zsh and bash have it, both from the start, both
copied from tcsh ($uid in tcsh).
In zsh, you can also set UID (and USERNAME and EUID, and GID...)
to change your uid (assuming you have the permission to do so).
$ sudo zsh -c 'id; UID=1000; id; UID=0'
uid=0(root) gid=0(root) groups=0(root)
uid=1000(chazelas) gid=0(root) groups=0(root)
zsh:1: failed to change user ID: operation not permitted
In bash, UID is read-only.
--
Stephane
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-09-07 20:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-07 14:44 Tests [ 0 -eq $UID ] don't work as in bash Andrey Voropaev
2016-09-07 15:55 ` Paul Smith
2016-09-07 20:02 ` Stephane Chazelas
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).