Experiment Four
Basic usage of 1.shell variables and usage of common symbols
(1) Change the main prompt to the user's home directory name
(2) Assign the string dos file c:> $student * to the variable x and display it
(3) In the shell command Terminal input likes= (Cosmos Galaxy Moon); Likes[6]=mars, and then uses echo to display the values of the following expressions, together with the result, to write out the function of the expression.
①${likes[*]} likes value of non-empty element
②${likes[@]} likes value of non-empty element
③${#likes [*]} The number of elements already set in the likes
④${#likes [@]} likes the number of elements already set
⑤${#likes} likes the length of the first element
⑥${#likes [0]} likes the length of the first element
(4) ${name:-hello} and unset name:
${name:-hello}
Declare this variable when name is not empty, or declare-after string if NULL
(5) ${name%%.} and ${name%.}
Function Analysis: ${name%%.*}: starts with the symbol at the end of the name. Match to delete the most matched portion.
${name%.*}: start with the symbol at the end of the name. Match to delete the least matched portion.
(6) ${name#/} and ${name#/}
Function Analysis: ${name#*/}: start with the symbol/match from the first name, and delete the least matched part.
${name##*/}: matches the symbol/match from the first name, deleting the most matched part.
(7) A student is known to submit a blog post page address as follows: address= ' http://www.cnblogs.com/xyz/p/8569001.html ' through string matching, how to get its blog home address: homepage= ' Http://www.cnblogs.com/xyz ' writes the shell command that gets the variable homepage from the address of the variable.
2.shell Script Analysis
(1) have ex1.sh script file
Summarize:
① what is the function of the entire shell script?
Reverse the parameter output
What is the function of ② line 8th, line 9th, and 11th?
8:cmd is the meaning of Echo \$ $count represents the last character where count=$ #表示参数的个数
9:count the number of corresponding parameters minus 1
11: Execute command in cmd
③ each run, the while loop in the script executes several times, respectively?
First 0 times, second 2 times, third 3 times
④ run. Ex1.sh God bless u, while the loop body is executed for the 1th time, the value of the variable cmd after the 8th line executes? {
echo $
(2) Use Vim/gedit to write shell script file ex2.sh, such as:
①line6, who | grep "^$1" function?
Determine if the user exists
②line8~10 function?
REDIRECT the first parameter of Hickeylu to limitstring and send Hello and limitstring
(3) Use Vim/gedit to write the shell script file ex3.sh, which reads as follows:
The function of the script: copy all. sh files from the current directory and append BACKUP-date-time.
(4) have ex4.sh script file
Run the script 3 times at the shell command terminal to see the results of its operation.
① run for the 1th time:. ex4.sh
② run for the 2nd time:. Ex4.sh/etc/apt/sources.list
③ Run for the 3rd time:. Ex4.sh/etc/apt/sources.list ex4.sh
Analyze the shell script function with the result of running. Reply:
What is the function of NL in ①line4-10?
Save user input parameters to x, add line number output
②line14, when a condition test is satisfied, what is the function of the script execution?
Execute function nl
③LINE17-LINE21, what is the function of this part of the script?
Pass this parameter to the NL call, and then shift to delete the parameter after the call is finished.
The next parameter moves left one bit, continuing the pass.
3. Shell Script Writing
This section requires that the shell script that implements the corresponding requirements be posted and the execution and results of the script are displayed
(1) Write a shell script that copies the second positional parameter and the file specified by each subsequent parameter to the directory specified in the first positional parameter.
(2) To print some files in a given directory, the first parameter indicates the directory where the files are located, and the remaining parameters are the names of the files to be printed.
(3) Use a For loop to move the. c files under the current directory to the specified directory, sorted by file size, and display the contents of the specified directory after the move.
(4) Design a program cuts, which reads the data by the standard input, obtains the data bounded by the first parameter N and the second parameter m, both N and m are integers, that is, all characters (including both characters) from the nth character to the M character from the input string.
(5) Guess the number game
The specific requirements are as follows:
① Write a function random0_100 generate random numbers between 0~100
② users from the keyboard input guessing numbers, if wrong, prompting the user to guess the number is too large or small, the user continues to guess until the right to guess.
(Hint: Shell internal system variable random generates an integer number between 0~32767)
Shell Programming (2)