15Shell Scripting-Process Control

Source: Internet
Author: User

Process Control Statements

Although you can write the most basic shell scripts by using Linux commands, pipe breaks, redirects, and conditional test statements, this script does not work for production environments. The reason is that it cannot adjust specific execution commands according to the actual job requirements, nor does it automatically loop execution according to certain conditions.

For example, we need to create a batch of 1000 for the user, first to determine whether these users already exist, or, if not, let the script create them one at a time by looping the statements.

There are 4 flow control statements that are commonly used if, for, while, and case.

    1. If condition test statement

1.1 Single Branch structure

if  条件测试操作    then    命令序列fi#shell脚本文件内容[[email protected] 0619]# cat mkcdrom.sh#!/bin/bashDIR="/media/cdrom"if [ ! -e $DIR ]thenmkdir -p $DIRfi#执行脚本[[email protected] 0619]# bash mkcdrom.sh#查看执行结果[[email protected] 0619]# ls  -l /mediatotal 0drwxr-xr-x. 2 root root 6 Jun 19 11:10 cdrom

1.2 Multi-branch structure

if  条件测试操作1    then    命令序列1elif    条件测试操作2    then    命令序列2else    命令序列3fi

In a Linux system, read is a command to read user input information, to assign the received user input information to the following specified variable, the-p parameter is used to display a certain hint message to the user.

[[email protected] 0619]# cat chkscore.sh#!/bin/bashread -p "Enter your score(0-100): " GRADEif [ $GRADE -ge 85 ] && [ $GRADE -le 100 ]; thenecho "$GRADE is Excellent"elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ]; thenecho "$GRADE is Pass"elseecho "$GRADE is Fail"fi[[email protected] 0619]# bash chkscore.shEnter your score(0-100): 9898 is Excellent
    1. For Condition Loop statement

      For variable name in value list
      Do
      Command sequence
      Done

The following uses a For loop statement to read multiple usernames from a list file and then create a user account and set a password for each one.

First create a list of user names, one row per user name.

[[email protected] 0619]# vim users.txt[[email protected] 0619]# cat users.txtboblily

Then write the shell script, where/dev/null is a file called the Linux black hole, redirecting the output information to this file is equivalent to deleting the data (similar to the garbage bin without recycling), can keep the user's screen concise.

[[email protected] 0619]# cat Example.sh#!/bin/bash#批量添加用户#输入用户密码(待创建的所有用户)read -p "Enter The Users Password: " PASSWD#对users.txt中的每一个用户名:检查用户名是否存在,如果存在,打印提示信息;如果不存在,则添加用户。添加成功或失败,都打印出错信息for UNAME in `cat users.txt`do# &> 表示将标准输出与错误输出共同写入到文件中id $UNAME &> /dev/nullif [ $? -eq 0 ]thenecho "Already exists"elseuseradd $UNAME &> /dev/nullecho "$PASSWD" | passwd --stdin $UNAME &> /dev/nullif [ $? -eq 0 ]thenecho "$UNAME, Create success"elseecho "$UNAME, Create failure"fifidone

Executes a shell script that creates a user in bulk. /ETC/PASSWD is a file that is used to store user account information. If you want to confirm that the script has successfully created a user account, you can open the file to see if there are any newly created user information.

[[email protected] 0619]# bash Example.shEnter The Users Password: 1bob, Create successlily, Create success[[email protected] 0619]# tail -5 /etc/passwdpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinhk:x:1000:1000:hk:/home/hk:/bin/bashbob:x:1001:1001::/home/bob:/bin/bashlily:x:1002:1002::/home/lily:/bin/bash
    1. While conditional loop statement

      While condition test action
      Do
      Command sequence
      Done

Write a shell script.

[[email protected] 0619]# cat Guess.sh#!/bin/bashPRICE=$(expr $RANDOM % 1000)TIMES=0echo "商品实际价格为0-999之间,猜猜看是多少?"while truedoread -p "请输入您猜测的价格数目:" INTlet TIMES++if [ $INT -eq $PRICE ]; thenecho "恭喜你答对了,实际价格是 $PRICE"echo "您共猜测了 $TIMES 次"exit 0elif [ $INT -gt $PRICE ]; thenecho "太高了!"elseecho "太低了!"fidone

Run the shell script

[[email protected] 0619]# bash Guess.sh商品实际价格为0-999之间,猜猜看是多少?请输入您猜测的价格数目:555太高了!请输入您猜测的价格数目:333太高了!请输入您猜测的价格数目:222太低了!请输入您猜测的价格数目:300太高了!请输入您猜测的价格数目:255太高了!请输入您猜测的价格数目:244太低了!请输入您猜测的价格数目:250太高了!请输入您猜测的价格数目:247太高了!请输入您猜测的价格数目:246恭喜你答对了,实际价格是 246您共猜测了 9 次
    1. Case Condition Test Statement (abbreviated)

15Shell Scripting-Process Control

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.