Linux System Administration

When writing a bash scripts most of us by default use echo command as means to print to standard output stream. echo is easy to use and mostly it fits our needs without any problem. However, with simplicity very often comes limitation. This is also the case with echo command. Formatting an echo command output can be a nightmare and very often impossible task to do.

The solution to this can be a good old friend of all C/C++ the “printf” tool. printf can be just as easily implemented into a bash script is it is used with C/C++ programs. This article describes some basics of printf along with practical examples:

Read more...

Introduction

When managing a linux/unix operating system from the command line, users are interacting with the system via shell. This article will explore some of the basic features of the bash shell prompt. Default bash command line prompt on many linux systems does not have a color and display information about user's username, hostname and current working directory. As you well see in the following sections of this document this default settings can be easily changed by exporting a bash prompt PS{n} variables. Bash prompt can be modified to suit users needs and can display time, load, number of users using the system, uptime and more.

Read more...

Sometimes it is needed to check what user is executing the bash script and whether the user supplied all required arguments:

#!/bin/bash 
 
display_usage() { 
	echo "This script must be run with super-user privileges." 
	echo -e "\nUsage:\n$0 [arguments] \n" 
	} 
# if less than two arguments supplied, display usage 
	if [  $# -le 1 ] 
	then 
		display_usage
		exit 1
	fi 
 
# check whether user had supplied -h or --help . If yes display usage 
	if [[ ( $# == "--help") ||  $# == "-h" ]] 
	then 
		display_usage
		exit 0
	fi 
 
# display usage if the script is not run as root user 
	if [[ $USER != "root" ]]; then 
		echo "This script must be run as root!" 
		exit 1
	fi 
 
echo "All good !!!"

OUTPUT:

$ ./script.sh 1 
This script must be run with super-user privileges.

Usage: ./script.sh [arguments] 

$ su
Password: 
# ./script.sh 1 2
All good !!!

Page 7 of 73



Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top