Shell programming note 2 Control Flow Structure

Source: Internet
Author: User
2. Control Flow Structure
Exit status
While, for, and until loops Loops
If then else statement
Actions in the script
Menu

1. Exit
An exit status is returned when any command is running. To observe the exit status, run the following command:
$ Echo $?
To exit the current process, shell provides the exit command in the following format:
Exit n, where n is a number

2. Control Structure
2.1 Flow Control
If, then, and else statements provide conditional tests. However, based on various conditions
Case statements allow matching modes, words, or values

2.2 cycles
For Loop
Until Loop
While Loop

3 if then else statement
If condition
Then command
Fi
When using the if statement, you must place the then part in the new line; otherwise, an error occurs. If you want to remove a branch, you must use the command delimiter:
If condition; then
Command
Fi

Echo-n "Enter you name :"
Read name
If [$ name = ""]; then
Echo "You did not enter any information"
Fi

Grep output check
$ PG grepif
#! /Bin/sh
# Grepif
If grep 'Dave \> 'data. File>/dev/null 2> & 1
Then
Echo "Great Dave is in the file"
Else
Echo "no Dave is not in the file"
Fi

Parameters passed to the script for test
$ # Indicates the number of parameters

Determines whether the script is in interactive mode.
Sometimes you need to know whether script execution is in interactive mode (Terminal Mode) or non-interactive mode (cron or)

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.