Shell Script Basics

Shell Script Basics

Shell script is an executable text file, which has a list of commands. It begins with a 'Shebang' interpreter directive. This Shebang is used to tell the kernel, which interpreter should be used to run the commands present in the file.

Uses

Scripts are widely used to automate processes such as ETL jobs, file backups, & archiving & general system administration tasks. It is used for nearly any computational tasks including application integration, plug-in & web application development.

Variables -

Variables are words that hold a value. This is called scalar variables. It can hold only 1 value at a time. To access a variable use $variableName.

Array variables - 1 variable holding multiple values.

Read only variables - The variable value cannot be changed. Eg. readonly variableName

Unsetting variables - Telling the shell, to not track the variable any more.

Compound commands -

List of commands both simple and complex separated by ; eg. ls ; date ; who Complex command = command + Arguments

Cron

Cron is a service which allows us to schedule tasks.

Crontab -> Files which has set of commands to be executed. These commands are called jobs. To see the list of crontab active use the command crontab -l.

Syntax -

cron job.gif

Quoting

Sometimes, its necessary to turn off the shell substitution and let each character stand for itself. Turning off the special meaning off a character is called quoting.

3 ways to do quoting -

  1. Using Backslash ()

  2. Using single quote (')

  3. Using double quote (")

Flow control -

The order in which commands execute in a shell script is called flow of the script.

Two Powerful flow control mechanics -

  1. if

  2. case

if is used for error checking, case is used to compare with multiple values and find the correct answer.