What are the Shell script questions?
This article mainly introduces what the Shell script test questions are, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.
1) how do I pass parameters to a script?
. / script argument
Example: show file name script
. / show.sh file1.txtcat show.shemaking raceme binbinarbashecho $1
(LCTT translation note: thank you for the reminder of an anonymous visitor, the original question is wrong, correct it. )
2) how to use parameters in a script?
The first parameter: $1, and the second parameter: $2
Example: the script copies the file (arg1) to the destination address (arg2)
. / copy.sh file1.txt / tmp/cat copy.shedding cards bind Bingray BASHCP $1 $2
3) how to calculate the parameters passed in?
$#
4) how do I get the script name in the script?
, 0
5) how to check whether the previous command was run 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 the third element of each line of a file?
Awk' {print $3}'
9) if the first element of each line in the file is FIND, how to get the second element
Awk' {if ($1 = = "FIND") print $2}'
10) how to debug bash scripts
Add the-xv parameter to #! / bin/bash
Example:
#! / bin/bash-xv
11) for example, how to write a function?
Function example {echo "Hello world!"}
12) how to concatenate two strings to?
V1 = "Hello" V2 = "World" V3 ${V1} ${V2} echo $V3
Output
HelloWorld
13) how to add two integers?
V1=1V2=2let V3=$V1+$V2echo $V3
Output
three
According to @ kashu, the better answer to this question is:
There are several ways to add two integers:
A=5B=6echo $(($Avalanche B)) # method 2echo $[$obtainable B] # method 3expr $obtainable B # method 4echo $Avalanche B | bc # method 5awk 'BEGIN {$A' +'"$B"'}'# method 6
14) how do I check if a file exists in the file system?
If [- f / var/log/messages] thenecho "File exists" fi
15) write all the loop syntax in the shell script?
For loop:
Foriin$ (ls); doecho item:$idone
While loop:
#! / bin/bashCOUNTER=0while [$COUNTER-lt 10]; doecho The counter is $COUNTERlet COUNTER=COUNTER+1done
Until loop:
#! / bin/bashCOUNTER=20until [$COUNTER- lt 10]; doecho COUNTER $COUNTERlet COUNTER-=1done
16) what does the #! / bin/sh or #! / bin/bash at the beginning of each script mean?
This line describes the shell to be used. #! / bin/bash indicates that the script uses / bin/bash. For python scripts, that is
#! / usr/bin/python .
17) how do I get line 10 of a text file?
Head-10 file | tail-1
18) what is 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 ordering "export"?
Make the variable available in the child shell.
21) how do I run a script in the background?
Add "&" after the script.
According to @ kashu, the better answer is:
Nohup command&
Most of the time we may use Linux remotely. I have encountered command running in the background due to network outage.
22) what does "chmod 500 script" do?
Give the script owner executable permissions.
23) what does ">" do?
Redirect the output stream to a file or to another stream.
24) what's the difference between & and &
&-you want the script to use it when it runs in the background
& &-use the current script when it completes successfully before executing subsequent commands / scripts
25) when should I use "if" before [condition]?
When you need to run multiple commands when the conditions are met.
26) Command: what is the output of name=John & & echo'My name is $name'
Variable
27) which symbol is used for annotations in bash shell scripts?
#
28) Command: what is the output of echo ${new:-variable}
Variable
29) what's the difference between quotation marks and quotation marks?
'- use it when we don't want to convert a variable to a value.
"- calculate the values of all variables and replace them with values.
30) how do I redirect standard output and standard error streams to log.txt files in a script file?
Add the "exec > log.txt 2 > & 1" command to the script file.
31) how do I use the echo command to get only a portion of a string variable?
Echo ${variable:x:y} x-start position y-length
Example:
Variable= "My name is Petras, and I am developer." echo ${variable:11:6} # will display Petras
32) if the string variable= "User:123:321:/home/dir" is given, how can I just use the echo command to get home_dir?
Echo ${variable#*:}
Or
Echo ${variable##*:}
33) how do I get "User" from the above string?
Echo ${variable%:*}
Or
Echo ${variable%%:*}
34) how do I use awk to list users whose UID is less than 100?
Awk-F:'$3