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

     
  • 相关阅读:
    VSFTP配置参数详解
    C语言---函数
    ios 学习计划
    读书笔记---金融学一<新国富论>
    读书笔记---人生规划一<斯坦福最受欢迎的人生规划课、像卡耐基一样经营人生、九型人格>
    网络基础
    swift中构造方法和Kvc
    swift中的懒加载
    private的用法
    extension
  • 原文地址:https://www.cnblogs.com/chenjunjie/p/4585091.html
Copyright © 2011-2022 走看看