≡ Menu

Bash

The question - I want my bash shell script to be run only as root. If another user tries to run the script, the script should terminate. Is there a way to do that in a shell script? Bash has several environment variables that indicate which user is running the script: [root@server ~]# echo $USER [...]

{ 0 comments }

The Bash shell (bourne again shell) is a very powerful shell which is used by a large majority of Linux systems. It is available on all Linux distributions like CentOS, Debian and Ubuntu. The Bash shell can auto complete commands, users and hosts depending on how you use the command. For example, to complete file [...]

{ 0 comments }

Bash For and While Loops Examples

Bash (Bourne Again Shell) is a very popular shell for Linux based systems. It is the default shell on most web hosting servers because of its simplicity and eash of performing several shell tasks. Bash comes with an elaborate syntax of writing shell scripts, and one such functionality is its loops. A loop allows you [...]

{ 0 comments }

Linux: Random Number Generation in BASH

Question - I need pick random files from a directory of 10 files. How do I generate a random number between 0 and 9 (inclusive) in a BASH shell script on CentOS Linux? The shell BASH contains a random number generator called $RANDOM. The value of this variable is a random number between 0 and [...]

{ 0 comments }

Linux command to get IP address

Question - I need to find the IP address of my Linux machine. Is there a command that can do this on CentOS? The ifconfig command can be used to get all of the IP addresses of a Linux machine. The command can be used to get information about network adapters and their IP addresses. [...]

{ 0 comments }

BASH: Check if a directory or file exists

Question - I am running a Linux CentOS box. I need to write a bash shell script in which I have to check if a file or a directory exists and do something it it exists. Is there a BASH command that I can use to do this? There are bash in built commands that [...]

{ 0 comments }