Shell Learning Path: Process Control (for)

Source: Internet
Author: User

Syntax for the FOR loop:

1. For variable in value 1 value 2 value 3 ....

Do

Program

Done

For example: The following script will print 4 times respectively the value of morning noon afternoon evening

1 #!/bin/bash2#打印时间 3#Author mrfeng45   for-time in morning noon afternoon evening6do         7                  Echo $time8          Done

For example, batch decompression:

1#!/bin/Bash2 #批量解压缩脚本3 #Author: Mrf4 5path=/opt/Lamp6 7 CD $path8 9 ls*.Tar. gz >ls. LogTen ls*.tgz >>ls. Log One  forIinch$(Cat ls. Log) A          Do -                 TarZXVF $i &>>cc. Log -          Done the RM-rfls. Log

2.for Syntax Two

Format:

1  for (initial value; cyclic control condition; variable change)) 2      Do 3            procedure  4      Done

For example:

1#!/bin/Bash2 #从1加到1003 4s=05  for((i=1; i<= -; i=i+1))6      Do7s=$ (($s +$i))8      Done9 Echo "The sum of 1+2+...+100is: $s"

Example: adding Users in bulk

1#!/bin/Bash2 # Users who add a number of users in bulk3# AUTHOR:MR F (e-mail:22222222)4 5Read-p"Please input user name:"-T -name6Read-p"Please input the number of users:"-T -Num7Read-p"Please input the password of users:"-T -Pass8 if[!-Z"$name"-A! -Z"$num"-A! -Z"$pass" ]9          ThenTeny=$ (Echo$num |sed 's/[0-9]//g') One                 if[-Z"$y"] ; Then A                          for((i=1; i<= $num; i=i+1)) -                                  Do -/usr/sbin/useradd $name $i &>/dev/NULL the                                         Echo$pass | /usr/bin/passwd--stdin $n -Ame$i &>/dev/NULL -                                  Done -                 fi + fi - Cat/etc/passwd|grepuser[1-$num] |Cut-D":"-F1>>userlist.log

Of course, I also wrote a script that uses Userlist.log for bulk deletion to make it easier for you to clean up after the experiment.

1 #!/bin/bash2 for in $ (cat ./Userlist.log)3 do         4                 /usr/sbin/userdel-r $i &>>del.log5          Done 6 #cat  del.log7rm -RF Del.log

Shell Learning Path: Process Control (for)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.