当前位置:网站首页>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
边栏推荐
- Latest version of source insight
- 一起上水硕系列】Day 9
- [function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram
- Brief introduction of realsense d435i imaging principle
- Transferring images using flask
- Gan network thought
- Configure DTD of XML file
- @Import annotation: four ways to import configuration classes & source code analysis
- Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- 2022.7.2 simulation match
猜你喜欢

College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
![[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation](/img/9b/a309607c037b0a18ff6b234a866f9f.jpg)
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation

@Import annotation: four ways to import configuration classes & source code analysis

一起上水硕系列】Day 9

PHP笔记超详细!!!

Kubernetes resource object introduction and common commands (V) - (configmap)

一起上水碩系列】Day 9

配置xml文件的dtd

【一起上水硕系列】Day 7 内容+Day8

Why should we rewrite hashcode when we rewrite the equals method?
随机推荐
期末复习DAY8
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
Troubleshooting of 32GB Jetson Orin SOM failure to brush
Altaro VM backup getting started
chromedriver对应版本下载
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
2022.7.2 simulation match
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
配置xml文件的dtd
Source insight operation manual installation trial
32GB Jetson Orin SOM 不能刷机问题排查
Ansible firewall firewalld setting
Training method of grasping angle in grasping detection
Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
NG Textarea-auto-resize
2022.7.2 模拟赛
mapbox尝鲜值之云图动画
ninja: build stopped: subcommand failed.