QUICK TIP - Useful Bash Scripts Linux – 9 Interview questions (updating)

- 1. QUICK TIP - How to make a YouTube subscription link and get more subscribers
- 2. QUICK TIP - Create Password Protected PDF with PHP & MySQL
Useful Bash Scripts – Linux – interview questions you should know. Why? These Bash Scripts are not only useful in practical situations but also you often face to the Bash Scripts questions from the interview. We are surveying and selecting the questions and giving you answers for referencing. We still keep update more on this post.
Okay. We are all ready. Let’s go!
1. Using Bash Scripts: How to read a text file with IPs list? Then check online IPs and add these IPs to a new file?
Answer: In order, giving a full answer for this Bash Scripts question, you should answer following this step-by-step.
Check IPs list file using Linux command: Cat
The bash scripts below shows you how to check the file is available, corruption or not… This step prepares for next step and also let the interviewers you are professional troubleshooting. We need to check the input first before going to write a Bash Script.
The example below shows us some example IPs. In the practical context, there are many IPs in text file. It may contain from 1k up to 1000k IPs.
Explanation: Cat is a Linux command which lists out and displays information in a text file in this situation.
Note: You can get more points from interviewers if you can show this step, although it is simple and easy. They will know that you are a professional IT and also a good preparation and careful guy. In many large organisation and big system, you should be careful and well-preparation.
1 2 3 4 5 6 |
1. File full of IP's [root@localhost scripts]# cat iplist.txt 172.31.57.63 localhost 127.0.0.1 172.31.57.62 |
Write a Bash Script to answer the question
Using Cat to create a script file in Linux. Firstly, we need a variable to store a string. This string will be the ips list text file that will store all alive ips after checking step. In addition, this step also need a WHILE loop to read and ping all ips in iplist.txt.
Explanation: Cat is a Linux command which creates new bash script file in this situation.
Now, we need to understand how this script works: ping -c 1 $ips
If an IP is alive, the result of the bash script will be the image below:
If an IP is died, the result of this bash script will be the image below:
if [ $? -eq 0 ]; then, this line will check the value after ping command is executed.
echo $ips >> $up_ipfile, this line will write the IP to text file if it is alive.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
2. Create the script [root@localhost scripts]# cat pingips.sh #!/bin/bash up_ipfile='onlineips.txt' while IFS= read -r ips; do ping -c 1 $ips > /dev/null 2>&1 if [ $? -eq 0 ]; then echo $ips >> $up_ipfile fi done < iplist.txt |
Run the script after making it executable
1 2 |
3. Run the script after making it executable [root@localhost scripts]# ./pingips.sh |
Create a file with IP’s which are alive
1 2 3 4 5 |
4. It will create a file with IP's which are alive [root@localhost scripts]# cat online_server.txt 172.31.57.63 localhost 127.0.0.1 |
2. Using Bash Scripts: How to read a list of users from a text file then create a folder and add right for all users as read only? Text file format: <username>:<fullname>:<usergroups>
If you face to the question like this, you should panic. In order to answer the question, you should keep calm and think. You should do the following steps below: