Mastering Shell Scripting: Top 15 Interview Questions for DevOps Roles
TLDR: This blog post covers 15 essential shell scripting interview questions for DevOps roles, providing detailed explanations and examples for each question to help candidates prepare effectively.
In today's competitive job market, preparing for a DevOps interview requires a solid understanding of shell scripting. This blog post dives into 15 crucial shell scripting interview questions that you can expect during your DevOps interviews. Whether you are preparing for an interview or looking to polish your scripting skills, this guide is for you.
1. How do you declare a variable in a shell script?
To declare a variable in shell scripting, you assign a name to the variable and provide a value using the equals symbol without spaces. For example:
channel=dgr_uploads
You can print the variable using the echo
command:
echo $channel
2. How do you make a shell script executable?
To make a shell script executable, use the chmod
command to set the executable permission. For example:
chmod +x script.sh
After this, you can execute the script using:
./script.sh
3. How do you pass arguments to a shell script?
Arguments can be passed to a shell script using $1
, $2
, etc., where $1
is the first argument, $2
is the second, and so on. For example:
echo "First argument: $1"
echo "Second argument: $2"
You can execute the script with arguments like this:
./script.sh arg1 arg2
4. How do you check if a file exists in a shell script?
To check if a file exists, use the if
statement with the -e
option:
if [ -e "file.txt" ]; then
echo "File exists"
else
echo "File does not exist"
fi
5. How do you write a loop that iterates over a list of items?
You can use a for
loop to iterate over a list of items:
for item in item1 item2 item3; do
echo $item
done
6. How can you redirect both STDOUT and STDERR to a file?
To redirect both standard output and standard error to a file, use:
command > output.txt 2>&1
This captures both the output and any errors into output.txt
.
7. How do you schedule a cron job to run a script every day at midnight?
To schedule a cron job, edit the crontab file using:
crontab -e
Then add the following line:
0 0 * * * /path/to/script.sh
This will run the script every day at midnight.
8. How do you find and replace text in a file using shell script?
You can use the sed
command to find and replace text in a file:
sed -i 's/old_text/new_text/g' filename.txt
This command replaces all occurrences of old_text
with new_text
in filename.txt
.
9. How do you read user input in a shell script?
To read user input, use the read
command:
echo "Enter your name:"
read name
echo "Hello, $name"
10. How do you check the exit status of a command in a script?
You can check the exit status of the last executed command using $?
:
command
if [ $? -eq 0 ]; then
echo "Command executed successfully"
else
echo "Command failed"
fi
11. How do you create a function in a shell script?
To create a function, define it with a name and use curly braces:
my_function() {
echo "This is a function"
}
my_function
12. How do you append text to a file in a shell script?
To append text to a file, use the double greater than operator:
echo "This is a test" >> output.log
This command appends the text to output.log
without overwriting existing content.
13. How do you get the current date and time in a shell script?
You can get the current date and time using the date
command:
current_date=$(date)
echo "Current date and time: $current_date"
14. How do you handle errors in a shell script?
To handle errors, use set -e
to exit the script on error and trap
to catch errors:
set -e
trap 'echo "Error occurred at line $LINENO"' ERR
15. How do you pass a CSV file in a shell script?
To read a CSV file, set the delimiter and read line by line:
IFS=','
while read col1 col2 col3; do
echo "Column 1: $col1, Column 2: $col2, Column 3: $col3"
done < file.csv
Conclusion
These 15 shell scripting questions cover essential concepts that are frequently encountered in DevOps interviews. Understanding these topics will not only help you in interviews but also enhance your scripting skills. If you have any questions or topics you would like to see covered in future posts, feel free to leave a comment. Happy scripting!