Scripts
A script is generally a file that contains a grouping of executions that you want to run sequentially or together.
Interpreter
Not all operating systems contain the same shell interpreters, so you will have seen that the most widespread are /bin/sh and /bin/bash
The first line of a script document is mandatory and specifies the interpreter to be used for execution, so if we try to execute a script for /bin/bash on an operating system that doesn’t support /bin/sh our script will give an execution error.
Here is a very simple example:
Passing arguments to our Shell Script
It’s very useful to execute scripts, but if we can also read input parameters, much better. Let’s see how to read the input arguments of a script.
Script execution permissions (chmod)
When we create a file, by default it is not an executable file and we must give execution permissions on that file to be able to use it. For this we use chmod
touch myscript.sh # create the file that will contain the script
sudo chmod +x ./myscript.sh # Give execution permissions (+x)
The Arguments array
In the execution of a bash script, we always have context to the complete execution line that has just been executed.
We can access each term of the execution by its index.
Let’s imagine this script myscript.sh
#!/bin/bash
echo "these are the terms of this execution process"
echo "the first is $1"
echo "the second is $2"
echo "the third is $3"
and we execute it passing arguments and see the result:
We also have a reference in the form of an array of all these parameters which is @
We change the script to:
#!/bin/bash
echo "these are the terms of this execution process"
echo "all arguments are[$@]"
# and we can iterate through them normally
for var in "$@"
do
echo "$var"
done
Working with key=value parameters
There are times when a simple execution script doesn’t need configuration parameters (for example a script that passes a string to uppercase).
But there are times when our script needs to be parameterized to do one thing and another internally.
For this we can pass arguments in key=value format and load them as environment variables in the execution and thus be able to work with them.
Let’s imagine this execution
Our script has parameters that determine the transformation mode.
To read these key values we can export each pair of arguments and then use them in the script.
It will look like this:
#!/bin/bash
# We iterate through the arguments and load them as global variables in our script
for var in "$@"
do
export $var
done
echo "the transformation mode is [$mode]"
echo "the value to transform is [$text]"
result=""
if [ "$mode" == "ToUpperCase" ] ;
then
result=$(echo $text | tr 'a-z' 'A-Z')
else
result="Not valid transformation mode value."
exit -1;
fi
echo "The result is: $result"
exit 0;
and the execution will be:
$ ./transformText.sh mode=ToUpperCase text=hello
the transformation mode is [ToUpperCase]
the value to transform is [hello]
HELLO
Working with complex parameters
There are times when our execution requires many configuration parameters and it’s tedious to pass them all through the command line.
An alternative option is to group all those key values in a .properties file (formed by key values) and have our script only ask for the parameter of where the configuration file is.
Properties files (key=value) can be loaded directly as environment variables using the source myfile.properties command
We build our configuration file, execution.properties:
And we change our script, read the parameter and load the configuration into memory from the file and execute as before.
#!/bin/bash
# We iterate through the arguments and load them as global variables in our script
for var in "$@"
do
if [[ "$var" =~ configFile* ]]; then
export $var
fi
if [[ "$var" =~ "text" ]]; then
export $var # We also export the value of the string to transform
fi
done
# We check that it has been loaded correctly from the configuration file
if [ -z "$configFile" ]
then
echo "The configuration parameter has not been specified"
exit -1;
else
source $configFile
echo "Loaded property mode=$mode"
echo "Loaded property reverse=$reverse"
echo "Loaded property argumentC=$argumentC"
fi
# EXECUTE
result=""
if [ "$mode" == "ToUpperCase" ] ;
then
result=$(echo $text | tr 'a-z' 'A-Z')
else
result="Not valid transformation mode value."
exit -1;
fi
echo "The result is: $result"
exit 0;
and the execution
$ ./transformText.sh mode=ToUpperCase text=hello configFile=./execution.properties
Loaded property mode=ToUpperCase
Loaded property reverse=false
Loaded property argumentC=whatever
The result is: HELLO
Prompting from shell script
To read an interaction from a script with a user we use read:
#!/bin/bash
read -p "Please insert full path to values-api file: " definitionFile
echo "definition file is $definitionFile"
Conclusion
We have seen in a simple way how to pass parameters to the execution of a script, read it to be able to do its tasks with the input arguments. We have seen that if it has a complex configuration we can use a properties file and a source.