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 ==== 

     
  • 相关阅读:
    新浪SAE搭建项目
    PHP ReflectionClass
    自定义时间函数
    mysql 日期函数
    jquery之商城菜单
    jquery之行自加自减
    前端之拖动面板
    商城轮播图
    js之返回网页顶部
    js之搜索框
  • 原文地址:https://www.cnblogs.com/chenjunjie/p/4585091.html
Copyright © 2011-2022 走看看