Book Image

Linux Shell Scripting Cookbook - Third Edition

By : Clif Flynt, Sarath Lakshman, Shantanu Tushar
Book Image

Linux Shell Scripting Cookbook - Third Edition

By: Clif Flynt, Sarath Lakshman, Shantanu Tushar

Overview of this book

The shell is the most powerful tool your computer provides. Despite having it at their fingertips, many users are unaware of how much the shell can accomplish. Using the shell, you can generate databases and web pages from sets of files, automate monotonous admin tasks such as system backups, monitor your system's health and activity, identify network bottlenecks and system resource hogs, and more. This book will show you how to do all this and much more. This book, now in its third edition, describes the exciting new features in the newest Linux distributions to help you accomplish more than you imagine. It shows how to use simple commands to automate complex tasks, automate web interactions, download videos, set up containers and cloud servers, and even get free SSL certificates. Starting with the basics of the shell, you will learn simple commands and how to apply them to real-world issues. From there, you'll learn text processing, web interactions, network and system monitoring, and system tuning. Software engineers will learn how to examine system applications, how to use modern software management tools such as git and fossil for their own work, and how to submit patches to open-source projects. Finally, you'll learn how to set up Linux Containers and Virtual machines and even run your own Cloud server with a free SSL Certificate from letsencrypt.org.
Table of Contents (14 chapters)

Customizing bash with configuration files

Most commands you type on the command line can be placed in a special file, to be evaluated when you log in or start a new bash session. It's common to customize your shell by putting function definitions, aliases, and environment variable settings in one of these files.

Common commands to put into a configuration file include the following:

# Define my colors for ls 
LS_COLORS='no=00:di=01;46:ln=00;36:pi=40;33:so=00;35:bd=40;33;01' 
export LS_COLORS 
# My primary prompt 
PS1='Hello $USER'; export PS1 
# Applications I install outside the normal distro paths 
PATH=$PATH:/opt/MySpecialApplication/bin; export PATH 
# Shorthand for commands I use frequently 
function lc () {/bin/ls -C $* ; }

What customization file should I use?

Linux and Unix have several files that might hold customization scripts. These configuration files are divided into three camps—those sourced on login, those evaluated when an interactive shell is invoked, and files evaluated whenever a shell is invoked to process a script file.

How to do it...

These files are evaluated when a user logs into a shell:

/etc/profile, $HOME/.profile, $HOME/.bash_login, $HOME/.bash_profile /
Note that /etc/profile, $HOME/.profile and $HOME/.bash_profile may not be sourced if you log in via a graphical login manager. That's because the graphical window manager doesn't start a shell. When you open a terminal window, a shell is created, but it's not a login shell.

If a .bash_profile or .bash_login file is present, a .profile file will not be read.

These files will be read by an interactive shell such as a X11 terminal session or using ssh to run a single command like: ssh 192.168.1.1 ls /tmp.

/etc/bash.bashrc $HOME/.bashrc

Run a shell script like this:

$> cat myscript.sh  
#!/bin/bash 
echo "Running"

None of these files will be sourced unless you have defined the BASH_ENV environment variable:

$> export BASH_ENV=~/.bashrc 
$> ./myscript.sh

Use ssh to run a single command, as with the following:

ssh 192.168.1.100 ls /tmp

This will start a bash shell which will evaluate /etc/bash.bashrc and $HOME/.bashrc, but not /etc/profile or .profile.

Invoke a ssh login session, like this:

ssh 192.168.1.100

This creates a new login bash shell, which will evaluate the following:

/etc/profile 
/etc/bash.bashrc 
$HOME/.profile or .bashrc_profile
DANGER: Other shells, such as the traditional Bourne shell, ash, dash, and ksh, also read this file. Linear arrays (lists) and associative arrays, are not supported in all shells. Avoid using these in /etc/profile or $HOME/.profile.

Use these files to define non-exported items such as aliases desired by all users. Consider this example:

alias l "ls -l"
/etc/bash.bashrc /etc/bashrc

Use these files to hold personal settings. They are useful for setting paths that must be inherited by other bash instances. They might include lines like these:

CLASSPATH=$CLASSPATH:$HOME/MyJavaProject; export CLASSPATH
$HOME/.bash_login $HOME/.bash_profile $HOME/.profile
If .bash_login or .bash_profile are present, .profile will not be read. A .profile file may be read by other shells.

Use these files to hold your personal values that need to be defined whenever a new shell is created. Define aliases and functions here if you want them available in an X11 terminal session:

$HOME/.bashrc, /etc/bash.bashrc
Exported variables and functions are propagated to subordinate shells, but aliases are not. You must define BASH_ENV to be the .bashrc or .profile, where aliases are defined in order to use them in a shell script.

This file is evaluated when a user logs out of a session:

$HOME/.bash_logout

For example, if the user logs in remotely they should clear the screen when they log out.

$> cat ~/.bash_logout 
# Clear the screen after a remote login/logout. 
clear