Shell Scripting Interview Questions and Answers
This section covers frequently asked shell scripting interview questions.
1. What is a Shell Script?
A shell script is a text file containing a sequence of commands for a shell interpreter (like Bash or Zsh) to execute.
2. Why Use Shell Scripts?
Shell scripts automate tasks by combining multiple commands into a single file. This is particularly useful for system administrators who perform repetitive operations.
3. Advantages of Shell Scripting.
- Automation of repetitive tasks.
- Improved efficiency.
4. Disadvantages of Shell Scripting.
- Errors can be costly.
- Requires careful attention to detail (errors can lead to data loss).
- Can be slower than compiled languages.
- Portability can be an issue across different operating systems.
5. Types of Shell Script Variables.
- System-defined variables: Predefined by the operating system.
- User-defined variables: Created by the user.
6. Nested if
Statement Syntax.
Syntax
if [ condition1 ]; then
# commands
elif [ condition2 ]; then
# commands
else
# commands
fi
7. The $?
Variable.
$?
contains the exit status of the last executed command. A value of 0 usually indicates success; non-zero values indicate an error.
8. The break
Command.
break
exits a loop prematurely.
9. GUI Scripting.
GUI scripting automates interactions with graphical user interfaces. Tools and techniques vary depending on the operating system and applications.
10. Stages of a Linux Process.
- Waiting: Waiting for resources.
- Running: Currently executing.
- Stopped: Execution paused.
- Zombie: The process has finished, but the system hasn't yet released its resources (the process entry still exists in the process table).
11. Substituting ls
for echo
.
Yes, you can use ls
(list directory contents) in places where you might use echo
(print text) if you need to output filenames.
12. while
Loop Syntax.
Syntax
while [ condition ]; do
# commands
done
13. Hard Links vs. Soft Links.
A hard link is another name for the same file (same inode). A soft link (symbolic link) is a pointer to a file. Deleting the original file deletes hard links but might leave soft links functional (pointing to a non-existent file).
14. The $#
Variable.
$#
represents the number of arguments passed to a shell script.
15. Zombie Processes.
Zombie processes are processes that have finished executing but haven't been fully cleaned up by the operating system (their process entry still exists).
16. Standard Streams in Linux.
- Standard Input (stdin): File descriptor 0; typically the keyboard.
- Standard Output (stdout): File descriptor 1; typically the console.
- Standard Error (stderr): File descriptor 2; typically the console.