【Shell脚本练习】判断当前用户

判断当前用户是否为root,如果是提示为root用户,如果不是提示为普通用户


#!/bin/bash
#title: testus.sh
#author: orangleliu
#date: 2014-08-09
#desc: get current user, if it is root user, tell us it is super user or tell us is a common user

#================
#Function CheckUser
#================

CheckUser()
{
	check_user=`whoami`
	if [ "$check_user" == "root" ]
	then 
		echo "You are $check_user user"
		echo "You are a super amdin"
	else
		echo "You are $check_user user"
		echo "You are a common user"
	fi
}
#================
#Function Main
#================
Main()
{
	CheckUser
}

Main
执行结果

[orangle@localhost shell]$ bash testus.sh 
You are orangle user
You are a common user
[orangle@localhost shell]$ su - root
Password: 
[root@localhost ~]# bash /home/orangle/shell/testus.sh 
You are root user
You are a super amdin


一方面之前没有系统的写过shell脚本,一方面是通过小案例来学习和总结shell。而不是通过各种语法的学习然后再去写脚本。


参考教程

本文出自 orangleliu笔记本 博客,请务必保留此出处http://blog.csdn.net/orangleliu/article/details/38449613


你可能感兴趣的:(shell,用户,判断)