1) How do I use parameters in a script?
First parameter: $ $, second argument: $ A
Example: The script will copy the file (arg1) to the destination address (ARG2)
./copy.sh file1.txt/tmp/
Cat copy.sh
#!/bin/bashcp $1 $2
2) How to calculate the parameters passed in?
$#
3) How do I get the script name in the script?
$
4) How do I check if the previous command was successful?
$?
5) How do I get the last line of a file?
tail-1
6) How do I get the first line of a file?
head-1
7) How do I get a third element for each line of a file?
awk ' {print $} '
8) How to get the second element if the first element in each row of the file is FIND
awk ' {if ($ = = ' FIND ') print $ '
9) How to debug a bash script
After adding the-XV parameter to #!/bin/bash
Example:
#!/bin/bash–xv
10) For example, how to write a function?
Function Example {
echo "Hello world!"
}
11) How to connect two strings?
v1="Hello"V2="World"V3=${v1}$ {V2}echo $V 3
Output
HelloWorld
12) How to add two integers?
A=5B=6Let C= $A + $B # method1Echo $Cecho $ ($A+ $B)) # method2Echo $[$A+ $B] # method3Expr $A+ $B # method4Echo $A+ $B | BC # Method5awk'Begin{print' "$A"'+' "$B"'}'# method6
Output
11
13) How do I check if a file exists in the file system?
if [-F/var/log/"File exists"fi
14) write out all the looping syntax in the shell script?
For loop:
#!/bin/bash
for in$ (LS); Do echo Item: $idone
While loop:
#!/bin/bashcounter=0 while]; Do is $COUNTER let COUNTER =counter+ 1 done
Until cycle:
#!/bin/bashcounter=ten ]; Do echo COUNTER $COUNTER let COUNTER-=1 done
15) What does the #!/bin/sh or #!/bin/bash of each script start mean?
This line describes the shell to use.#!/bin/bashIndicates that the script uses/bin/bash. For Python scripts, this is #!/usr/bin/python.
16) How do I get to the 10th line of a text file?
head-10 file|tail-1
What is the first symbol of a bash script file?
#
18) Command: [-Z "] && echo 0 | | What is the output of echo 1?
0
19) What is the use of the command "Export"?
Make the variable available in the child shell.
20) How do I run scripts in the background?
Add the following script"&"。
What do you do with "chmod script"?
Enables the script owner to have executable permissions.
what does ">" Do?
Redirects the output stream to a file or to another stream.
& What is the difference between &&
&-Use it when you want the script to run in the background
&&-use it when a script is successfully completed before executing the command/script.
24) When do you want to use "if" before [condition]?
You need to run multiple commands when the condition is met.
) which symbol in the Bash shell script is used for comments?
#
26) Command: What is the output of Echo ${new:-variable}
variable
27) What is the difference between ' and ' quotes?
'-Use it when we don't want to convert the variable to a value.
"-The values of all variables are computed and replaced by values.
28) How do I redirect standard output and standard error streams to the Log.txt file in a script file?
Adding in the script file"exec >log.txt 2>&1"Command.
29) How do I get only part of a string variable with the echo command?
Echo ${variable:x:y}
x-Starting position
y-Length
Example:
variable= "My name is Petras, and I am developer."
Echo ${variable:11:6}# will show Petras
30) If the given string variable= "User:123:321:/home/dir", how can I get home_dir with the echo command only?
echo ${variable#*:*:*:}
Or
echo ${variable##*:}
31) How do I Get "User" from the string above?
echo ${variable%:*:*:*}
Or
echo ${variable%%:*}
32) How can I use awk to list users with a UID of less than 100?
awk-f: ' $3<100 '/etc/passwd
33) Write the 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 Do 2 done
34) How do I change the standard domain delimiter in the bash shell to ":"?
ifs= ":"
35) How do I get the variable length?
${#variable}
36) How do I print the last 5 characters of a variable?
echo ${variable:-5}
PNS) ${variable:-10} and ${variable:-10} What's the difference?
${variable:-10}-Output 10 If you have not previously assigned a value to the variable, and if there is an assignment, output the variable
${variable: -10}-Output variable The last 10 characters
38) How can I replace only part of a string with the echo command?
Echo ${variable//pattern/replacement}
39) Which command replaces the command with uppercase?
TR ' [: Lower:] ' [: Upper:] '
40) How to calculate the number of local users?
wc-l/etc/passwd|cut-d ""-f1Orcat/etc/passwd|wc-l
41) How do I calculate the number of words in a string without the WC command?
set ${string}
Echo $#
" export $variable" or "Export variable" which is correct?
Export Variable
43) How to list the second letter is a or b file?
ls-d? [Ab]*
44) How do I add an integer A to B and assign a value to C?
c=$ ((a+b))
Or
c= ' expr $a + $b '
Or
c= ' echo ' $a + $b "|BC"
45) How do I remove all the spaces in a string?
echo $string |tr-d ""
46) Write out the output number 0 to 100 in multiples of 3 (0 3 6 9 ...) 's orders?
for inch {0.. . 3 }; Do echo $i;d One
Or
for ((i=0; i<=; i=i+3 )); Do "Welcome $i Times";d one
47) How do I print all parameters passed to the script?
Echo $*
Or
echo [email protected]
[$a = = $b] and [$a-eq $b] What's the difference?
[$a = = $b]-For string comparisons
[$a-eq $b]-For digital comparisons
49) = and = = What is the difference
=-Used to assign a value to a variable
= =-For string comparisons
50) Write the command that the test $a is greater than 12?
[$a-gt]
51) Write the command that the test $b is less than or equal to 12?
[$b-le]
52) How do I check if a string begins with the letter "abc"?
[[$string = = abc*]]
[ [$string = = abc*]] and [[$string = = "abc*"]] What's the difference?
[[$string = = abc*]]-Check whether the string starts with the letter ABC
[[$string = = "abc"]]-check if the string is exactly equal to ABC
54) How do I list user names that start with AB or XY?
egrep "^ab|^xy"/etc/passwd|cut-d:-f1
in bash) $! What does that mean?
The PID of the recently executed command in the background.
56) $? What does that mean?
The end state of the last command in the foreground.
57) How to output the current shell PID?
Echo $$
58) What is the difference between $* and [email protected]
$*-Output all parameters passed to the script as a string
[email protected]-List all parameters passed to the script as a $IFS delimiter
59) How do I define an array in bash?
array= ("Hi" "My" "Name" "is")
60) How do I print the first element of an array?
echo ${array[0]}
61) How do I print all the elements of an array?
echo ${array[@]}
62) How do I output all the array indexes?
echo ${!array[@]}
63) How do I move an element with an index of 2 in the divisor group?
unset array[2]
64) How do I add an element with ID 333 in an array?
array[333]= "New_element"
How does the shell script get the input value?
A) by parameter
./script param1 param2
b) through the Read command
read-p "Destination backup Server:" Desthost
66) How do I use "expect" in the script?
/usr/bin/expect <<-"*?assword:*""${ password}\r"expect Eofeod
Classic Shell Scripting issues