Shell Script Basics Command Learning (i)

Source: Internet
Author: User

The role of the shell is to parse the user's instructions and combine the shell instructions with a specific process to form a script.

To view the current system Shell version:

Once you have written a simple shell script, there are several ways to do it:

1, use chmod to add X permissions to the script, such as: chmod +x test.sh

2,/bin/sh or/bin/bash Execute script (child process is created)

3,source./test.sh Script Execution

4, parentheses, creates child processes

Environment variables can be set directly, for example var=100, using unset var can be removed

Wildcard characters:

* Match one or more arbitrary characters

? Match an arbitrary character

[several characters] match any one of the characters in parentheses

Command substitution:

Parentheses:

Arithmetic conversions:

Binary conversions:

Escape characters and special characters:

Conditional branches:

$? Represents the return value 0 is true and 1 is false

Test or [] can be used as an expression to judge

GT (greater than), LT (less than), eq (equals), NE (Not equal to), Le (less than equals), GE (greater than or equal)

Parentheses to determine,-f is a normal file,-D is a folder,-Z to determine whether the string is empty

Logical judgment:

Logical Non!

Logic and-A

Logical OR-O

such as: [-F a.txt-a-D a.txt]

Note the spaces on both sides of the parentheses if there are no errors

If branch statement:

echo "Is that your boy?" Read Yyif ["$yy" = "yes"];then    echo "You is a boy" elif ["$yy" = "no"]    echo "is a girl" else    echo "idk "Fi

Case Branch Statement:

Echo "is a boy?" Read Yycase "$yy" in    y|yes| YES)        echo "You is a boy";;    [nn]*]        echo "You is a Girl";;    *)        echo "idk"        exit 1;; Esac

For Loop statement:

For i in X y zdo    echo ' $i ' done

While Loop statement:

echo "Please input your passwd" read yywhile ["$yy"! = "123"];d o    echo "Sorry,try again"    read Yydoneecho "login su Ccess "

Calculate 1+2+3+ .... 100 Summation:

Sum=0i=1while [$i-lt 101];d o    sum=$[sum+i]    i=$[i+1]doneecho $sum

Positional parameters and special variables:

$             A is equivalent to the C language main function of argv[0]$1,    ... These are called positional parameters (positional Parameter), equivalent to the C language main function of the argv[1], argv[2]...$#             equivalent to the C language main function argc-1, note that there is no comment behind the # [email Protected]             represents the parameter list "$" ..., for example, can be used in the for loop in the back. $*             represents the parameter list "$" "$" ..., the same as $?             Exit status$$ The             current process number of the previous command

Use of functions:

Foo () {    echo "$-$"}echo "Start call foo" foo one 22echo "End call"

Remote File Transfer supplement:

SCP 1.txt [Email Protected]:/home/ubuntu

  

Shell Script Basics Command Learning (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.