Operating system experimental class notes

ctrl+alt+t open the black box

1 switch to super user

su root or su-
and then enter the password (the password is not displayed * nor the plain text is displayed and press Enter after the input is complete)
ordinary user is $ root user is #
Insert picture description here

1.1 Shutdown and restart (requires super user permissions)

Enter shutdown -h now to shut down the system
Enter shutdown -r now to restart the system


2. Basic commands

2.1 Common commands

pwd view the current path
ls view the files and folders under the current path (blue is the folder and white is the file)
cd folder name jump to the specified folder
cd / jump to the root directory
(return to the two points of the previous cd) //There is a space in between

mkdir folder name create folder (create by make)
rmdir folder name delete folder (remove delete)
rm -rf folder name delete non-empty folder and its sub-files

cat filename to view the contents of the file
rm filename delete files
move to create a copy delete files / folders can be multiple simultaneous operating spaces separated to

History displays all previous commands.
Cal displays the current month calendar. There are two common methods: cal 2019/cal 4 2020
clear to clear the screen (all previous operations are still valid and can be viewed by entering history)

Copy and move files (no need to sudo when super user, normal user add sudo and enter password)
cp file path new folder path copy file to new folder
cp file path new folder path/new name copy file to new file Folder and rename
the path of the mv file. New file name. Rename the
file. Path of the mv file. New folder path. Move the file to the new folder.
Path of the mv file. New folder path/new name. Move the file to the new folder and rename
( If the file and the new folder are in the current path, you can omit the path and write only the name

. After the above operation, file and file1 have entered ff2 and f2 is no longer in file2.
Multiple commands can be run at the same time, separated by semicolons. can

man chapter number command name Find the specified command in the specified chapter
eg: man 3 fopen
Insert picture description here

2.2 Create files (three types):

2.2.1 Use cat command

cat> filename
and enter the contents of the file.
Enter ctrl+d to end and enter
cat >> filename to append the contents of the file
cat> filename to overwrite the contents of the file
cat filename view the contents of the file
Insert picture description here

2.2.2 Use vi editor

There are three modes of vi editor: command mode, editing mode (i), bottom line command mode (:)
vi filename creates a file and opens the vi editor. The
first thing you enter is the command mode. After you enter i , you can enter the contents of the file and press esc. Key to return to the command mode and then press ":" to jump to the bottom command mode
Save the content of the file and exit the vi editor and return to the interface wq
if the content has not been modified, exit and enter q
If forcibly exit, enter q!
Insert picture description hereInsert picture description here

2.2.3 Use the touch command

touch filename creates an empty file

3. Create and delete users

useradd id create user id
userdel id delete user id
useradd -f user name : force delete user, even if the user is currently logged in
useradd -r user name : delete user at the same time, delete all files related to user
passwd user name : set for user The password
su id detects whether the user has been deleted (success if it prompts unknown)
Insert picture description here


4. Process

4.1 Process classification

User process : associated with the terminal.
System process : the process loaded by the system environment platform after the operating system is started.
Daemon : there is no screen display, just waiting for user or system requests in the background.
It can also be divided into foreground process and background process

4.2 Process related commands

4.2.1 Foreground process

Create foreground process Command to
view foreground process ps
default display this user To display all users ps-af
-e display all processes (user + system)
-l long format (more information)
-w wide output (default)
-r Only show running processes
-m show thread information
-x show processes that do not control the terminal
-f show detailed information about processes
Insert picture description here

4.2.2 Background process

Create a background process : Command &
view background process : jobs
put a command that is being executed in the foreground into the background, and pause : ctrl + z
a suspended background process goes to the background running state : bg background process number
background process goes to the foreground process : fg background process number (the number of the process is the number)
delete process : kill -9 PID number (-9 means forced deletion can be saved)
Insert picture description here

4.2.3 View the current process dynamically

Dynamically display the current process command : top
only displays a certain user process : press u , enter the user name , and press Enter to
kill the process : press k to enter the process PID number and press Enter twice. If you cannot delete the above steps, press 9 to force delete and
exit the top environment Return to the command line : press q/Q

4.2.4 Create an instruction to execute the job for a certain period of time in the future

Format: at time, carriage return input command ctrl+d to end input

Specify to execute certain commands at 5:30 this afternoon (am 12-hour format in the morning and pm in the afternoon) Insert picture description here

View the list of undoed jobs: at -l
delete undoed jobs: at -d ID
Insert picture description here


5. An experiment of the vi editor

1. Insert the 2020 calendar and name the file 2020.txt.

 vi 2020.txt 
 :r!cal 2020

2. Display the line number.

:set number 
或者 :set nu

3. Move the calendar for April, May, and June one tab stop to the right.

12G 8>>

(12G means that the cursor is positioned on the 12th line 8>> means that the following eight lines are all moved one tab stop to the right)

4. Change the title of the calendar "2020" to "2020 Annual Calendar".

2G :s/2020/2020年全年日历

5. Delete the calendars of January, February, and March, and then restore them.
The cursor is positioned on the first line of the calendar for January, February, and March.
Delete d9G to cancel u (d9G means to delete 9 lines below the cursor)

6. Move the calendar of July, August, and September to the end of the calendar as a whole.
Position the cursor on the first line of the calendar for July, August, and September, cut 8dd ( 8yy if copied),
move the cursor to the last line and paste p

7. Retrieve the string "30" in turn, and delete "30" if the last day of each month is "30".
:/30 Enter to
see if there is 31 in the month where 30 is located, if not, press " x " to delete 30; otherwise press " n " to continue searching

8. Count the size of the file in the vi editor.
:!wc 2020.txt (If it doesn’t work once, enter it several times)


6. About the shell

1. The establishment of shell scripts : Like cat and text files can establish vi command
2. run the shell script :
1> sh <script name
> sh script under 2
3> The user to add executable permission to the script after ./ Script name
ps: chmod a+x Script name //It is the code to add executable permissions to the script. For more permissions, please refer to Chapter 3 of point 8 of this article.
Examples are as follows:
Insert picture description here
3. Assignment and output of shell variables.
Variable name=variable Value // Directly assign
echo variable name // output variable name
echo $ variable name // output variable value
Examples are as follows:
Insert picture description here


7. Use a shell script to complete a small task

According to the keyboard, you can input the student's grades (percentile system), and display the corresponding grade standards (pass and fail), press the Q key to exit, and press other keys to prompt you to re-enter.

code show as below:

cat > t//先创建一个脚本
while true//死循环直接这么写就可
do
read a//创建一个变量来读取输入
if [ "$a" = "Q" ]  //判断字符串是否相等用"="且中间必须有空格
then break
elif [ $a -ge 60 -a $a -le 100 ] //-ge大于等于 -le小于等于 -a表示逻辑与等同于&&
then echo "pass"
elif [ $a -ge 0 -a $a -lt 60 ]  //-lt表示小于 每个空格都是必要的~
then echo "fail"
else echo “please input again”
fi
done
sh t//运行脚本

Run screenshots:
Insert picture description here
The problem that the program needs to pay attention to: all spaces can't be saved

Knowledge points:
shell input and output commands:
input: read variable 1 variable 2…
output: echo "The content to be output can be a mixture of variables and characters"
example is as follows:
Insert picture description here
string judgment:
equal use = unequal use! = and before and after A
space is required ** to separate the
numerical comparison: (a space is required to separate the front and back)

-eq 相等  
-ne 不等
-lt 小于
-le 小于等于
-gt 大于
-ge 大于等于

Logical judgment:

!逻辑非
-a 逻辑与
-o 逻辑或

The format of the if statement:

if[条件] then
elif[条件] then //可无
...
else then
fi

The format of the while statement:

while [表达式]
do
语句体
done

To sum up: the if statement uses fi to indicate the end, while uses done to indicate the end of the judgment statement in the shell, all use brackets

Guess you like

Origin blog.csdn.net/qq_43760243/article/details/105972577