Linux under Bash programming character test with For Loop statement (v)
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
2.for loop: Enter condition, exit condition
for variable in list; Do
Loop body
Done
Build list:
{1..100}: Represents a number from 1 to 100 with a step of 1
SEQ [Start number [step length]] End number for example: SEQ 1 100 default step is 1
DECLARE option variable name = value
Options:
-I declaration As Integer
-X declared as environment variable
3. Experiment: Add 10 users user1 to User10, password with user name; script accepts one parameter:
If it is add:, the user will be added; If it is Del, the user will be deleted, otherwise the parameter error is indicated;
Note that the user exists on the prompt cannot be added, the user does not exist can not delete the user, using the following script implementation:
if [= = ' Add ']; Then
For I in ' seq 1 10 '; Do
if! ID user$i &>/dev/null; Then
Useradd user$i
echo "User$i" | passwd--stdin user$i &>/dev/null
Else
echo "user$i exist"
Fi
Done
elif [= = ' del ']; Then
For I in {1..10}; Do
If ID user$i &>/dev/null; Then
Userdel-r user$i
Else
echo "User$i not Exist"
Fi
Done
Else
echo "Agur is not Add/del!!!"
Fi
4. Experiment: Greet the Bash users in/etc/passwd and count the number of bash users in turn, using the following script:
#!/bin/bash
file=/etc/passwd
Lines= ' Cat $FILE | Wc-l '
Declare-i a=0
For I in ' seq 1 $LINES '; Do
Bsh= ' Head-n $I $FILE | Tail-1 | cut-d:-f7 '
if [[$BSH = = '/bin/bash ']]; Then
echo "Hello, ' head-n $I $FILE | Tail-1 | cut-d:-f1 ', Your Shell is: $BSH "
a=$[$A +1]
Fi
Done
echo "There are $A users"
5. Calculate all the odd and all-even numbers within 100 and display, using the following script:
#!/bin/bash
Os=0 represents even numbers
Js=0 represents an odd number
For I in ' SEQ 1 100 '; Do
If [$[$I%2] = = 0]; Then
os=$[$OS + $I]
Else
js=$[$JS + $I]
Fi
Done
echo "JS SUM is $JS"
echo "OS SUM is $OS"
echo "Js+os is $[$JS + $OS]"
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/0D/wKiom1cQ2ZmSeLfzAACbc2uhRlg824.jpg "title=" Os1.jpg "alt=" Wkiom1cq2zmselfzaacbc2uhrlg824.jpg "/>
This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1764290
Linux under Bash programming character test with For Loop statement (v)