Script Files in Linux

Before you start

Objectives: Learn what are scripts, how can we use existing ones and create new ones.

Prerequisites: no prerequisites.

Key terms: file, command, scripts, use, variable, script, example, profile, run


What are Scripts

Script is a command or multiple commands stored in a file. In Linux, the shell can read scripts and execute each command in the file, as if we were entering those commands trough the keyboard. Linux system comes with some predefined scripts, such as:

  • /etc/profile – a systemwide file which executes after a user logs in. It is used primarily to set environment variables.
  • /etc/bashrc – a systemwide file which executes after /etc/profile. It is is often executed by individual users’ .bashrc file. It is commonly used for aliases and functions.
  • ~/.bash_profile or ~/.bash_login or ~/.profile – the system searches for these files in the user’s home directory after the /etc/profile executes. These files are actually optional. Users can create settings specific to their systems with these files. ~/.bash_login only executes in the absence of ~/.bash_profile. ~/.profile only executes in the absence of the other two. The ~/ is a bash home directory variable. By using that variable, we don’t need to know where a user’s home directory is.
  • ~/.bashrc – executes when bash runs, often used to include /etc/bashrc in its scripts.
  • /etc/rc.d/rc.local – this file is related to the init process. Scripts in this file execute after all the entries in the /etc/inittab file have executed.

Users typically modify the /etc/bashrc file by adding functions and aliases. We should use a text editor to add aliases and functions to the script files. We can also use the “alias” and “function” commands to create scripts at the command line, but this way scripts are only active during our command line session. In addition to modifying scripts listed above, we can also write our own scripts and run them from the command line. The only thing we have to do before we run them is make them executable.

Syntax

To create an alias we use the following syntax: alias shortcut=’command(s)’. For example, alias ls=”ls -al” for default directory listing. To remove the alias we can use the “unalias shortcut” command. For example, unalias ls.

The sytax to create a function is function NAME() { command; }. For example, function hello() { echo “Hello there”; }. When we type in the hello in the command line, the function will run.

We can also run commands based on a condition. The syntax is if [statement]; then lines to run fi. We have to end the command with the “fi”. Example:

if ( is_file_exits “$1” )
then
echo “File found”
else
echo “File not found”
fi

In our scripts we will often use system variables. To use a variable in a script we simply add $ to the front of the variable we want to use. For example, the command “echo $PATH” will output the path environment variable.

When we run specific script file, the system creates special variables related to the script and which we can use. For example, variable $0 is the name of the script file. Other variables such as $1, $2, etc. are variables for parameters included in the command. For example, if we run the script like this: “/customscript Blue Yellow“, the $0 variable will contain the script name (customscript), variable $1 will contain first parameter (Blue), and $ variable $2 will contain second parameter (Yellow).