1. (1) Change the main prompt to the user's home directory name (hint: reference material 4.6.8 section environment variable PS1 and HOME usage)
(2) Assign the string DOS file c:>\ $student \* to the variable x and display it (hint: note the choice of quotation marks while ensuring that multiple spaces, $, *, and so on in the string are displayed exactly as they are)
(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[@]}③${#likes [*]}④${#likes [@]}⑤${#likes}⑥${#likes [0]}
①${likes[*]} array likes non-null values
②${likes[@]} ibid .
③${#likes [*]} the number of likes already set in the array
④${#likes [@]} Ibid .
⑤${#likes} The length of the first element "mars" in the array likes
⑥${#likes [0]} ibid .
(4) In the shell command Terminal input Name=phoenix, and then use the Echo display expression ①, observe the results, and then enter the command unset name, and then enter the expression ① observation results. Combine two results to write the function of the expression. ①${name:-hello}
Use variable when name is not empty:- string assignment is not valid.
and unset removes the name after it is empty, so the assignment "hello"
(5) Enter Name= '/usr/share/doc/apg/php.tar.gz ' at the shell command terminal, then use echo to display the values of the expression ① and ② respectively, and observe the results. Modify the value of name, let Name= '/etc/apt/sources.list.d ', and again use echo to display the values of the expression ① and ② respectively, and observe the results. Combining the results, write out the function of the expression. ①${name%%.*}②${name%.*}
${name%%.pattern} and ${name%.pattern} if the pattern and name Value , the value of the expression is the value of the expression after the match is removed from The name value
${name%%.pattern} is the most matched part removed, only the gz
the part that is removed in ${name%.pattern} is the least matched part, leaving only the first part
(6) Enter name=/usr/bin/x11 at the shell command Terminal, and then use echo to display the values of the expression ① and ② respectively, and observe the results. Modify the value of name, let Name= '/etc/apt/sources.list.d ', and again use echo to display the values of the expression ① and ② respectively, and observe the results. Combining the results, write out the function of the expression. ①${name#*/}②${name##*/}
Similar to (5), the difference is that % means matching from the end,# means matching from the beginning
(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. Script Analysis
(1) #!/bin/bash
count=$#
Cmd=echo
While [$count-GT 0]
Do
cmd= "$cmd \$ $count"
count= ' Expr $count-1 '
Done
Eval $cmd
① the entire program to reverse the output of the parameter
in script ex1.sh ,$# is a special Shell variable that refers to the number of parameters.
②cmd= "$cmd \$ $count" $cmd echo,\$ $count represents the last parameter, the entire statement represents the last parameter of the output
count= ' Expr $count-1 ' indicates a reduction in the number of parameters
Eval $cmd represents a command run in cmd
③ first command loop 0 times, second 2 times, third 3 times
④Echo $
(2)
①who | the function of grep "^$1" is to determine whether the WLL exists in the current system
the function of ②line8~10 is to redirect the parameter wll to limitstring and send the Hello .
(3)
function to be will be look for the SH format files , copy them to the new file, the new file name is the original filename and add "backup--Current time (month and day) ..."
(4)
Line4-10 function to read input variable into x , output line number and the variable in the next line
Line14 When conditions are met, the script calls the NL function, which is the above function
The LINE17-LINE21 function reads the parameters into the function NL ,the shift command shifts the parameters left one bit, and the leftmost parameter is discarded.
3. Scripting
(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 file under the current directory to the specified directory and sort by file size to display the contents of the specified directory after the move
(3) design a program cuts, which reads data from the standard input, obtains the data boundedby the first parameter n and the second parameter m ,N and m are integers, that is, extracting the nth character from the input string to the m All characters between characters, including the two characters. For example:
$cuts 11 14
This was a test of cuts program (input)
Test (Result)
5. guess the number of games, the specific requirements are as follows:
① Write a function random_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.
Shell Programming (2)