Verifying user input with lists
In this script, we will ensure that a value has been supplied to the first positional parameter. We can modify the hello2.sh
script that we created in Chapter 1, What and Why of Scripting with Bash, to check for user input before displaying the hello
text.
You can copy the hello2.sh
script to hello4.sh
or simply create a new script from scratch. There will not be a lot a typing and the script will be created as $HOME/bin/hello4.sh
, as shown:
We can ensure that the script is executable with the following command:
$ chmod +x $HOME/bin/hello4.sh
We can then run the script with or without arguments. The test
statement is looking for the $1
variable to be zero bytes. If it is, then we will not see the hello
statement; otherwise it will print the Hello message. In simple terms, we will see the hello
message if we supply a name.
The following screenshot shows the output that you will see when you do not supply a parameter to the script, followed by the supplied parameter...