zoukankan      html  css  js  c++  java
  • 切换用户后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 /]#
     
    答:
    最主要是当前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 ==== 

     
  • 相关阅读:
    扩展Dijkstra
    CodeForces 1396E. Distance Matching
    大联盟2
    整式乘除法
    美国数学会众多教授推荐的本科&研究生代数几何经典书籍教材清单
    算法题——立方体的体对角线穿过多少个正方体?
    导数练习题
    导数压轴题
    集合
    著名数学家Ky Fan的故事
  • 原文地址:https://www.cnblogs.com/chenjunjie/p/4585091.html
Copyright © 2011-2022 走看看