Practice:
Pass a user name parameter to the script to determine if the user's user name is consistent with the group name of their base group, and the results are displayed.
Write a script:
#!/bin/bash
User=$1
if [! $#-eq 1];then
echo "Please input only one username"
Exit 2
Fi
if! ID $ &>/dev/null;then
echo "The user is not exist"
Exit 3
Fi
If [' Id-u $ '-eq ' id-g $ '];then
echo "Same"
Else
echo "Different"
Fi
determine the current host's The CPU manufacturer whose information is in the vendor ID row in the /proc/cpuinfo file .
if its manufacturer is AUTHENTICAMD, the company isshown as AMD ;
if its manufacturer is Genuineintel, it is shown as Intel Corporation;
Otherwise, it is said that it is a non-mainstream company;
#!/bin/bash
Cpufac= ' sed-n '/^vendor_id/p '/proc/cpuinfo | Cut-d '-f2 '
if [$CPUFAC = = Authenticamd];then
echo "AMD"
Elif
[$CPUFAC = = Genuineintel];then
echo "Inter"
Else
echo "Other company"
Fi
Write a script:
Pass three integers to the script to determine the maximum and minimum numbers and display them.
#!/bin/bash
DECLARE maxnum
DECLARE minnum
If [$#-ne 3];then
echo "Please input three number"
Exit 2
Fi
If [$1-ge $];then
Maxnum=$1
Minnum=$2
Else
Maxnum=$2
Minnum=$1
Fi
If [$MAXNUM-ge $];then
echo "The Maxnum is $MAXNUM"
[$1-ge] && echo "is first number" | | echo "is second number"
Else
Maxnum=$3
echo "The Maxnum is $MAXNUM is third number"
Fi
If [$MINNUM-le $];then
[$1-le]&& echo "The Minnum is $" | | echo "The Minnum is $"
[$1-le]&& echo "The Minnum is First" | | echo "The Minnum is second"
Else
echo "The Minnum is $ third number"
Fi
Test string
1 [$A = = $B] at this time, = = before and after the white space is to be recognized or the default will be identified as an assignment,
2 [$A! = $B]
3 a single-mesh operator that tests whether a string is empty
-n string null is true, NOT NULL for true NULL when false and test [ space variable space ] such format to test
4 test string is not empty
-S not empty for true, empty for false
4 test string is greater than > < to indicate
Use for Loops
For variable in list;
command line 1;
command line 2;
Done
1.1 Build List
1.1{1..100}
1.2 Seq Start number step end number
Seq number means starting from 1 to number
Seq number1 number2 Indicates a step size of 1 with a number1 ending Count of number2
Seq number1 number2 Number3 1 Indicates the start number 2 indicates the step 3 represents the end number
1.3 ' ls/etc/'
declaration of data type declare
Declare-i sum=0 declaration as an integer!
Declare-x declared as environment variable
Write a script:
1, the value of the set Variable FILE is /etc/passwd
2. say hello to each user in/etc/passwd and show the shellof each other inturn ,such as:
Hello, Root, your shell:/bin/bash
3. count the number of users
4. just ask the default shell for bash users .
# CAT/ETC/PASSWD | Cut-d:-f1,7 | head-1 | Tail-1 | Cut-d:-f2 | Sed ' [Email protected]/.*/@@ '
# CAT/ETC/PASSWD | Cut-d:-f1,7 | head-1 | Tail-1 | Cut-d:-f1
#!/bin/bash
DECLARE I
DECLARE j=0
Num= ' cat/etc/passwd | Wc-l '
For I in ' seq $NUM ';d o
User= ' cat/etc/passwd | Cut-d:-f1,7 | head-$I | Tail-1 | cut-d:-f1 '
Ushell= ' cat/etc/passwd | Cut-d:-f1,7 | head-$I | Tail-1 | Cut-d:-f2 | Sed ' [Email protected]/.*/@@ '
# echo "HI $USER your shell is $USHELL"
if [$USHELL = = Bash];then
echo "Hi $USER your shell is $USHELL"
Let j= $J +1
Fi
Done
echo "The users total was $I the bash is $J"
Exit
Write a script:
1, add ten users user1 to user10, password with the user name, but requires only the user does not exist in the case can be added;
Extended:
Accept a parameter:
Add: Adding user user1. User10
Del: Delete user user1. User10
Other: Exit
Adminusers User1,user2,user3,hello,hi
!/bin/bash
if [= = = "Add"];then
For I in {1..10};d o
If ID user$i &>/dev/null;then
echo "user$i is exist"
Else
Useradd user$i
echo "User$1" | passwd--stdin user$i &>/dev/null
Fi
Done
elif [= = = "Del"];then
For I in {1..10};d o
Userdel-r user$i
Done
Else
echo "Please input Add or del"
Exit 2;
Fi
write a script that shows all default shell- bash users on the current system and the default Shell as /sbin/ Nologin users, and Count The total number of users under each shell. The results appear as follows:
BASH,3users, they are:
Root,redhat,gentoo
Nologin, 2users, they is:
Bin,ftp
#!/bin/bash
#
Numbash= ' grep ' bash$ '/etc/passwd | Wc-l '
bashusers= ' grep ' bash$ '/etc/passwd | cut-d:-f1 '
Bashusers= ' echo $BASHUSERS | Sed ' [Email protected][[:space:]]@,@g '
echo "BASH, $NUMBASH users, they is:"
echo "$BASHUSERS
Combination test conditions
-A: with relation
-O: or relationship
! : Non-relational
If [$#-gt 1-a $#-le 3]
If [$#-gt 1] && [$#-le 3]
This article is from the "Linux Learning drip" blog, please be sure to keep this source http://2858259639.blog.51cto.com/8111214/1873324
Linux Learning Basics 7 Script Exercises