This essay is Linux experiment 3, the main content is the basic use of shell
a special character
Two: shell command application practice
①:/etc/passwd:
Each line of /etc/passwd represents a user account. Use: to split seven fields, such as:
The last one, from front to back, is:
Login name, optional encrypted password, numeric user ID, numeric group ID, username and comment fields, user home directory, optional user command interpreter
②:/etc/shadow
shadow is a file that contains password information and optional age information for system accounts, each line contains 9 fields, separated by: , such as
From front to back are
log-in name;
Encrypted password, the date the password was last changed, the number refers to the number of days since January 1, 1970;
The period when the password was last changed
The minimum age of the password, that is, the limit time between two modifications, if it is 0, it means no limit;
The maximum password age, that is, after the corresponding time, the user must change the password. After the time expires, the password is still available, but the user will be required to change the password at the next login. An empty field indicates that there is no maximum password age. If the maximum password age is less than the minimum age, it cannot be changed;
The password warning time period, that is, the number of days to warn the user in advance before the password expires, is 7 in the above figure
Password disable period, that is, the number of days that the password can still be used after it expires. If this grace period expires, you will not be able to log in after that, and you need to contact the system administrator. An empty field means that there is no mandatory password expiration.
Account expiration time. Note that account expiration is different from password expiration. When the account expires, the user will not be allowed to log in; when the password expires, the user will not be allowed to log in with their password. An empty field means never expires
Reserved field, this field is reserved for future use.
③:/etc/group
This is the user group file, which saves the groups in the system, each group occupies one line, separated by:, such as
From front to back, they are the group name, encrypted password, GID, and the list of members of this group
④:/etc/gshadow
Shadowed group files, containing shadowed group account information, separated by :, such as
It can be seen that, similar to the figure in ③, from front to back are the group name, encrypted password, administrator, member
⑤:id
The id command displays the user's id, the user's group id, followed by the group id of the group where guqi1901 belongs.
id -u only displays the id of the user, followed by a specific user such as root to display the information of the corresponding user
⑥: Search
which: where to find the executable
whereis: Displays related files for the specified command, including binaries, source code files, and man pages
locate: Find the specified document in the file system. It should be noted that locate is faster than find, because it does not really find files, but the database
find: search in the specified directory, you can connect the -name option, indicating that the specified string is used as a template style for finding files or directories
⑦: grep and regular expressions
grep "match_pattern" file_name is to search for the result matching the regular expression under the corresponding file
Some commonly used wildcards:
*: matches any character of any length
? : matches any single character
[]: matches any single character within the specified range
[^]: matches any single character outside the specified range
[:space:]: whitespace character
[:punct:]: punctuation mark
[:lower:]: lowercase letters
[:upper:]: uppercase letters
[:alpha:]: uppercase and lowercase letters
[:digit:]: digit
[:alnum:]: numbers and uppercase and lowercase letters
So grep -n -E 'root|guqi1901|^user*' /etc/passwd shows the line containing root or guqi1901 or the letter starting with user in the corresponding file
grep -n -E ' [[:digit:]] ' /etc/passwd displays the lines containing numbers in the corresponding file
grep -n -E ' [[:alpha:]] ' /etc/passwd displays the corresponding file Lines containing letters
grep -n '[0-9]\{4,\}' /etc/group show lines containing four consecutive numbers in the corresponding file
⑧: Install and uninstall
sudo apt install gimp install gimp
which gimp looks for the location of the gimp executable
sudo apt remove gimp uninstall gimp
which gimp looks again to confirm removal
⑨ : ls
ls -l --time-style=long-iso List all times in the form of long-iso
ls -l --time-style=long-iso -t Sort by time
ls -l --time-style=long -iso -t -r reverse order from above
⑩:cp gzip
ls /usr/share/man Open the corresponding directory
ls /usr/share/man | grep man[1-8] Check if there are man1—man8 files in this directory
ls /usr/share/man/man1 Open the corresponding directory
file /usr /share/man/man1/ls.1.gz (combined with the execution results, review the contents of the gzip command in section 2.8.1) View the corresponding file information
mkdir ~/temp; cp /usr/share/man/man1/ls.1.gz ~ /temp Create a new dir under home/user/, copy the file into
cd ~/temp; ls enter temp
sudo gzip -d ls.1.gz; ls decompress the file
⑪: Use of wildcards
ls -l /home | grep "^d" | wc -l Find the number of folders under the corresponding folder, ^d is used to determine the file type
sudo adduser user7 Add a new user user7
ls /home | tee users | wc -l List the information in the home, write it to the users file, and count the number of lines
⑫: Four examples
Search for the file signal.h in the directory /usr/include if it exists find /usr/include -name signal.h
Find the line containing BUFSIZ in all files in the /usr/include directory and display the line number find /usr/include -exec grep -nH "BUFSIZ" {} \; 2>/dev/null
Find the user information record whose login shell is bash in the username and password file /etc/passwd grep -n 'bash' /etc/passwd
Cut the first column (group name) and the third column (group id) from the /etc/group file, and sort them according to the numerical value of the group id number from small to large cat /etc/group | cut -d':' - f1,3 | sort -t ':' -k 2n
awk, sed
/etc/apt/sources.list is a configuration file used by the package management tool apt to record the location of the package repository, such as
From left to right are:
File type: deb indicates that the type is a binary precompiled package, which is the file type we generally use; dev-src is the source code used to compile binary packages
Warehouse address We can change the warehouse address to a mirror that is geographically closer to ourselves to improve the download speed. The above picture is not the default address.
Distribution There are two classification methods for distribution, one is the specific code of the distribution, such as xenial, trusty, precise, etc., and the other is the distribution type of the distribution, such as oldstable, stable, testing and unstable. There may be further specifications such as xenial-updates, trusty-security, stable-backports, etc. after the release.
Package classification In ubuntu, main is the officially supported free software. restricted Officially supported software that is not fully free. Free software maintained by the universe community. multiverse non-free software.
sed is a file processing tool, which is mainly processed in line units, and can replace, delete, add, and select data lines.
The commonly used command s represents replacement work, the format is sed 's/string to be replaced/new string/g'
awk is a language for processing text files and is a powerful text analysis tool. For example, awk '{[pattern] action}' {filenames} represents a line matching statement, where $0 represents the entire line, and $n represents the nth word
⑭: curl and wget
The curl command is a file transfer tool that uses URL rules to work under the command line. It supports file uploading and downloading, but it is customary to call curl a download tool. curl supports many protocols including HTTP, HTTPS, ftp, etc. It also supports POST, cookies, authentication and other functions.
The wget command is used to download files from the specified URL. wget is very stable. It has strong adaptability in the case of narrow bandwidth and unstable network. If the download fails due to network reasons, wget will keep trying until the entire file is downloaded. If the server interrupts the download process, it will reconnect to the server and resume the download where it left off. This is useful for downloading large files from servers with limited link times.
Three: Two shell scripts
filename=backup-$(date +%Y%m%d%H%M) file name is current time
tar -cJf - 'find . -mtime -1 -type f -print' >${filename}.tar.xz 2>error_info Write the information of the document modified in the last day to the corresponding tar.xz
echo "backup archive file:"
ls ${filename}.tar.xz shows the successfully compressed file
echo
echo "fail to backup:"
cat error_info shows the wrong information
#!/bin/bash
# exercise for shell variable
read -p 'Please enter your student number: ' -n11 stu_no limit to read up to 11 characters, end when the number is read
grade=${stu_no:0:4} find Get the first five
num=${stu_no:(-3)} Find the last three
code=${stu_no:4:4} Find the middle four
printf "\n"
printf "You are a $grade student.\n "
printf "Your major code is $code.\n"
printf "The last three digits of your student number are $num.\n"
filename="major_code.txt"
if [ -f $filename ]
then
grep $code $filename > t1 && read x major < t1 search code and filename redirect to t1, then output from t1 to major
if [ "$major" ]
then
echo "Your major is: $major."
else
echo "Can't find your major."
fi
else
echo "Can't find file $filename."
fi
unset grade num code x major
Four: Summary
1. Spaces cannot be added at will, and many places will not produce ambiguity for high-level languages.
2. The proficiency of basic commands directly affects shell programming :-)