Command Line Arguments! Shell Scripting Part 3

Published on September 11, 2022 at 1:15 am by LEW

Introduction

In this installment, we will be looking at passing data to a shell script using command line arguments. These are arguments that are added when you execute a shell script, and can be pulled into the script.

We will be using a conditional operator (if then fi) to evaluate the number of arguments and give feedback. We will also be using a loop operator (for done) to output all the provided arguments.

There are several ways to get data into a shell script for processing. Using command line arguments is probably the quickest and easiest. In a future post we will explore having the script ask the user for data. But for today we will supply the arguments when we execute the script.

Adding Command Line Arguments

When you execute a shell script, if there is any space separated additional data, it is parsed into a array that is available while the script is running.

arg_count-v1.sh [argument1] [argument2] [argument3] ...

The information from the command line is then available within the script using automatically generated variables. Argument1 content are in the variable $1, Argument2 content in the variable $2, and so on through all the added arguments.

Note: you should be careful not to label any of your variables using the above style. Also for our purposes here, do not add arguments with spaces in them. While this can be done, it is a bit more complex than what we are doing in this example.

Counting Command line Arguments

In our example we will also be using a “for loop” to parse through the arguments. A “for loop” is a repeating block of code which has a specific ending, or exit, condition.

We will also add a check and response in case no argument is passed. This is another area of scripting that should not be ignored, throwing meaningful error messages when something goes wrong. You will never be able to do 100% of all possible instances, but you should think about the some of obvious ones and account for them.

In most cases you will know the specific number of arguments to be passed. However, there could be a occasion where this might be unknown. Techniques like the one we will use here can allow a variable number of arguments to be passed.

The Code Example

This is my script for the above. We will be going over the new stuff below.

#!/bin/bash

# Name: arg_count             Version: 1
# Author: RetiredTechie       Date: 11 SEP 2022

# Description: Count the number of command line arguments,
# check for no arguments and print back arguments

echo

# Check if any arguments where supplied

if [ "$#" -lt 1 ]; then
    echo -e "You must provide at least one argument! \n"
    exit 1
else
    echo -e "Total Number of Arguments: " $# "\n"
fi

# output all supplied arguments

for var in "$@"; do
    echo -e "$var" "\n"
done

exit 0

Script Review

We will skip over the shebang and header comments, as we covered those last time. Our first command is echo, which with no argument or text simply outputs a line feed.

The if clause is next. Note the “$#”. This is a special variable that holds the number of arguments. We compare it to the number 1, since we need at least one argument to make the script work. Note that the actual script name is argument zero (0).

The “-lt” represents less than. Note if we used a “<” sign we would need to use double parentheses instead of single square brackets. The “[“ is a built in self test, and requires the “-lt” operator. You can read about it by typing “man test”. Using the “<” is a math operation and requires the double parentheses, see example.

if (( "$#" < 1 )); then

If we type no arguments, then the first condition is meet. We send a message to the screen stating this. Note that we are using “echo” with the “-e” option. This ensures that the new line escape sequence (\n)will be recognized.

Note we also add an “exit 1”. After the script runs, we can examine the exit value, which is stored in a special variable called “$?”. For example, to display the exit condition.

echo $?

Exit codes do not have to be zero or one. You can get creative and use any integer, By examining the exit code, you will know which exit was used.

If we have added any arguments, then the “else” condition applies. In this case we print the total number of arguments to the screen.

After this, we come to the for loop. In this case we will be using “$@” which is an array of all arguments, as opposed to “$#” which is just the number of arguments. We simply step through the array, outputting values, until we reach the end.

When we reach the end of the program we use “exit 0”. Technically we do not need to, as the exit variable ($?) will be set to zero by default if nothing else changes it.

Conclusion

In this installment, we have discussed checking a condition with the if statement, outputting data with a for loop, using command line arguments, and setting exit conditions that we can examine after the script runs.

Next time around we will discuss how your script can get user input.

Shell or Terminal? Shell Scripting – Part 1

Simple Script Example! Shell Scripting – Part 2

Command Line Arguments! Shell Scripting Part 3

User Input! Shell Scripting Part 4

Using Functions! Shell Scripting Part 5

Variable Odds and Ends! Shell Scripting Part 6

Add New Comment

Your email address will not be published. Required fields are marked *