Linux Shell script interview 25 questions
Q: What is a 1 Shell script and is it necessary?
A: A Shell script is a text file that contains one or more commands. As a system administrator, we often need to use multiple commands to complete a task. We can add all these commands in a text file (Shell script) to complete these daily tasks.
Q: 2 What is the default logon shell and how to change the logon shell of a specified user?
A: In Linux, "/bin/bash" is the default logon shell, which is allocated when a user is created. Use the chsh command to change the default shell. Example:
# Chsh <User Name>-s <new shell>
# chsh linuxtechi -s /bin/sh
Q: 3 what types of variables can be used in shell scripts?
A: In shell scripts, we can use two types of variables:
- System Defined variables
- User-Defined variables
System variables are created by the system itself. These variables are usually composed of uppercase letters and can be viewed using the "set" command.
User variables are generated and defined by the System user. You can run the"Echo $ <variable name>
"View.
Q: 4 How can I redirect both standard output and error output to the same location?
A: There are two methods to achieve this:
Method 1:
2> & 1 (for example, # ls/usr/share/doc> out.txt 2> & 1)
Method 2:
&> (For example, # ls/usr/share/doc &> out.txt)
Q: How to nest the "if" syntax in 5 shell scripts?
A: The basic syntax is as follows:
If [condition]
then
Command 1
Command 2
…..
else
If [condition]
then
Command 1
Command 2
….
else
Command 1
Command 2
…..
fi
fi
Q: "$?" In 6 shell scripts ?" What is the purpose of the tag? ?
A: When writing a shell script, if you want to check whether the previous command is successfully executed, use "$?" In the if condition ?" You can check the end status of the previous command. A simple example is as follows:
root@localhost:~# ls /usr/bin/shar
/usr/bin/shar
root@localhost:~# echo $?
0
If the end state is 0, the previous command is successfully executed.
root@localhost:~# ls /usr/bin/share
ls: cannot access /usr/bin/share:No such file or directory
root@localhost:~# echo $?
2
If the end state is not 0, the command execution fails.
Q: 7. How do I compare two numbers in shell scripts?
A: Use the test command (such as-gt) in if-then to compare two numbers. The example is as follows:
#!/bin/bash
x=10
y=20
if[ $x -gt $y ]
then
echo “x is greater than y”
else
echo “y is greater than x”
fi
Q: What is the role of the break command in 8 shell scripts?
A: A simple use of the break command is to exit the execution cycle. We can use the break command in the while and until loops to exit the loop.
Q: 9 What is the function of the continue command in shell scripts?
A: The continue command is different from the break command. It only jumps out of the iteration of the current loop, not the whole loop. The continue command is often useful, for example, when an error occurs, but we still want to continue executing the large loop.
Q: 10 tell me the syntax of the Case statement in the shell script?
A: The basic syntax is as follows:
Case variable in
Value 1)
Command 1
Command 2
…..
Last Command
!!
Value 2)
Command 1
Command 2
……
Last Command
;;
esac
Q: What is the while LOOP syntax in the 11 shell script?
A: Like a for loop, A while LOOP repeats its command block as long as the condition is true. Unlike for loop, while loop continues iteration until its condition is not true. Basic Syntax:
While [condition]
do
Command...
done
Q: 12 how can I make the script executable?
A: run the chmod command to make the script executable. Example:
# chmod a+x myscript.sh
Q: 13 "#! What is the role of/bin/bash?
A :#! /Bin/bash is the first line of shell script, called the release companion line (shebang. Here # The symbol is called hash, and! It is called bang. It means that the command is executed through/bin/bash.
Q: 14 is the for loop syntax in shell scripts?
A: The basic syntax of the for Loop:
For variable in loop list
do
Command 1
Command 2
….
Last Command
done
Q: 15 How do I debug shell scripts?
A: You can use the '-x' parameter (sh-x myscript. sh) to debug shell scripts. Another method is to use the '-nv' parameter (sh-nv myscript. sh ).
Q: How do 16 shell scripts compare strings?
A: The test command can be used to compare strings. The test command compares each character in the comparison string.
Q: 17 What are the special variables in the Bourne shell (bash?
A: The following table lists the special variables set by the Bourne shell for the command line.
Built-in Variables |
Explanation |
$0 |
Script Name in command line |
$1 |
The First Command Line Parameter |
$2 |
The second command line parameter |
..... |
....... |
$9 |
Ninth Command Line Parameter |
$ # |
Number of command line parameters |
$ * |
All command line parameters, separated by Spaces |
Q: 18 How to test files in a shell script? Q: 18 in shell scripts, how do I test files?
A: The test command can be used to test files. The basic usage is as follows:
Test |
Usage |
-D file name |
Returns true if the file exists and is a directory. |
-E file name |
Returns true if the file exists. |
-F file name |
Returns true if the object exists and is a normal object. |
-R file name |
Returns true if the object exists and is readable. |
-S file name |
Returns true if the file exists and is not empty. |
-W file name |
Returns true if the file exists and can be written. |
-X file name |
Returns true if the file exists and is executable. |
Q: 19 in shell scripts, how do I write comments?
A: Annotations can be used to describe what a script can do and how it works. Each line of comment starts. Example:
#!/bin/bash
# This is a command
echo “I am logged inas $USER”
Q: 20 how can I get the input from the terminal from the shell script?
A: The read command can read data from a terminal (using a keyboard. Read command to get the user input and place it in the variables you give. Example:
# vi /tmp/test.sh
#!/bin/bash
echo ‘Please enter your name’
read name
echo “MyNameis $name”
# ./test.sh
Please enter your name
LinuxTechi
MyNameisLinuxTechi
Q: 21. How do I cancel a variable or assign a value to a variable?
A: The "unset" command is used to cancel a variable or cancel a variable assignment. The syntax is as follows:
# Unset <variable name>
Q: 22. How do I perform arithmetic operations?
A: There are two ways to perform arithmetic operations:
1. Useexpr
Command (# expr 5 + 2) 2. Use a dollar sign and square brackets ($ [Expression]
) Example: test = $[16 + 4]; test = $[16 + 4]
Q: 23 is the basic format of the do-while statement?
A: the do-while statement is similar to the while statement, but the command is executed at least once before the Condition Statement is checked .). The following is the syntax of the do-while statement.
do
{
Command
} While (condition)
Q: How do I define functions in shell scripts?
A: A function is a code block with a name. When we define a code block, we can call the function name in our script and the block will be executed. Example:
$ diskusage (){ df -h ;}
Note: Below is the shell function syntax I have given.
[Function] function name [()]
{
Command;
[returnint;]
}
Q: 25 How do I use the BC (bash calculator) in shell scripts )?
A: Use the following format and use bc in shell scripts:
variable=`echo “options; expression” | bc`
Cat commands use Linux redirection to merge files
Shell programming
Replace Linux Shell Parameters
Shell for parameters
Pass Linux/Unix Shell parameters to SQL scripts
Introduction to parameter passing methods in Shell scripts
PASS command line parameters through Shell scripts
Linux Shell wildcards, escape characters, metacharacters, and special characters
This article permanently updates the link address: