In many cases, Bash scripts require argument values to provide input to the script. 1
The arguments are the values provided after the script name when running it. 2
For example, if you are running apt install nginx, apt ist the script or the command name, install 3
is the first argument of the script and nginx is the second argument of the script. 4
The name of the script and the arguments are separated by spaces which in this case is known as the 5
default internal field separator. 6
There are some predefined variables for this purpose, also known as positional arguments, and will 7
take them one by one. If you run a script, like say./script.sh filename1 8
dir1 and an IP address, then $0 is the name of the script itself. 9
So script.sh 10
This is not a positional argument, but is useful when you want to run the script again while giving 11
it another input values. 12
$1 is the first positional argument 13
so filename1$2 is the second positional argument 14
so dir1 and the third, 15
the last argument of the script is the IP address. 16
If there were more arguments, $9 would be the ninth argument, and after $9 the 17
arguments must be enclosed in curly braces; for example, $ and 10 or 11 between curly braces. 18
And then $# is the number of the positional parameters. 19
"$*" is a string representation of all positional arguments 20
so , $1 $2 $3 and so on and $? is the most recent foreground 21
command exit status. 22
All of these predefined variables are available and can be used inside the scripts. 23
This concept is very important 24
so I've just created the following script for you to see exactly what the positional arguments mean. 25
This is the script I've just created. 26
It's displaying the value of each argument. 27
I'm executing the script first without any arguments. 28
And we notice that $0 stores the name of the script $1, 2 and 3 are empty because 29
there are no arguments and $# is the 0 because the script was run with zero arguments. 30
But let's see what will happen if we run it with some arguments. 31
So the name of the script and some arguments: linux, windows, mac, and 10. 32
So the first argument of the script is linux, so $1=linux; 33
The second argument, windows is stored in $2; #3 34
will hold the string value 35
mac; this is $4 ; $* or asterisk is a string representation of all positional arguments 36
and $# 37
is storing the number of arguments so4 38
Look at this example 39
until you deeply understand it; this is very important! 40
Let's see some more useful examples! I'm creating a new script that takes a file as argument, displays 41
its contents and then creates a compressed copy of it. 42
So vim display_compress .sh 43
The first line is the shebang. 44
I'm displaying a message which is saying what the script does; it's always a good idea to be verbose 45
and tell the user what's happening. 46
So echo "Displaying the contents of $1 ..." 47
And I'm posing the execution for two seconds so that the user has enough time to read the previous 48
message;sleep 2 and I'm displaying the contents of the file, so of the first argument of the script, 49
which is stored in the special variable $1 50
So cat $1 Remember that $1 is a predefined variable that already exists, doesn't need to be declared 51
and holds the value of the first argument. 52
Echo for an empty line and then echo compressing $1; I'm compressing the file, the first argument 53
of the script. 54
I am sleeping for two seconds, and now the tar command that will comprise the file so tar - 55
-czvf I'm going to create the gz archive, the name of the archive, so that $1 the name 56
of the file.tar.gz and the file $1; I'm saving and exiting the file, setting the execution 57
permission and I'm running the script with a file as argument. 58
So display_compress.sh and the file as argument, for example arguments.sh it's in 59
the same directory. 60
It's displaying the contents of the file and then it's compressing the file. 61
Look, this is the compressed file. 62
Of course, we can call it with any other file on the system as an argument; this time I'm running 63
it as root because the archive will be created in /etc so sudo 64
./ display_compress and the file will be /etc/passwd 65
I'm entering the password, it's displaying the contents of the password file, and now it's compressing 66
it. 67
And this is the compressed file. Let's move ahead to the second example. 68
In the previous lecture, we've created a script that prompts the user for an IP address and drops all 69
packets comming from that IP address. 70
This is the script. 71
Now I want to change the script so that the e is given IP address is given as an argument to the script instead 72
of being written by the user. 73
I'm making a copy of the script because I want to keep the existing version of the script as well. 74
So copy block_ip.sh let's say drop ip.sh; you can give it any name you want. 75
I'm opening the file and changing it. 76
I'm removing this line and instead I'm writing: 77
"Dropping packets from $1" the argument of the script, which is the IP address. 78
And in the iptables command instead of $1p, I'm using $1. 79
And the package from $1 will be dropped; that's all. And I'm testing the script, so sudo ./drop_ip.sh 80
and an IP address, for example, 4. 81
4. 82
4.4. this is the first argument of the script 83
so $1; let's see if the connection to 84
4.4.4.4. is working. 85
So ping 4.4.4.4. 86
OK, it's not working I'm going to choose another IP address; I'm pinging a random IP address. 87
You can choose any IP address you want. 88
Ping is working, I'm using that IP address as the first argument of the script and hitting enter. 89
And Ping has stopped; all packets from 100.0.0.1 are dropped . OK! That's for the moment 90
about positional arguments. 91
Thank you!