切换用户后whoami打印用户的问题
问题:
为何第二个whoami打印的还是root?
root@localhost /]#
[root@localhost /]#
[root@localhost /]# more test.sh
#!/bin/bash
echo "current user is $(whoami)"
su - oracle <<!
echo "current user is $(whoami)"
whoami
[root@localhost /]# /test.sh
current user is root
current user is root
oracle
[root@localhost /]#
[root@localhost /]#
[root@localhost /]# more test.sh
#!/bin/bash
echo "current user is $(whoami)"
su - oracle <<!
echo "current user is $(whoami)"
whoami
[root@localhost /]# /test.sh
current user is root
current user is root
oracle
[root@localhost /]#
答:
最主要是当前shell去解释了 $() 的问题
1: 当 su -c '' 这样执行是没有这个问题的。 使用的是 单引号, 如果双引号还是一样的。
2: 当使用heredoc 执行时, 如果不转义$,就会先执行这里的 whoami 所以是 root, 如果转义,就不一样的。
[root@cAdmin tmp]# more re
#!/bin/bash
echo "A current user is $(whoami)"
su - uchip -c 'echo "B current user is $(whoami) $EUID $UID";whoami'
echo "--------------------"
su uchip <<EOF
echo "C current user is \$(whoami) \$EUID \$UID"
echo "--------------------"
whoami
echo \$USER $USER "===="
EOF
[root@cAdmin tmp]# bash re
A current user is root
B current user is uchip 500 500
uchip
--------------------
C current user is uchip 500 500
--------------------
uchip
uchip root ====