Skip to main content
LPI Linux Essentials

3.3 Turning Commands into Scripts

By August 16, 2013September 12th, 2022No Comments

Turning commands into scriptsBeing able to type commands on the command line shell in Linux is your first step in turning commands into scripts. A shell script is just a series of commands that may be linked with keywords.Creating Linux scripts allows you to quickly carry out repetitive tasks effectively and reliably. A shell script is a text file with a bunch of commands and operators; so first we have to look at the text editors available to us at the command line.

Text Editors

First, we take a quick tour of vi, pico, nano and joe. These are all command line editors in Linux, if you are using the GUI then you may use editors like kate or gedit. The emphasis of the LPI training is at the command line and the CLI is always available to you and often quicker. Linux being Linux you always have the choice of tools to use. I would suggest trying them all and working with the editor that suits you. Think of it as trying on a pair of shoes, they need to be comfy and your editor needs to fit you.

Using Vi

Vi or Vi Improved (vim) is one of the classical editors in Linux and Unix. It may be difficult to use at first; however is very powerful and quick to use once mastered. I am sure most people only use a little of its functionality but that can be said of all editors really. Understanding the basics of this editor is going to be useful as most distributions are going to ship with this editor.

The first video linked here looks at the basics a vi / vim.

The second video linked here looks at some of the advanced features of vi and how we can add indents into a script.


Pico and Nano

Pico, nano and joe are all command line editors but with the addition of a basic menu system. This menu helps when you are starting but also can slow the process down and certainly do not have the features available in vi.

The video linked here looks at using nano on CentOS.

Writing Your First Script

When turning commands into scripts we often expect tp print Hello World . As a change, we can print the Process ID or PID that the script runs in using the variable $$. We will also learn that scripts will normally start with the shebang, identifying the command line shell to use when executing. This is a commented line, starting with a hash #, but is read when the script runs.

#!/bin/bash 
#!/bin/sh 
#!/bin/ksh

The above lines could be used as the shebang. The first being Bash , then the Bourne Shell and the final would be if we wanted to run the script in the Korn shell. Only one would be used per script and, if used, the shebang must be the first line of the script.

System Variables

Certain system variables exist for us to use. $$ represents the PID of the script, for long running scripts this may be useful to write out to a file so if we need to send a signal to end the script we know the process to send the signal to.


From the above graphic we can see the output from the command line, within a script we may use something line this:

#!/bin/bash
echo $$ > /var/run/script.pid

Executing the script, the file, script.pid, would contain the process id of the script. This would only be useful if the script were to be long lived. Other system variables we may use

  • $0 : The script name
  • $1 : The first argument passed to the script
  • $# : The number of arguments passed to the script
  • $* : The list of arguments passed to the script

Passing Arguments to a Script

Building up a script so that it may accept arguments we could write something similar to the following code:

#!/bin/bash
COUNT=$(wc -l $1)
echo $COUNT

Executing the script with the hosts files as and arguments would look like this:

scriptname.sh /etc/hosts

We would count the number of lines in the local hosts file and print the count out to the screen.

Conditional Statements

The previous code identifies an issue that we may have; what if the user input just /etc rather than a regular file. This is where we could add in IF or other conditional  statements:

#!/bin/bash
if [ -f $1]
 then 
 COUNT=$(wc -l $1)
 else
 echo “$1 is not a file”
 exit 53
fi
echo $COUNT

The script tests that the supplied argument is a regular file. If it is we can run the word count if it is not we echo out a warning and exit the script with an error code. We can now start to see the power of turning commands into scripts. This introduces the basics of shell scripting and the video follows to give you a demonstration: