Loop of shell scripts: repeating commands

Source: Internet
Author: User
Tags iptables rsyslog

Loop of shell scripts: repeating commands

1.for Cycle

Grammar:

For variable name in variable Value list

Do

Command

Done

For based on the contents of the variable Value list, executes the command repeatedly until all the contents of the Variable Value column have been evaluated and finished.

Type of Value list: can be a specific text file, command generation list

Case:

VI User.txt

Zsan

Lisi

: Wq

VI useradd_for.sh

#!/bin/bash

un=$ (Cat/root/bin/user.txt)

For I in $Un

Do

Useradd $i

echo 123123 |passwd--stdin $i

Done

: Wq

chmod +x useradd_for.sh

./useradd_for.sh


Case: Checking for surviving hosts on the network

VI chk_net_alive_host.sh

#!/bin/bash

Read-p "Your want chk Net:" Net

For I in $ (SEQ 1 254)

Do

Ping-c 2 $NET. $i &&echo "$NET. $i are up."

Done

Case: Setting up the boot service

VI chk_ser_onboot_for.sh

#!/bin/bash

sl=$ (Cat/root/bin/ck.list)

For I in $SL

Do

/etc/init.d/$i status |grep PID

If [$?-ne 0];then

/etc/init.d/$i Restart

Fi

Chkconfig $i on

Done

VI ck.list

AUDITD # #审计服务

Blk-availability # #块设备管理服务

Crond # #计划任务

dhcpd

gpm

Iptables # #iptables防火墙

Lvm2-monitor # #lvm的管理服务

Named # #dns服务

Network # #网络

NTPD # #时间服务器

Postfix # #邮件服务

Rsyslog # #系统日志服务

SSHD # #远程连接

Udev-post # #udev硬件管理服务

XINETD # #超级服务, CVS,TFTPD and other small service Manager





For value list extension: Sequence list, array list

Sequence list:

VI for_list.sh

#!/bin/bash

Fl={vsftpd,dhcpd,named}

For I in $FL

Do

/etc/init.d/$i Restart

Chkconfig $i on

Done

For i in {1..254}

Do

Ping-c 2 192.168.10. $i &>/dev/null

Done

Array list:

VI for-arry.sh

#!/bin/bash

A= (crond gpm iptables lvm2-monitor network rsyslog sshd udev-post)

Echo ${a[1]}

Echo ${a[5]}

echo A arry length is ${#A [*]}

For i in ${a[*]};d o

Chkconfig--list $i

Done

[Email protected] bin]#


This article is from the "12289734" blog, please be sure to keep this source http://12299734.blog.51cto.com/12289734/1908242

Loop of shell scripts: repeating commands

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.