1) How do I pass parameters to a script?
./script argument
Example: show file name script
./show.sh file1.txt cat show.sh #!/bin/bash cat $1
2) How do I use parameters in scripts?
First parameter: $1, second parameter: $2
Example: the script will copy the file (arg1) to the destination address (arg2)
./copy.sh file1.txt /tmp/ cat copy.sh #!/bin/bash cp $1 $2
3) How to calculate the parameters passed in?
$#
4) How to get the script name in the script?
$0
5) How to check whether the previous command runs successfully?
$?
6) How do I get the last line of a file?
tail -1
7) How do I get the first line of a file?
head -1
8) How to get the third element of each line of a file?
awk '{print $3}'
9) If the first element in each line of the file is FIND, how to get the second element
awk '{ if ($1 == "FIND") print $2}'
10) How to debug bash script
take -xv Parameter add #!/ After bin/bash example: #!/bin/bash –xv
11) For example, how to write a function?
function example { echo "Hello world!" }
12) How do I connect two strings to a?
V1="Hello" V2="World" 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
14) How do I check if a file system exists?
if [ -f /var/log/messages ] then echo "File exists" fi
15) Write out all the loop syntax in the shell script?
for 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 loop : #!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ]; do echo COUNTER $COUNTER let COUNTER-=1 done
16) Every script starts #/ bin/sh or #/ What does bin/bash mean?
This line describes the shell to use. #/ bin/bash indicates that the script uses / bin/bash. For Python scripts, it's #/ usr/bin/python. (LCTT translation note: this line is called Release companion. )
17) How do I get line 10 of a text file?
head -10 file|tail -1
18) What is the first symbol in the bash script file
#
19) Command: [- Z ""] & & echo 0 | what is the output of echo 1
0
20) What's the use of the command "export"?
Make the variable in the child shell Available in.
21) how to run scripts in the background?
Add after script“&".
22) "chmod 500 script" for what?
Make the script owner executable.
23) what does ">" do?
Redirect the output stream to a file or another stream.
24) & what's the difference between & & and &
& – You want the script to use it when it runs in the background && – The following commands cannot be executed until the current script is successfully completed/Use it when scripting
25) when should I use "if" before [condition]?
When multiple commands need to be run when the conditions are met.
26) command: name = John & & echo 'what is the output of My name is $name'
My name is $name
27) which symbol in Bash shell script is used for annotation?
#
28) what is the output of the command: echo ${new:-variable}
variable
29) what is the difference between 'and' quotation marks?
' – Use it when we don't want to convert a variable to a value. " – The values of all variables are calculated and replaced with values.
30) how to redirect standard output and standard error stream to log in script file Txt file?
Add to script file“ exec >log.txt 2>&1″ Command.
31) how to get a part of a string variable only with 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} # Petras is displayed
32) if the string variable = "User:123:321:/home/dir" is given, how can I get home only with echo command_ dir ?
echo ${variable#*:*:*:} or echo ${variable##*:}
33) how to get "User" from the above string?
echo ${variable%:*:*:*} or echo ${variable%%:*}
34) how to use awk to list users with UID less than 100?
awk -F: '$3<100' /etc/passwd
35) write a program to calculate the number of main groups for users and display the times 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 change the standard field separator to ":" in bash shell?
IFS=":"
37) how to obtain the variable length?
${#variable}
38) how to print the last 5 characters of variables?
echo ${variable: -5}
39) what is the difference between ${variable: -10} and ${variable: -10}?
${variable:-10} – If you haven't given it before variable Assign value and output 10 ${variable: -10} – output variable Last 10 characters of
40) how to replace a part of a string only with echo command?
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 perhaps cat /etc/passwd|wc -l
43) how to calculate the number of words in a string without wc command?
set ${string} echo $#
44) "export $variable" or "export variable" which is correct?
export variable
45) how to list documents whose second letter is a or b?
ls -d ?[ab]*
46) how to add integer a to b and assign it to c?
c=$((a+b)) or c=`expr $a + $b` or c=`echo "$a+$b"|bc`
47) how to remove all spaces in a string?
echo $string|tr -d " "
48) rewrite this command to convert the output variable into a complex number: item = "car"; echo “I like $item” ?
item="car"; echo "I like ${item}s"
49) write the command to output the multiple of 3 (0 3 6 9...) from 0 to 100?
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
How do I pass all the parameters printed to 50)?
echo $* or echo $@
51) [$a = = $b] what's the difference between [$a -eq $b]
[ $a == $b ] – For string comparison [ $a -eq $b ] – For digital comparison
52) = and = = what's the difference
= – Used to copy variables == – For string comparison
53) write a command to test whether $a is greater than 12?
[ $a -gt 12 ]
54) write a command to test whether $b is less than or equal to 12?
[ $b -le 12 ]
55) how to check whether a string starts with the letter "abc"?
[[ $string == abc* ]]
56) what is the difference between [[$string = = ABC *]] and [[$string == "abc *"]]
[[ $string == abc* ]] – Check whether the string is alphabetic abc start [[ $string == "abc" ]] – Check whether the string is exactly equal to abc
57) how to list user names starting with ab or xy?
egrep "^ab|^xy" /etc/passwd|cut -d: -f1
58) bash $! What do you mean?
The number of recently executed commands in the background PID.
59) $? What do you mean?
The end state of the most recent command in the foreground.
60) how to output the PID of the current shell?
echo $$
61) how to get the number of parameters passed to the script?
echo $#
62) what's the difference between $* and $@
$* – Output all parameters passed to the script as a string $@ – with $IFS Lists all parameters passed to the script for the delimiter
63) how to define an array in bash?
array=("Hi" "my" "name" "is")
64) how to print the first element of the array?
echo ${array[0]}
65) how to print all elements of an array?
echo ${array[@]}
66) how to output all array indexes?
echo ${!array[@]}
67) how to remove the element with index 2 from the array?
unset array[2]
68) how to add an element with id 333 to the array?
array[333]="New_element"
69) how does the shell script get the input value?
a) Pass parameter
./script param1 param2
b) Through the read command
read -p "Destination backup Server : " desthost
70) how to use "expect" in the script?
/usr/bin/expect << EOD spawn rsync -ar ${line} ${desthost}:${destpath} expect "*?assword:*" send "${password}/r" expect eof EOD