当前位置:网站首页>70 shell script interview questions and answers
70 shell script interview questions and answers
2022-07-03 05:44:00 【TAOXC( ̿ ▀ ̿ ̿Ĺ̯̿̿ ▀ ̿ ̿】
We have chosen the expected 70 individual shell Script questions and answers . For all system administrators , It's really important to know the script or at least the basics , This in turn helps automate many tasks in your work environment . In the past few years , We have seen all linux Job specifications require scripting skills .
1) How to pass parameters to scripts ?
./ Script parameters
Example : The script will display the file name
./show.sh file1.txt
cat show.sh
#!/bin/bash
cat $1
2) How to use parameters in scripts ?
The first parameter :$1,
The second parameter :$2
Example : Script will file (arg1) Copy to target (arg2)
./copy.sh file1.txt /tmp/
cat copy.sh
#!/bin/bash
cp $1 $2
3) How to calculate the number of passed parameters ?
$#
4) How to get the script name in the script ?
$0
5) How to check whether the previous command runs successfully ?
$?
6) How to get the last line from the file ?
tail -1
7) How to get the first line from the file ?
head -1
8) How to get the third element from each line of the file ?
awk '{ print $3}'
9) If the first equals FIND, How to get the second element from each line of the file
awk '{ if ($1 == "FIND") print $2}'
10) How to debug bash Script
take -xv Add to #!/bin/bash
Example
#!/bin/bash –xv
11) For example, how to write functions ?
function example {
echo "Hello world!"
}
12) How to add a string to a string ?
V1="Hello"
V2="World"
let V3=$V1+$V2
echo $V3
Output
Hello+World
13) How to add two integers ?
V1=1
V2=2
V3=$V1+$V2
echo $V3
Output
3
Remember you need to add "let" to line V3=$V1+$V2
then echo $V3 will give 3
if without let , then it will be
echo $V3 will give 1+2
14) How to check whether there are files on the file system ?
if [ -f /var/log/messages ]
then
echo "File exists"
fi
15) write down shell Syntax of all loops in the script ?
loop :
for i in $( ls ); do
echo item: $i
done
while loop :
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
Until the cycle :
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
16) At the beginning of each script #!/bin/sh or #!/bin/bash What does that mean? ?
This line tells which shell.#!/bin/bash Use /bin/bash Executed script . If it is python Script , There will be #!/usr/bin/python
17) How to get the number... From a text file 10 That's ok ?
head -10 file|tail -1
18) bash What is the first symbol in the script file
#
19) What is the output of the command : [ -z "" ] && echo 0 || Echoes 1
0
20) What order “export”?
In the child shell Open variables in
21) How to run scripts in the background ?
Add... At the end of the script “&”
22) “chmod 500 Script ” What does it do ?
Enable script owners to execute scripts
23) “>” What do you do ?
Redirect the output stream to a file or another stream .
24) & and && What's the difference?
& - We use it when we want to put the script in the background
&& - When we want to execute orders / Script time , If the first script completes successfully
25) When we're in [ Conditions ] Previous needs "if" when ?
If the conditions are met , We need to run multiple commands .
26) What is the output of the command :name=John && echo 'My name is $name'
My name is $name
27) bash shell What are the symbols used for comments in the script ?
#
28) What is the output of the command : echo ${new:-variable}
variable
29) ' and " What's the difference between quotation marks ?
' - We use it when we don't want to evaluate variables as values
" - All variables will be evaluated and assigned their values .
30) How to integrate stdout and stderr The stream is redirected from within the script to log.txt file ?
add to “exec >log.txt 2>&1” As the first command in the script
31) How to use only echo Command to get some string variables ?
echo ${variable:x:y}
x - The starting position
y - length
Example :
variable=" My name is Petras, I'm a developer ."
echo ${variable:11:6} # Will be displayed Petras
32) Only if string variables =“User:123:321:/home/dir” When given , How to use echo Command acquisition home_dir?
echo ${variable#*:*:*:}
or
echo ${variable##*:}
33) How to get from the string above “ user ”?
echo ${variable%:*:*:*}
or
echo ${variable%%:*}
34) How to list UID Less than 100 (awk) Users of ?
awk -F: '$3<100' /etc/passwd
35) Programming , Calculate a unique primary group for the user and display only the count and group name
cat /etc/passwd|cut -d: -f4|sort|uniq -c|while read c g
do
{ echo $c; grep :$g: /etc/group|cut -d: -f1;}|xargs -n 2
done
36) How to be in bash shell Change the standard field separator to “:”?
IFS=“:”
37) How to get variable length ?
${#variable}
38) How to print the end of a variable 5 Characters ?
echo ${variable: -5}
39) ${variable:-10} and ${variable:-10} What's the difference? ?
${variable:-10} - If in ${variable: -10} No variables have been allocated before , Then give 10 - give
At the end of the variable 10 Symbols
40) How to use only echo Command to replace part of the string ?
echo ${ Variable // Pattern / Replace }
41) Which command replaces the string with uppercase ?
tr '[:lower:]' '[:upper:]'
42) How to calculate local accounts ?
wc -l /etc/passwd|cut -d" " -f1
or
cat /etc/passwd|wc -l
43) How in the absence of wc Command to calculate the words in the string ?
set ${string}
echo $#
44) Which is right “export $variable” or “export variable”?
export variable
45) How to list the second letter as a or b The file of ?
ls -d ?[ab]*
46) How to put integers a Add to b And assigned to c ?
c=$((a+b))
or
c=`expr $a + $b`
or
c=`echo "$a+$b"|bc`
47) How to delete all spaces from a string ?
echo $string|tr -d " "
48) Rewrite the command to print sentences and convert variables to plural numbers :item="car"; Echoes “ I like $item”?
item="car"; echo "I like ${item}s"
49) Writing will print from 0 To 100 And display every third (0 3 6 9 ...) The order of ?
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 to print all the parameters provided to the script ?
echo $*
or
echo [email protected]
51) [ $a == $b ] and [ $a -eq $b ] What's the difference?
[ $a == $b ] - Should be used for string comparison
[ $a -eq $b ] - Should be used for digital testing
52) = and == What's the difference between
= - We use it to assign values to variables
== - We use it to compare strings
53) Write commands to test $a Is it greater than 12 ?
[$a-gt 12]
54) Write commands to test $b Is it equal to 12 ?
[ $b -le 12 ]
55) How to check whether a string is in “abc” Beginning of letter ?
[[ $string == abc* ]]
56) [[ $string == abc* ]] and [[ $string == "abc*" ]] What's the difference?
[[ $string == abc* ]] - The string will be checked to see if it starts with abc Beginning of letter
[[ $string == "abc*" ]] - Will check whether the string is exactly equal to abc*
57) How to list with ab or xy The user name at the beginning ?
egrep "^ab|^xy" /etc/passwd|cut -d: -f1
58) what $! It means in bash in ?
Recent background commands PID
59) what $??
The nearest foreground exits .
60) How to print the current shell Of PID?
echo $$
61) How to get the number of parameters passed to the script ?
echo $#
62) $* and [email protected] What's the difference?
$* - Treat all parameters passed to the script as a single string
Provide [email protected] - Provide all parameters passed to the script as a separated list . Separator $IFS
63) How to be in bash In the definition of array ?
array=("Hi" "my" "name" "is")
64) How to print the first array element ?
echo ${array[0]}
65) How to print all array elements ?
echo ${array[@]}
66) How to print all array indexes ?
echo ${!array[@]}
67) How to delete id by 2 Array elements of ?
unset array[2]
68) How to add id by 333 New array elements of ?
array[333]="New_element"
69) shell How the script gets the input value ?
a) Through parameters
./script param1 param2
b) By reading commands
read -p "Destination backup Server : " desthost
70) How do we use in scripts “expect” command ?
/usr/bin/expect << EOD
spawn rsync -ar ${line} ${desthost}:${destpath}
expect "*?assword:*"
send "${password}\r"
expect eof
EOD
边栏推荐
- 大二困局(复盘)
- About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller
- 中职网络子网划分例题解析
- Skip table: principle introduction, advantages and disadvantages of skiplist
- [written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis
- Get and monitor remote server logs
- ansible防火墙firewalld设置
- [trivia of two-dimensional array application] | [simple version] [detailed steps + code]
- Go practice -- design patterns in golang's singleton
- Best practices for setting up altaro VM backups
猜你喜欢
Altaro set grandfather parent child (GFS) archiving
Communication - how to be a good listener?
[Shangshui Shuo series together] day 10
@Autowired 导致空指针报错 解决方式
中职网络子网划分例题解析
mapbox尝鲜值之云图动画
Progressive multi grasp detection using grasp path for rgbd images
今天很多 CTO 都是被幹掉的,因為他沒有成就業務
3dslam with 16 line lidar and octomap
[set theory] relational closure (relational closure related theorem)
随机推荐
Apache+PHP+MySQL环境搭建超详细!!!
AtCoder Beginner Contest 258(A-D)
期末复习(Day5)
【一起上水硕系列】Day 10
AtCoder Beginner Contest 258(A-D)
Disassembly and installation of Lenovo r7000 graphics card
The request database reported an error: "could not extract resultset; SQL [n/a]; needed exception is org.hibernate.exception.sqlgram"
Redis cannot connect remotely.
@Import annotation: four ways to import configuration classes & source code analysis
Best practices for setting up altaro VM backups
kubernetes资源对象介绍及常用命令(五)-(ConfigMap)
Explanation of several points needing attention in final (tested by the author)
【无标题】
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
[untitled]
How to set up altaro offsite server for replication
Progressive multi grasp detection using grasp path for rgbd images
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
[Shangshui Shuo series together] day 10
Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)