Shell script Programming If statement learning Notes _linux Shell

Source: Internet
Author: User
Tags echo command

I am not studying things under Linux, so here is only a description of the use of shell script, I am in the process of using the problem recorded, but also write down some of the rules of the shell script syntax, easy to view later. Let's take a look at the usage of the conditional statement if, and use this simple syntax to illustrate what the shell script does when programming.

Copy Code code as follows:

#!/bin/sh
echo "Please input Text1"
Read Text1
echo "Please input Text2"
Read Text2
#判断字符串等或者是不等只有一个等号
if test $text 1 = $text 2
Then
echo "Text1 equals Text2"
Else
echo "Text1 not equals Text2"
Fi

#判断字符串是否为空, here's the judgment remember to add double quotes to the $TEXT1.
If [-Z "$text 1"]
Then
echo "Text1 is null"
Fi
If [-N "$text 1"];then
echo "Text1 is not NULL"
Fi

#算术比较 the contents of Text1 and Text2 can only be numbers.
If ["$text 1"-eq "$text 2"];then
echo "Equal"
elif ["$text 1"-gt "$text 2"];then
echo "Great"
elif ["$text 1"-le "$text 2"];then
echo "Little and equals"
Fi

echo "Input a file or not file"
Read file
#判断是文件还是目录
If [-D $file];then
echo "$file is a directory"
elif [f $file];then
echo "$file is a file"
Fi
#判断文件的大小是否为空
If [-S $file];then
#echo-N is to remove line breaks
Echo-n ' $file ' size is not NULL '
Fi

#判断文件的读写权限
If [f "$file"];then
If [-R "$file"];then
echo "read"
Fi
If [-W "$file"];then
echo "Write"
Fi
If [-X "$file"];then
echo "EXE"
Fi
Fi

Exit 0

Let's analyze this program a little bit, the first line is a special annotation statement, in the Linux shell, using # as a comment, so if you want to annotate things with the # start, the first line of the comment statement is to tell the shell to execute this shell script when the use of the/bin/ SH This directory under the shell. Then use the echo command to output a line of statements, the string behind the echo is best to enclose in double quotes, after all, usually the best strings are enclosed in double quotes, which can avoid some difficult to find the bug,echo behind how to add-N option, then the statement output is not wrapped. Then the read command, which is used to read input into the variable Text1, in the shell script, we do not need to use the definition of the predefined, in the use of the direct use of this can be. Next, we'll talk about the IF conditional statement that we're going to explain in this blog, which is the IF syntax. Then.. Elif.. Then.. else.. Fi specific look at the code, the code has been reflected, in the C language we use if to judge the sentence in parentheses, where the judgment is judged by the test command, you interpret it as in parentheses, and the test can be judged by the string, and The judgment of the numerical value, in the case of a file directory, this test can be replaced with a [symbol, which is the reason for not using Test at the end of the program, but for the [decision to end with], and the judgment statement is written in brackets because [is the test, so [ And the judgment conditions behind it have spaces. Next come back to the test can be judged by the type, the code shows the judge of the string, note that we judge Text1 and Text2 equal when using an equal sign, everyone C language is accustomed to do not forget is an equal sign, the value of the variable is in the front of the variable plus $ symbol, This gets the value of the variable. Also need to add double quotes, you may not find the wrong, but if the input is empty, that is, directly hit the return, this time the judge became if test = text2,test behind nothing of course wrong, if the double quotes is such if test "" = Text2, So there's no mistake. Other things about the options, such as-Z,-N,-gt,-F,-R, and so on, each of these is shorthand for English words, very simple! Next, run the shell program, use the chmod +x command to give executable permissions, and then run, everything ok!

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.