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.
(1)
Character test:
= =: Test for equality, equality is true, not equal to False
! =: Test Whether it is unequal, not equal to true, etc as false
>
<
-N String: Tests whether the specified string is empty, empty is true, or false
-Z String: Tests whether the specified string is not empty, is not empty, and the null is False
Exercise: Write a script
Pass a parameter (single-character on the line) to the script, such as the parameter is Q, exit the script; otherwise, the user's parameters will be displayed;
Exercise: Write a script
Pass a parameter (single-character on line) to the script, such as Q, Q, quit, or quit, to exit the script, otherwise, the user's parameters are displayed;
#!/bin/bash#if [$ = ' Q '];thenecho "quiting ..." exit 1elif [$ = ' Q '];thenecho "Quiting ..." Exit 2 elif [$ = ' quit ']; Thenecho "Quiting ..." Exit 3 elif [$ = ' Quit '];thenecho "quiting ..." Exit 4 Elseecho $1fi Example: 1, echo "SCALE=2;111/22;" | BC//Reserve 2 decimal places, calculate the value of 111/22 by Calculator BC (Method 1). 2, BC <<< "SCALE=2;111/22;"//Reserve 2 decimal places, calculate the value of 111/22 by Calculator BC (Method 2).
Practice:
Pass three parameters to the script, the first is an integer, the second is an arithmetic operator, the third is an integer, the result of the calculation is displayed, and the two-bit precision is required to be preserved. Shaped like:
./calc.sh 5/2
Practice:
Pass 3 parameters to the script, the parameters are the user name. The user's account information is extracted and placed in the/tmp/testusers.txt file, and the beginning of each line is required to have a line number.
Write a script:
Determine the current host CPU manufacturer whose information is in the Vendor ID row in the/proc/cpuinfo file.
If its manufacturer is AUTHENTICAMD, it will be shown as AMD;
If its manufacturer is Genuineintel, it will be displayed as Intel Corporation;
Otherwise, it is said that it is a non-mainstream company;
(2)
Loop: Enter condition, exit condition
For
While
Until
(3)
for variable in list; Do
Loop body
Done
Cases:
For I in 1 2 3 4 5 6 7 8 9 10; Do
Addition operation
Done
After the traversal is completed, exit;
(4) How to generate a list:
{1..100}//method for generating an integer list 1
' SEQ [start number [step length]] End Number '//method of generating an integer list 2
(5)
Bashshell does not support floating-point arithmetic, and the default data type for bash shell is character.
(6)
Bash Shell declares the method of shaping:
Declare-i sum=0//-I is integer here
Or:
Let sum=0
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 shell of each other in turn, such as:
Hello, Root, your shell:/bin/bash
3, statistics a total number of users
For I in ' seq 1 $LINES '; Do echo "Hello, ' head-n $I/etc/passwd | Tail-1 | cut-d:-f1 ' "; Done
Just ask the default shell for bash users.
Write a script:
1, add 10 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
Write a script:
Calculates the number of positive integers within 100 that can be divisible by 3;
Modulus, residual:%
3%2=1
100%55=45
Write a script:
Calculates the sum of all odd numbers and all the even numbers within 100, and displays them respectively;
Let i=$[$I +1]sum=$[$SUM + $I]let sum+= $Ilet i+=1 equivalent to let I++-=let i-=1 equivalent to let I--++i,--i*=/=%=
Write a script that shows all the default shell-bash users and default shell-/sbin/nologin users on the current system, and counts the total number of users under each shell. The results appear as follows:
Bash,3users,they is:
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
Linux Foundation 11-bash Programming (string test and for loop)