1. Write Script/root/bin/systeminfo.sh, display the current host system information, including hostname, IPV4 address,
OS version, kernel version, CPU model, memory size, hard disk size.
Analysis:
Explain:
This is a stack of commands, and it's easy to know some of the results from the command line by command.
Host name: hostname command.
IPV4 Address: ifconfig command is used to view the IP address, but there are a lot of superfluous content, then we can pass the regular expression,
and the cut command to intercept.
Operating system version: can be found using Cat/etc/redhat-relase.
Kernel version: Uname-r yes.
CPU Model: There is a file about the CPU that is/proc/cpuinfo, here is the model to be CPU, we use grep
Combining regular interception with mode makes it easy to get the results you want.
Memory Size: There is also a file about memory that is/proc/meminfo next is the problem of interception.
Hard disk Size: Using the command fdisk-l, you can view the details and then use grep and cut to get the columns you want.
Here we need to know how to invoke a command can use the $ (command) or • command • Both of these methods.
To get the size of the memory, you can also use free-m, calculate the size of the memory, if you need to convert units, then you have to
Know how to perform the operations between variables, which are mentioned later.
Seemingly simple questions, let us learn:
1. Writing script specifications is important
2. Use variables to make scripts more conditioned
3. Skilled use of the regular can make us more effective
For:
#!/bin/bash
#------------------------
#Filename: systeminfo.sh
#Revision: 1.0
#Date: 2017-08-08
#Description: SystemInfo
#------------------------
ipv4= ' ifconfig eth0 |grep "\<inet\>" |tr-s ": |cut-d:-F 4 '
Sysmode= ' Cat/etc/redhat-release '
Kermode= ' Uname-r '
cpu= ' lscpu|grep ' Model name ' |cut-d:-F 2|tr-s '
ferr=$[$ (Cat/proc/meminfo |head-1|grep-o "[[:d igit:]]\+")/1024]
fdisk= ' Fdisk-l|head-n 2|tail-1 |cut-d,-F 1 |cut-d:-f2 '
echo "System basic Information"
echo "Host Name: $ (hostname)"
echo "IPV4 Address: $IPV 4"
echo "System version: $Sysmode"
echo "kernel version: $Kermode"
echo "CPU Model: $CPU"
echo "Memory Size: ${FERR}MB"
echo "Hard disk size: $Fdisk"
unset Ipv4 sysmode Kermode CPU ferr Fdisk
#########################################################
Finally, we'll talk about the calculation of variables, here are only three ways:
Method One: Let var= arithmetic expression eg. Let Count=2+4 Echo $count becomes 6, it is recommended to use this hair
Method Two: var=$[arithmetic expression] eg. COUNT=$[2*3] Echo $count turned 6.
Method Three: var= (arithmetic expression) is almost the same as the previous example.
2. Write script/root/bin/sumid.sh to calculate the sum of the IDs of the 10th and 20th users in the/etc/passwd file.
Analysis:
Explain:
Since it is the sum of the calculation uid, then we must first know how to derive the UID, the method is not difficult, using cut with a colon delimiter, you can get my
We want the UID, the rest of the matter is how to calculate the sums of two, in the previous question we also teach you methods, this time we will discuss in detail such as
How to calculate the sum of two variables, and we need to note that the topic is the 10th user and the 20th user uid of the sum, how to make the problem of the general
That is, when we enter numbers (representing the number of users), we can calculate the sum, which is what we need to consider using the location
Variables, of course, also involve the content of regular expressions. For example, adding our input is not 10 and 20, but an unknown, can only be used
Instead of $ and $, you have to sift through the UID and calculate the rows and the $, we can use Cat-n to display line numbers, and then use regular filtering,
Using Cut intercept, let's take a look at the implementation method.
For:
#!/bin/bash
#--------------------
#Filename: sumid.sh
#Revision: 2.0
#Description: UID sum
#--------------------
Read-p "Please input the other arg:" Number1 number2
root1=$ (cat/etc/passwd | head-n $number 1 |TAIL-1 | cut-d:-F3)
root2=$ (cat/etc/passwd | head-n $number 2 |TAIL-1 | cut-d:-F3)
Let Sumid=root1+root2
echo "$number1 user and $number2 user uid: $sumid"
unset number1 number2 root1 Root2
----------------------------------------------------------------------------------------------
3. Write the script/root/bin/argsnumsh, accept a file path as a parameter, or if the number of parameters is less than 1, prompt the user
"At least one parameter should be given" and exit immediately; If the number of arguments is not less than 1, the number of blank lines in the file pointed to by the first parameter is displayed.
Analysis:
Explain:
First we know that the position variable has a calculation of the number of parameters, that is, $#, as long as we take advantage of this variable can be judged.
In order to consider the overall point, we need to consider whether the file exists, the last difficulty is how to calculate the number of blanks.
^$ We know is blank line, do not forget to consider the case of space inside, so the final result is "^[[:space:]]*$".
For:
#!/bin/bash
#--------------------------
#Filename: Argsnumsh
#Revision: 3.0
#Date: 2017-08-10
#Description:d Etermine args
#--------------------------
[$#-lt 1]&&echo "should give at least one parameter" &&exit 1
[-F $]| | (echo "Please input exist file"; Exit 1) | | Exit 1
Space= ' grep ' ^[[:space:]]*$ ' $1|wc-l '
[$#-ge 1]&&echo "Blank behavior of the first parameter file: ${space} line" &&exit 0
Unset Space
4. Write a script/root/bin/createuser.sh, the execution syntax of the script must be: createuser.sh-u username-m password,
multiple spaces can be supported between options and parameters, but cannot be reversed in order. prompt with error output when the correct option or parameter is not specified
"createuser.sh-u username-m password" after exiting the script. The user name must start with a letter and can include numbers and _.
otherwise it is illegal. Prompt the user with the error output "user name contains only alphabetic data and underscore", when the user name detection is valid, determine the user name
whether it already exists, if it exists, then determine if the user has set a password, if set password, direct exit, not set, then set the password
after the password is specified Show "username password updated after exiting" in the correct output mode when the user name does not exist, the user is created,
and sets the password specified for the user "User username created and updated password" requires script execution when the correct output is displayed
No other output results that are not required in the process can occur. The script should be returned in a non-correct way to return the parameter to a value other than 0.
Analysis:
Explain:
This problem is relative to the above several questions are certain difficulty, but the careful analysis still has the thought to follow. The first is to determine the format that can be thought of using
Positional variables, $1,$2,$3. You can determine that there are $-u,$3 for-M and $4 cannot be determined, but you can determine the number of parameters, that is, $ #为4,
At this point, we can solve the problem by using simple judgment sentences. Next task to determine whether the user name is legitimate, that is, the judgment of the
The topic requires that the user name must start with a letter, can include numbers and _, you can use regular expressions, write requirements, it should not be difficult to
"^[[:alpha:]]\ ([[: alnum:]]\|_\) *" Here is a standard regular expression that uses this condition to match the user-supplied user name,
The statement in the title of the non-character. Next determine whether the user name exists, using the method of course is to use the ID command, if the return value is correct,
Then the next is to determine the existence of the password, using the method is to look at the/etc/shadow file, observe the situation of the password bit,
If the user does not exist, create a user, and set the password, this is relatively simple, the following answer.
For:
Method One:
#!/bin/bash
#-----------------------
#Filename: createuser.sh
#Revision: 7.0
#Date: 2017-08-11
#Description: Create User
#-----------------------
#判断用户名是否规范, and gives the return value
Decide= ' echo $2|grep ' ^[[:alpha:]]\ ([[: Alnum:]]\|_\) *$ "&>/dev/null;echo $? '
#判断用户是否存在, and gives the return value
Id= ' ID $ &>/dev/null;echo $? '
#截取用户密码位
Mima= ' Getent Shadow $ |cut-d:-F 2 '
#判断第一个参数
If ["$"! = "-U"];then
echo "Createuser.sh-u username-m password"
Exit 1
Fi
#判断第三个参数
If ["$ $"! = "-M"];then
echo "Createuser.sh-u username-m password
exit 1
Fi
#判断参数个数
If [$#-ne 4];then
&NB Sp echo "createuser.sh-u username-m password"
exit 1
Fi
#判断第二个参数
If [$deci De-ne 0];then
echo "user contains alphanumeric and underscore"
&NB Sp;exit 1
Else
If [$id-eq 0];then
if ["$mima" = = "!"-o "$mima" = = ""];then
echo "$4" |passwd--stdin $ &>/dev/null
&N Bsp echo "password updated"
exit 1
Else
&NB Sp exit 1
Fi
Else
' Useradd '
' Echo $4 |passwd--stdin &>/dev/null '
echo "User $ have created and updated password"
Fi
Fi
unset Decide ID Mima
Method Two:
[$ = "-U"-$ $ $ = "-M"-$#-eq 4] &>/dev/null | | (Echo createuser.sh-u username-m password 1>&2 && exit
1) && (echo$2|egrep "\<^[[:alpha:]" ([[:d igit:]]|_|[ [: Alpha:]]) *$\> "&>/dev/null | | (The Echo user name contains only the number of letters >
According to and underline 1>&2; exit 1) && (' id $ &>/dev/null ' | | (' Useradd $ '; Echo $4 |passwd--stdin $ &>/dev/null &&
echo User ${2} has created and updated the password; exit 1
) && (getent shadow $ |egrep ' \$ ' [[:d igit:] ' \$ ' &>/dev/null && (echo password saved >
in 1>&2; Exit 0
)|| (
Echo $4 |passwd--stdin $ &>/dev/null &&echo user ${2} password updated; exit 1
)|| Exit 1) | | Exit 1) | | Exit 1) | | Exit 1
Summary: Method One is more easy to understand a little, method two logical thinking is stronger, each has the advantage.
Learn shell programming in the case