A useful bash shell script that will only run a command as the root user.

Posted: August 26, 2013. At: 11:24 PM. This was 4 years ago. Post ID: 6167
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

This is a shell script that will only run if the user executes it as the root user. I got this tip here: http://www.cyberciti.biz/tips/shell-root-user-check-script.html.

#!/bin/bash
 
 
CMDROOT="yum update"
 
if [[ $EUID -ne 0 ]]; then
	echo
	echo "You must be a root user to run this command." 2>&1
	exit 1
else
	echo
	echo "The command you ordered is: \"$CMDROOT\" And it is about to run."
	echo
	exec $CMDROOT
fi

Here is some code that will create a folder if it does not exist.

#!/bin/bash
# Make a folder if it does not exist.
 
if [ -e mydir ]; then
	echo "The folder exists."
else
	mkdir mydir
fi

How to convert text to uppercase with the command-line. Simply use the tr command to transform the text into uppercase.

-{homer@neo } $ echo "Hello World" | tr a-z A-Z
HELLO WORLD

This is how to do this with environment variables.

|{~/Documents}-{Mon Aug 26 23:49:45}
-{homer@neo } $ echo ${LOGNAME^^}
HOMER

Do you want a DOS styled prompt for your Linux box? Then put this into your .bashrc file and that is what you will get.

PROMPT_COMMAND='export PWD_UPCASE="${PWD^^}"'
PS1='C:${PWD_UPCASE//\//\\}> '

No comments have been made. Use this form to start the conversation :)

Leave a Reply