【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


版权声明:本文orangleliu(http://blog.csdn.net/orangleliu/)原创文章。文章转载申报。

原文地址:https://www.cnblogs.com/blfshiye/p/4803369.html