Chapter 8 Homework Questions

Homework questions:

1. Set the environment variable HISTSIZE so that it can save the history of 10,000 commands.

vim /etc/profile Modify HISTSIZE=10000

2. Why if the result displayed by PS1 (PS1="[\u@\h \W]\$ ") is different from what we expected, how to set it to restore the original default?
 Escape the double quotes. Double quotes can be changed to single quotes. (PS1='[\u@\h \W]\$ ')

3. Find a way to replace all lowercase letters in the file names of the files in the current directory with uppercase letters.
  for i in `ls`;do mv $i `echo $i|tr '[az]' '[AZ]'`;done


4. Use sort to sort paragraph 5 of the /etc/passwd file with ":" as the delimiter.
   sort -t ":" -k5 /etc/passwd

5. Use cut to cut out the third character of /etc/passwd with ":" as the delimiter.
    cut -d ":" -f 3 /etc/passwd


6. Briefly describe the functions of these files: /etc/profile, /etc/bashrc, .bashrc, .bash_profile.
./etc/profile No matter which user, this file will be read when logging in.
/etc/bashrc When bash is executed, no matter what the method is, this file will be read. bashrc
When bash is executed in non-login mode, this file will be read.
.bashrc_profile This file is read when bash is executed as login. Usually this configuration file is also
configured to read .bashrc.


7. What is the role of export?

Export means to declare a variable, so that the subshell of the shell also knows what the value of the variable is. If no variable name is added after export, it will declare all variables.

8. What are the rules for custom variables under linux?  

a. Set the format of the variable as "a=b", where a is the variable name, b is the content of the variable, and there can be no spaces on both sides of the equal sign;

b. The variable name can only consist of letters, numbers and underscores, and cannot start with a number;

c. When the variable content contains special characters (such as spaces), single quotation marks need to be added;

d. If the variable content needs to use Backticks can be used for the running results of other commands;

e. The contents of variables can be added to the contents of other variables, and double quotes are required;


9. How to throw the command to be run in the background? How to transfer the process running in the background to the foreground?
Ctrl+z or add &fg +job number at the end of the command


10. List the files and directories starting with "test" in the current directory.

ls -d test*

11. How to not only print the output of a command to the screen but also redirect it to a file?

echo xxxx |tee 1.txt print xxxx to the screen and input to 1.txt

12. If there is a long command, how can we use a simple string instead of this complex command? Please give an example.
alias dns='vi /etc/resolv.conf' Then enter dns on the command line to enter the /etc/resolv.conf file


13. How can I implement such a function, throw a command to the background to run, and redirect its correct output and error output to a file at the same time?
Example: cat 2.txt 3.txt>1.txt 2>&1 & (2.txt is an existing file and 3.txt is a non-existing file) 1.txt contains correct output and incorrect output


14. How to separate a large file according to the size (if according to 10M), and how to separate according to the number of lines (if 10000 lines)?

split -b10M bigfile splitname #Split by 10M per file

 split -l10000 bigfile splitname #Split according to 10000 lines of each file

15. Do the experiment and figure out the meaning of these three symbols; && ||.

&& After the previous command is executed successfully, the following command will be executed; if the previous execution is unsuccessful, the latter command will not be executed

|| If the execution of the previous command is unsuccessful, the latter command will be executed; if the execution of the former command is successful, the latter command will not be executed.

; Whether the command on the left is successful or not, the following commands will be executed

16. What if you only want a certain user to use a certain variable?
.vim ~/.bashrc write export variable = variable content save source ~/.bashrc 


17. Which command will be used to list all the variables in the system and the current user-defined custom variables?
set

extended reading:

"PS1" of Linux environment variables  http://www.lishiming.net/thread-5364-1-1.html

Linux supports Chinese  http://www.lishiming.net/thread-5360-1-1 .htmlKeep

command history permanently and add timestamp  http://www.lishiming.net/thread-283-1-1.html

/etc/profile, /etc/bashrc, ~/.bash_profile, ~/. What does bashrc do  http://www.lishiming.net/thread-909-1-1.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325440204&siteId=291194637