Shell Learning Notes (i)

Source: Internet
Author: User

1, the first line must be #! /bin/bash

#! indicates that the script uses the following interpreter to interpret the execution

2. Echo Print output

For example echo "Hello World" > Aa.txt

  

3. Receiving parameters

#!/bin/bash

Name=$1

Age=$2

Sex=$3

echo "Name: $name; age: $age; Sex: $sex"

Operating effect:

4, accept the user input parameters

#! /bin/bash

Read-p "Please input your name:" Name

echo "My name is $name"

READ: Waiting for user input, similar to scanner inside Java

-P: Prompt information

5, Judge If/then/elif/else/fi

Elif equivalent to else if

If must end with FI, otherwise an error will be made

For example: Judging by the age of a person he is a teenager, middle age or old age (here is the assumption that 1~20 is a teenager 21~60 is aged 600 or older milk year)

#! /bin/bash
Read-p "Please input your Age:" Age
If [$age-lt 0]; Then
echo "Age is incorrect"
Else
If [$age-lt 20]; Then
echo "Younger"
elif [$age-lt 60]; Then
echo "Adult"
elif [$age-lt 120]; Then
echo "Older"
Else
echo "Age is incorrect"
Fi
Fi

Schedule

1. String judgment

STR1 = str2 When two strings have the same content, length is true
Str1! = str2 True when string str1 and str2 are not equal
-N str1 True when the length of the string is greater than 0 (string non-null)
-Z str1 True when the string length is 0 (empty string)
STR1 is True when string str1 is not empty

2, the number of the judgment

Int1-eq int2 Two numbers equal to True
Int1-ne Int2 Two number is true
INT1-GT Int2 int1 greater than Int2 is true
Int1-ge Int2 int1 greater than or equal to Int2 true
Int1-lt Int2 int1 Less than Int2 is true
Int1-le Int2 int1 less than or equal to Int2 true

3, the judgment of the document

-r file user readable as True
-W file user can write as true
-X file user can execute as true
-F file is true for regular files
-d file files are directory-True
-C File file is true for character special files
-B file files are true for block special files
-S file files non-0 o'clock True
-T file is true when the specified device is terminal (default = 1)

If the-e file file exists test-e filename
-S file is present and is a socket file
Whether the-p file exists and is a FIFO (pipe) file


The-u file file exists and has the Suid property
The-G file is present and has the Sgid property
The-K file is present and has the Sticky bit property
-s Fie file exists and is not a blank file

4. Complex logic judgment

-A and
-O or
! Non -

Shell Learning Notes (i)

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.