Shell script (iv) Case Process Control and Exit command

Source: Internet
Author: User
Tags bz2 case statement gz file

Exit Command

Exit The procedure used to control the program and the expression, there is an exit function in the General programming language, also exists in the Linuxshell.

Exit 0

Exit status

Status A number between 0-255 indicates that the return status value is accompanied by the exit of the script (in fact, exit).

Is that $? The value can be queried using echo

Exit The command is not the same on the script and on the terminal.

Experiment 1-Determine if the value entered from the keyboard is empty


1, Enter a sub-shell and edit a file. (Prevent execution exit when exiting the terminal, so execute a child shell first)

[Email protected] ~]# bash
[Email protected] ~]# vim exit.sh

#!/bin/bash
echo "Please enter a string:"
? Read str1
If [-Z $str 1]
Then
???? echo "What are you to enter is null"
?? Exit 1
Fi

Validation scripts

Let's first enter a value to see that my exit status is 0 (the script's variable value input is not empty) and will not execute the statement in then

Enter an empty string then execute the command followed by then and exit status 1

Continue validation:

Change the value in the exit.sh script to 255

You can set the exit status value yourself while writing a script, but the specific values are specific and do not mess with them.


Case Statement Structure

The case statement is a multi-select statement. A case statement can be used to match a value with a pattern, and if the match succeeds, the matching command is executed.

The case statement is primarily applicable to the existence of multiple values for a variable and the need to execute a different sequence of commands for each of these values

Case syntax Structure of branch statements

Case variable name in

???????? Mode 1)

???????????????????? Command Sequence

????????? Mode II)

???????????????????? Command sequence

;;

????????????? ......

???????????????? * )

?????????????????? Default command sequence

Exit

Esac

Precautions:

Must be a word after the value is taken?? In

Each pattern must end with a closing parenthesis.

The value can be either a variable or a constant.

This ESAC is the end of the case, like if...fi,
attention commands?;; In the ";;" cannot be lost.

After the match discovery value conforms to a pattern, all commands begin to execute until;;.

The value will detect each pattern that matches. Once the pattern matches, the other mode is no longer resumed after the corresponding command is executed.

If there is no matching pattern, use an asterisk * to capture the value and then execute the subsequent command.



Experiment

Write a script called untar.sh, which extracts the. tar.gz and. TAR.BZ2 Formatted archive files

Vim untar.sh

Case $ in
????? *.tar.gz)
????????????? Tar zxf $
;;
????? *.TAR.BZ2)
????????????? Tar jxf $
;;
????? *)
???????? echo "Error"
;;
Esac

Test:

Create a test. tar.gz file











Shell script (iv) Case Process Control and Exit command

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.