This article prepares 70 shell script interview questions and answers, and shell scripts are the skills that must be mastered in Linux work.
1 How can I pass parameters to a script?
./script argument
Example: Display file name script
./show.sh File1.txt
Cat show.sh
#!/bin/bash
Cat $
2 How do I use parameters in a script?
First argument: $, second argument: $
Example: The script will copy the file (arg1) to the destination address (ARG2)
./copy.sh file1.txt/tmp/
Cat copy.sh
#!/bin/bash
CP $ $
3 How to calculate the parameters passed in?
$#
4 How do I get the script name in the script?
$
5 How to check if the previous command is running successfully?
$?
6 How do I get the last line of the file?
Tail-1
7 How do I get the first line of the file?
Head-1
8 How do I get a third element of each row of a file?
awk ' {print $} '
9 if the first element of each line in the file is find, how to get the second element
awk ' {if ($ = ' find ') print $} '
10 How to debug a bash script
After adding the-XV parameter to the #!/bin/bash
Example:
#!/bin/bash XV?
11 example how to write a function?
function Example {
echo "Hello world!"
}
12 How to connect two strings?
v1= "Hello"
V2= "World"
V3=${V1}${V2}
Echo $V 3
Output
HelloWorld
13 How do I add two integers?
V1=1
v2=2
Let v3= $V 1+ $V 2
Echo $V 3
Output
3
According to @kashu opinion, the better answer to the question is:
Add two integers, there are several ways to do this:
A=5
B=6
echo $ (($A + $B)) # method 2
echo $[$A + $B] # Method 3
Expr $A + $B # Method 4
echo $A + $B | BC # Method 5
awk ' begin{print ' $A ' + ' ' $B '} ' # Method 6
14 How do I check if a file exists in the file system?
if [-f/var/log/messages]
Then
echo "File exists"
Fi
15 Write all the looping syntax in the shell script?
For loop:
For I in $ (LS); Todo
Echo Item: $i
Done
While loop:
#!/bin/bash
Counter=0
While [$COUNTER-lt 10]; Todo
echo the counter is $COUNTER
Let Counter=counter+1
Done
Until cycle:
#!/bin/bash
Counter=20
Until [$COUNTER-lt 10]; Todo
Echo COUNTER $COUNTER
Let Counter-=1
Done
16 What does the #!/bin/sh or #!/bin/bash at the beginning of each script mean?
This line shows the shell to use. #!/bin/bash indicates that the script uses/bin/bash. For Python scripts, it's #!/usr/bin/python. (LCTT: This line is called the companion line.) )
17 How do I get the 10th line of the text file?
Head-10 FILE|TAIL-1
The first symbol of the bash script file
#
19) Command: [-Z ""] && echo 0 | | What is the output of echo 1?
0
20 What is the use of the order "export"?
Make the variable available in the child shell.
21 How to run the script in the background?
Add "&" after the script.
According to @kashu opinion, the better answer is:
Nohup Command &
Most of the time we could be using Linux remotely, and I've encountered Command & that runs in the background because the network is disconnected ...
What does "chmod" do?
Make the script owner have executable permissions.
">" What to do?
Redirects the output to a file or to another stream.
& && What's the Difference
&-Want the script to run in the background and use it
&&-use it when the current script completes successfully before executing the command/script later
25 when do you want to use "if" before [condition]?
When conditions are met, multiple commands need to be run.
26 Command: What is the output of Name=john && echo ' My name is $name '
My name is $name
In bash shell script which symbol is used for annotation?
#
28) Command: What is the output of the Echo ${new:-variable}
Variable
29) What is the difference between ' and ' quotes?
'-use it when we don't want to convert the variable to a value.
"-Calculates the value of all variables and replaces them with values.
30 How to redirect standard output and standard error stream to Log.txt file in script file?
Add the "Exec >log.txt 2>&1" command in the script file.
31 How do I get only part of a string variable with the echo command?
Echo ${variable:x:y}
X-Start position
Y-Length
Example:
Variable= "My name is Petras, and I am developer."
Echo ${variable:11:6} # will show Petras
32 if the given string variable= "User:123:321:/home/dir", how can you get home_dir only with the echo command?
echo ${variable#*:*:*:}
Or
echo ${variable##*:}
33 How do I get "User" from the string above?
echo ${variable%:*:*:*}
Or
echo ${variable%%:*}
34 How do I use awk to list users with a UID of less than 100?
Awk-f: ' $3<100 '/etc/passwd
35 Write program for the user to calculate the number of primary groups and display the number and group name
Cat/etc/passwd|cut-d:-f4|sort|uniq-c|while Read C G
Todo
{echo $c; grep: $g:/etc/group|cut-d:-f1;}| Xargs-n 2
Done
36 How do I change the standard field delimiter in the bash shell to ":"?
Ifs= ":"
37 How do I get the variable length?
${#variable}
38 How do I print the last 5 characters of a variable?
echo ${variable:-5}
${VARIABLE:-10} and ${variable:-10} What's the difference?
${VARIABLE:-10}-Output 10 If the variable has not been assigned before, and output if there is a value assigned
${variable:-10}-Output variable last 10 characters
40 How do I replace part of a string with the echo command only?
Echo ${variable//pattern/replacement}
41 which command replaces the command with uppercase?
TR ' [: Lower:] ' [: Upper:] '
42 How to calculate the number of local users?
Wc-l/etc/passwd|cut-d ""-f1 or Cat/etc/passwd|wc-l
43 How to calculate the number of words in a string without WC command?
Set ${string}
Echo $#
"Export $variable" or "Export variable" which is correct?
Export variable
45 How do you list a file with the second letter A or B?
Ls-d? [ab]*
46 How do I add integer A to B and assign a value to C?
c=$ ((a+b))
Or
C= ' expr $a + $b '
Or
C= ' echo ' $a + $b ' |BC '
47 How do I get rid of all the spaces in the string?
echo $string |tr-d ""
48 rewrite This command to convert the output variable to plural: item= "car"; echo "I like $item"?
item= "Car"; echo "I like ${item}s"
49 write out a multiple of 3 in the output number 0 through 100 (0 3 6 9 ...) 's order?
For i in {0..100..3}; do echo $i; Done
Or
for ((i=0; i<=100; i=i+3)); Do echo "Welcome $i times"; Done
50 How do I print all the parameters passed to the script?
Echo $*
Or
Echo $@
What's the difference [$a = = $b] and [$a-eq $b]
[$a = = $b]-for string comparisons
[$a-eq $b]-for numeric comparisons
52 = and = = What is the difference between
=-Used to assign values to variables
= =-for string comparisons
53 Write the test $a is greater than 12 command?
[$a-GT 12]
54 Write the test $b is less than or equal to 12 command?
[$b-le 12]
55 How do I check if the string starts with the letter "ABC"?
[[$string = = abc*]]
[[$string = = abc*]] and [[$string = = "abc*"]] what's the difference
[[$string = = abc*]]-checks whether the string starts with the letter ABC
[[$string = = "abc"]]-check that the string is exactly equal to ABC
57 How do I list the user names that start with AB or XY?
Egrep "^ab|^xy"/etc/passwd|cut-d:-f1
$! in Bash What does that mean?
The PID for the most recent command execution in the background.
59) $? What does that mean?
The end state of the most recent command at the foreground.
60 How to output the current shell PID?
Echo $$
61 How do I get the number of parameters passed to the script?
Echo $#
(LCTT: Repeat with question 3rd.) )
62 What's the difference between $* and $@?
$*-Output all parameters passed to the script as a string
$@-Lists all parameters passed to the script in $IFS as delimiters
63 How do I define an array in bash?
array= ("Hi" "I" "Name" "is")
64 How do I print the first element of an array?
Echo ${array[0]}
65 How do I print all the elements of an array?
Echo ${array[@]}
66 How do I output all the array indexes?
Echo ${!array[@]}
67 How do I move an element with an index of 2 in the divisor group?
Unset Array[2]
68 How do I add an element with ID 333 in an array?
array[333]= "New_element"
How does the shell script get the value entered?
A) through the parameters
./script param1 param2
b) through the Read order
Read-p "Destination backup Server:" Desthost
70 How do I use "expect" in a script?
/usr/bin/expect << EOD
Spawn Rsync-ar ${line} ${desthost}:${destpath}
Expect "*?assword:*"
Send "${password}\r"
Expect EOF
EOD