Day-6.3-devops

Essential Shell Scripting Interview Questions for DevOps Engineers

Commonly Asked Questions

1. What are the most commonly used shell commands?

When asked about commonly used shell commands, be honest and list the commands you use regularly. Some of the most frequently used commands include:

  • ls: List files in a directory.

  • cp: Copy files.

  • mv: Move files.

  • mkdir: Create directories.

  • touch: Create empty files.

  • vim: Open files for editing.

  • grep: Filter output based on patterns.

Avoid mentioning commands that are rarely used unless specifically asked, as this may indicate a lack of practical experience.

2. Write a simple shell script to list all processes.

To list all processes, you can use the command:

ps -ef

This command provides detailed information about all running processes. If asked to print only the process IDs, you can use the awk command to filter the output:

ps -ef | awk '{print $2}'

3. Write a script to print only errors from a remote log.

To retrieve logs from a remote server, you can use the curl command. For example:

curl http://example.com/logfile | grep "error"

This command fetches the log file and filters it to show only lines containing the word "error."

4. Write a shell script to print numbers divisible by 3 and 5 but not 15.

To solve this problem, you can use a for loop and if conditions:

for i in {1..100}; do
  if (( i % 3 == 0 || i % 5 == 0 )) && (( i % 15 != 0 )); then
    echo $i
  fi
done

This script checks each number from 1 to 100 and prints those that meet the criteria.

5. Write a script to count occurrences of 's' in the word "Mississippi."

You can achieve this using the grep command:

echo "Mississippi" | grep -o "s" | wc -l

This command counts the occurrences of the letter 's' in the word.

6. How do you debug a shell script?

To debug a shell script, you can use the command:

set -x

This command enables a mode of the shell where all executed commands are printed to the terminal, helping you trace the execution flow.

7. What is crontab and how is it used?

crontab is a Linux utility that allows you to schedule tasks to run at specific intervals. For example, to run a script every day at 6 PM, you would add the following line to your crontab:

0 18 * * * /path/to/script.sh
  • Hard Link: A hard link is a direct reference to the file's inode. If the original file is deleted, the hard link still points to the data.

  • Soft Link: A soft link (or symbolic link) points to the file name. If the original file is deleted, the soft link becomes broken.

9. What are the differences between break and continue statements in loops?

  • Break: Exits the loop entirely.

  • Continue: Skips the current iteration and proceeds to the next iteration of the loop.

10. What are the disadvantages of shell scripting?

Some disadvantages include:

  • Lack of static typing, which can lead to runtime errors.

  • Limited error handling capabilities compared to more robust programming languages.

11. What are the different types of loops in shell scripting?

The main types of loops in shell scripting are:

  • for loop

  • while loop

  • until loop

12. Is bash dynamically or statically typed?

Bash is dynamically typed, meaning you do not need to declare variable types explicitly.

13. What networking troubleshooting tools do you use?

Common tools include:

  • traceroute: To trace the path packets take to a network host.

  • ping: To check the reachability of a host.

14. How do you sort a list of names in a file?

You can use the sort command:

sort names.txt

This command sorts the names in the specified file alphabetically.

15. How do you manage logs that generate huge files every day?

You can use logrotate to manage log files efficiently. It allows you to compress and rotate logs based on size or time.

16. What is the purpose of the awk command?

awk is a powerful text processing tool used for pattern scanning and processing. It is commonly used for extracting and manipulating data from files.

17. How do you open a file in read-only mode?

You can open a file in read-only mode using:

vim -R filename.txt

18. What is the purpose of the chmod command?

chmod is used to change the file permissions in Linux. For example, to make a script executable, you would use:

chmod +x script.sh

19. How do you check disk space usage?

You can check disk space usage using the df command:

df -h

This command displays disk space usage in a human-readable format.

20. What is the significance of the shebang (#!) in a script?

The shebang indicates the script's interpreter. For example, #!/bin/bash tells the system to use the Bash shell to execute the script.

Conclusion

Preparing for shell scripting interviews requires a solid understanding of both basic and advanced concepts. By familiarizing yourself with these common interview questions and practicing your responses, you will be better equipped to demonstrate your skills and knowledge in shell scripting during your interviews. Good luck!