linux 如何判断当前用户

 2023-09-13 阅读 27 评论 0

摘要:linux 中,如何判断当前用户是否是root 呢? 判断当前用户是否是root Shell代码 #----------------------------rootusercheck---------------------startif[`id-u`-ne0];thenecho"Pleasere-run${this_file}asroot."exit1fi#-------------------

linux 中,如何判断当前用户是否是root  呢?

判断当前用户是否是root

Shell代码  收藏代码
  1. #---------------------------- root user check ---------------------start     
  2. if [ `id -u` -ne 0 ]; then    
  3.     echo "Please re-run ${this_file} as root."    
  4.     exit 1     
  5. fi     
  6. #---------------------------- root user check ---------------------end    
  7.    
  8.   
  9. if [ x"$USER" = x"" ];then    
  10.     if  id |grep "^uid=0(root)">/dev/null ;then    
  11.         :     
  12.     else    
  13.         echo "Please rerun `basename $0` as root ."    
  14.         exit 1     
  15.     fi     
  16. else    
  17.     if [ ! x"$USER" = x"root" ];then    
  18.         echo "Please rerun `basename $0` as root ."    
  19.         exit 1     
  20.     fi     
  21. fi    
  22.    
  23.   
  24. #=============== check current user start ================     
  25. if [ ! $USER = root  ];then    
  26.     echo "Please rerun this script as root ."    
  27.     exit 1     
  28. fi     
  29. #=============== check current user end ================    
  30.    
  31.   
  32. #!/bin/sh     
  33. if  id |grep "^uid=0(root)">/dev/null ;then    
  34.     echo is  root     
  35. else    
  36.     echo not  root     
  37. fi     
  38.    
  39.   
  40. check_user(){     
  41.     if [ $UID -ne 0 ];then    
  42.         echo "Current user is not ROOT."    
  43.         echo "Please rerun the installer as Root user."    
  44.         exit 11     
  45.     fi     
  46. }      
  47.   
  48.    
 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/55437.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息