Advanced Bash-shell Guide (Version 10) study notes two

Source: Internet
Author: User
Tags arithmetic

Variable substitution
$variable is a shorthand for ${variable}
hello= "A B C D"
Echo $hello # A B C D
echo "$hello" # A B C D
The quotation mark preserves the whitespace inside the variable

1 echo "$uninitialized" # (blank line)
2 Let "uninitialized + = 5" # ADD 5 to it.
3 echo "$uninitialized"
Uninitialized variable is null, but equals 0 in an arithmetic expression
Command substitution
A= ' ls-l ' # assigns result of ' ls-l ' command to ' a '
Echo $a
Or
a=$ (LS-L)
Echo $a

string + Integer equals integer, string equivalent to 0
But when divided by a null variable, the system error.

Variable type
Local variables
Visible in code block
Environment variables
Variables that affect the shell and user interface
Position parameters
Take the last parameter
1 args=$# # Number of args passed.
2 Lastarg=${!args}
Or
lastarg=${!#}
[...] and [[...]]
use [[...]] can block logic errors that are common in scripts compared to []
For example: &&, | |, <, and > Operations

When you do arithmetic, [[...]] Octal and hexadecimal are automatically calculated and []
You can not
5 decimal=15
6 octal=017 # = (decimal)
7 hex=0x0f # = (decimal)
8
9 If ["$decimal"-eq "$octal"]
Ten Then
echo "$decimal equals $octal"
Else
echo "$decimal isn't equal to $octal" # Equal to 017
+ fi # doesn ' t evaluate within [single brackets]!


if [["$decimal"-eq "$octal"]]
Then
echo "$decimal equals $octal" # equals 017
Else
echo "$decimal is not equal to $octal"
FI # evaluates within [[double brackets]]!

((...)) Arithmetic test
If the expression evaluates to 0, its exit status code is 1 or False
If the expression evaluates to non-0, its exit status code is 0 or True
Its exit status and [...] Instead


Echo ${aa##*/} Gets the file name of the variable AA with the/bit delimiter
${filename##*.} ! = "GZ"
filename##*. Gets the filename extension


Infinite Monkeys
AppMakr
String manipulation

Get string length
${#string}
Length of expr $string
Expr "$string": '. * '

Gets the length of a string that matches from the beginning of the string
Expr match "$string" ' $substring '
Expr "$string": ' $substring '
$substring is a regular expression

Gets the position of the substring starting in the string
Expr index $string $substring

Remove substring
${string:position}
${string:position:length}

This article is from the "Linux is belong to You" blog, make sure to keep this source http://jwh5566.blog.51cto.com/7394620/1657454

Advanced Bash-shell Guide (Version 10) study notes two

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.