Objective
???? Officially write
Body
- Demand simulation
For example, one day we made a very good software.
For security purposes, the software is not recommended to run with the root account at runtime. So we'll have an init shell script to create the user and initialization file
????
1, to determine whether the user exists (for example, God), did not create
2. Set password to 123 after creation
3, to copy some program files in the past, and in addition to root, can only God user run (preferably root is not easy to run)
?
?
- grep command searches for files and contents in files
Powerful Text Search tool that can use regular expressions to search for text and print matching lines.
This is also one of the most used commands for routine maintenance and system deployment.
???? 1. Format
grep "The string you want to retrieve" file name
grep "The string you want to retrieve" file name *
grep ' regular ' file name
Cat file name | grep "You like the string"
ls | grep "You like the filename"
???? 2. Common parameters
???? -C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
????
Example: CAT/ETC/PASSWD | grep ' ^pangzi '-C
- stdin non-interactive input analog keyboard
Using passwd to change passwords requires prompt and confirmation if we force setup OK?
echo "123" | passwd User name--stdin
stdin standard input (keyboard)
--stdin represents the use of non-interactive mode input, the previous statement is a pipeline
- shell1
USER _count= ' cat/etc/passwd | grep ' ^god: '-C '
user_name= ' God '
if [$USER _count-ne 1]
then
???? Useradd $USER _name
???? echo "123" | passwd $USER _name--stdin
else
P style= "text-align:justify" >???? echo ' OK ' fi
?
Cons: users cannot create and modify passwords repeatedly
- Powerful markers (partial)
$ #传递到脚本的参数个数
$$ the current process ID number for the script to run
$! Process ID Number of the last process running in the background
[Email protected] and $ #相同, but use quotation marks and return each parameter in quotation marks
$? Displays the exit status of the last command. 0 means no error, and any other value indicates an error (or unsuccessful execution)
?
?
- shell2 optimized repeatable execution
User_count= ' cat/etc/passwd | grep ' ^god: '-C '
User_name= ' God '
?
Useradd $USER _name
If [$?-eq 0] #判断上一个命令的执行状态, 0 is a failure
Then
echo "123" | passwd $USER _name--stdin
echo "Done"
Else
echo "Error"
Fi
?
End
17. Create a user's grep command and a powerful $