---
# System prepended metadata

title: L5. Bash Scripting. Exercises

---

# L5. Bash Scripting. Exercises

#### Excercise 1 - Hello, Bash
1. Написать скрипт, выводящий в консоль строку “Hello, Bash!”
1. Написать скрипт, выводящий в новый файл “hello.txt” строку “Hello, Bash!”. Если файл существует, то добавить строку в конец файла.

---

#### Excercise 2 - While
Create a bash file with the name, ‘while_example.sh’, to know the use of while loop. In the example, while loop will iterate for 5 times. The value of count variable will increment by 1 in each step. When the value of count variable will 5 then the while loop will terminate

---

#### Excercise 3 - If-else
The use of else if condition is little different in bash than other programming language. ‘elif’ is used to define else if condition in bash. Create a file named, ‘elseif_example.sh’ and add the following script to check how else if is defined in bash script.

---

#### Excercise 4 - For
The basic for loop declaration is shown in the following example. Create a file named ‘for_example.sh’ and add the following script using for loop. Here, for loop will iterate for 10 times and print all values of the variable, counter in single line.

---

#### Excercise 5 - User Input
‘read’ command is used to take input from user in bash. Create a file named ‘user_input.sh’ and add the following script for taking input from the user. Here, one string value will be taken from the user and display the value by combining other string value.

---

#### Excercise 6 - Arguments
Bash script can read input from command line argument like other programming language. For example, $1 and $2 variable are used to read first and second command line arguments. Create a file named “command_line.sh” and add the following script. Two argument values read by the following script and prints the total number of arguments and the argument values as output.

---

#### Excercise 7 - Strings
You can easily combine string variables in bash. Create a file named “string_combine.sh” and add the following script to check how you can combine string variables in bash by placing variables together or using ‘+’ operator.

---

#### Excercise 8 - Function
Bash function can pass both numeric and string values. How you can pass a string value from the function is shown in the following example. Create a file named, ‘function_return.sh’ and add the following code. The function, greeting() returns a string value into the variable, val which prints later by combining with other string.

---

#### Excercise 9 - Make dir
If you want to check the existence of directory in the current location before executing the ‘mkdir’ command then you can use the following code. ‘-d’ option is used to test a particular directory is exist or not. Create a file named, ‘directory_exist.sh’ and add the following code to create a directory by checking existence.

---

#### Excercise 10 - Read file
You can read any file line by line in bash by using loop. Create a file named, ‘read_file.sh’ and add the following code to read an existing file named, ‘book.txt’.

---

#### Excercise 11 - Append to file
New data can be added into any existing file by using ‘>>’ operator in bash. Create a file named ‘append_file.sh’ and add the following code to add new content at the end of the file. Here, ‘Learning Laravel 5’ will be added at the of ‘book.txt’ file after executing the script.

---

#### Excercise 12 - Test file
You can check the existence of file in bash by using ‘-e’ or ‘-f’ option. ‘-f’ option is used in the following script to test the file existence. Create a file named, ‘file_exist.sh’ and add the following code. Here, the filename will pass from the command line.

---

#### Excercise 13 - Ping-Bash*
Determine the ip-addresses of available hosts on the local network using the ping utility. List the available hosts on the screen.

---

#### Excercise 14 - If*
Different types of logical conditions can be used in if statement with two or more conditions. How you can define multiple conditions in if statement using AND logic is shown in the following example. ‘&&’ is used to apply AND logic of if statement. Create a file named ‘if_with_AND.sh’ to check the following code. Here, the value of username and password variables will be taken from the user and compared with ‘admin’ and ‘secret’. If both values match then the output will be “valid user”, otherwise the output will be “invalid user”.

---

#### Excercise 15 - Date*
You can get the current system date and time value using `date` command. Every part of date and time value can be parsed using ‘Y’, ‘m’, ‘d’, ‘H’, ‘M’ and ‘S’. Create a new file named ‘date_parse.sh’ and add the following code to separate day, month, year, hour, minute and second values.

---

#### Excercise 16 - Access Log*
Write a script that analyzes an arbitrary file of access to the Apache web server (access.log) and displays a list of unique ip addresses sorted by the frequency of requests to the server
>https://drive.google.com/file/d/15Y27BxADxRg21etx41oyjGwalOA0bCPg/view?usp=sharing

---

#### Excercise 17 - Exploits*
Write a script to search for a given exploit from searchsploit and download all matches.

---

#### Excercise 18 - Nmap-parser*
Write a script to parse nmap output for open http/https ports

---

#### Excercise 19 - Nmap-Bash*
Write a script to run Nmap from terminal with specific arguments to find open snmp ports in arbitary network
>-P0 -v -sU -p 161 -oA snmp_scan --max-parallelism=50

---

#### Excercise 20 - SNMP-scan*
Write a script to check snmp open ports with default passwords (public|private) and get sysName and sysDescr from devices. The targets for scanning need to be obtained from the nmap output file (*.gnmap). Write results to output file.
>snmpwalk -v2c -c public <ip> SNMPv2-MIB::sysName.0
>snmpwalk -v2c -c public <ip> SNMPv2-MIB::sysDescr.0