Let's talk about prompting for user's input in Shell scripts. There are hardly any programs without 1
any input. 2
In most cases, the input comes from the keyboard. For the purpose of getting the user's input 3
Bash provides the built-In command named Read. 4
Read will make the program flow stop until the user has given an input and has indeed the input with 5
the return or enter key. Let's see some examples. 6
The first example will be written directly in the terminal and then will create a shell script. 7
So read name. 8
The execution stopped and its's waiting for the user to enter a string and hit enter; and I'm writing “John”. 9
A new variable called name was created and the value stored as string 10
John. If you want it to display some helpful message for the user on the screen add the -p option. 11
So read -p "Enter the IP address: " and the variable Iip I'm hitting enter. 12
I'm entering an IP address. 13
And I'm displaying its value; and ping -c 1 $ip I can use the variable in the Shell commands 14
or scripts. 15
Now let's create a useful script that drops all packets from a specific IP address that is given by 16
the user. We'll use IP tables. 17
Please don't focus on it for the moment, but instead on how read works. To drop all packets coming from 18
a specific IP address or domain execute as root 19
the following command 20
sudo i tables - I 21
INPUT written in capitals -s the IP address 8.8.8.8 -j written 22
in lowercase letter and DROP written in capitals; before hitting enter 23
I want to check the connection to that IP address in another terminal. 24
So ping 8.8.8.8 and it's working. I'm stopping it; now I'm hitting enter, 25
I'm entering the password and the command was run. 26
I'm pinging the IP address again, and it's not working; the packets are dropped. 27
Now let's create a script. 28
So vim block_ip.sh the shebang and read -p enter the IP address or domain to block. 29
ip 30
And the iptables command: iptables - I input -s $ip -DROP 31
And I'm also printing a message for the user: echo "The packets from $ip 32
will be dropped" 33
I'm saving the file and exiting. 34
I'm giving it the execution mode and then running it as root. 35
So sudo ./ block ip.sh 36
Enter the IP address or domain name; sorry for the misspelling here it's or not of. First, I want to 37
check the connection to that IP address, for example, ping 1. 38
1 39
.1 40
.1 41
It's working! 42
I'm entering the IP address 1 43
.1 44
.1 45
.1 I'm hitting 46
enter; the packets from 1.1 47
.1 48
.1 will be dropped and 49
I'm testing it and it's not working. 50
The packets are dropped! Perfect! 51
And finally, if you want to enter a password without echoing it in the terminal add the -s option 52
like this read -s -p "Enter password:" let's say pswd and I'm hitting enter. It's 53
prompting for a password and I'm writing something. 54
It's not echoing. 55
echo$pswd This is the random string I have entered. 56
This is how you get the user's input in a variable. 57
This is very useful in Shell scripts.