(Linux shell) Chapter One-small trial sledgehammer (next)

Source: Internet
Author: User
Tags array length logical operators

Article Source: (Linux Shell) The first chapter--Small trial sledgehammer (next)


1.6 Arrays and associative arrays

1.6.1 Preparation Knowledge

Bash supports both normal and associative arrays, and normal arrays can use integers only as an array index, whereas associative arrays use strings as an array index. Associative arrays are quite useful in many operations.

1.6.2 Practical Walkthrough

There are many ways to define an array, and you can use a column of values in a single line to define an array:

Array_var= (1,2,3,4,5,6) #这些值将会存储在以0为起始索引的连续位置上

In addition, you can define an array as a set of index-values:

array_var[0]= "Test1"

array_var[1]= "Test2"


Print out the contents of a specific array element:

Echo ${array_var[0]}

Print all the values in the array in the form of a list:

Echo ${array_var[*]}

Print array length

echo ${#array_var [*]}


1.7 Using aliases

1.7.1 Preparation Knowledge

Aliases are available in a number of convenient ways, you can use functions, or you can use the alias command

1.7.2 Practical Walkthrough

Alias new_command= ' command sequence '

Alias install= ' sudo apt-get install '

Of course, this modified alias is only temporary, valid only for the current terminal, if you want to keep the function, you can put it into the ~/.BASHRC


1.8 Getting terminal information

1.8.1 Prep Just

Tput and Stty are two terminal processing tools.

1.8.2 Practical Walkthrough

Get the number of rows and columns of a terminal

Tput cols

TPU Lines


Print out the current terminal name:

Tput longname

Move the cursor to the azimuth (100,100)

Tput Cup 100 100

Set the terminal background color

Tput Setb No (0--7)

Set the text style to bold:

Tput Bold

Delete all the contents of the current cursor to the end of the line:

Tput Ed

Use Stty-echo to hide user-entered content


1.9 Get Set date and time delay

1.9.1 Practical Walkthrough

Read Date:

Date

When you print an era

Date +%s

The option--date is used to provide a date string as input. But we can use any of the date formatting options to print the output. The date string as input can be used to know the day of the week given the date

Date--date ' Jan ' +%a

You can print out the date format as you choose:

Date "+%d%B%Y"


Time delay can be used by the Sleep sec command


1.10 Functions and parameters

1.10.1 Practical Walkthrough

To define a function:

function fname () {

statements;

}

or fname () {statments}

You just need to use a function name to call the function:

FName

Call Function Pass parameter:

fname arg1 arg2

The following is the syntax for a function read parameter:

FName () {

Echo $1,$2 #访问参数1和参数2

echo "[email protected]" #以列表的方式一次打印所有的参数, i.e. "$" "$" "$"

echo "$*" #类似于 [email protected], but the parameter is used as a single entity, "$1c$2c$3", and C is a character of IFS

}


1.11 Comparison and testing

? 1.11.1 Preparatory Knowledge

? ? We can use if,if else and logical operators to perform tests, and compare data items with some comparison operators. Another test command can also be used for testing.

? 1.11.2 Practical Walkthrough

? ? if condition; Then

?    ? ? commands;

? ? fi

? ? Arithmetic comparison

? The condition is usually placed inside the brackets, be sure to note that there is a space between [or] and the operand. If you forget this space, the script will get an error. For example:

? ? [$var-eq 0] or [$var-eq 1]

? ? Some other important operators are:

?    ? ?-GT: Greater Than

?    ? ?-LT: Less than

?    ? ?-ge: Greater than or equal to

?    ? ?-le: Less than or equal to

? ? You can test with multiple conditions in the following ways

? ? [$var-ne 0-a $var 2-gt 2] #逻辑与-A

? ? [$var-ne 0-o $var 2-lt 2] #逻辑或-O

? ?

? ? file system Related tests:

? ? [-F $file _var] #如果给定的变量包含正常的文件路径和文件名 returns True

? ? [-X $var] #如果给定的变量包含的文件可执行 returns the true

? ? [-D $var] #如果给定的变量包含的是目录 returns the true

? ? [-e $var] #如果给定的变量包含的文件存在 returns True

? ? [-W $var] #如果给定的变量包含的内容可写 returns True

? ? [-D $var] #如果给定的变量包含的内容可读 returns the true

? ? [-L $var] #如果给定的变量包含的是一个符号链接 returns True

? ?


? ? string comparison:

?    ? ? You can check that two strings are equal and not equal:

?    ? ?    [[$str 1 = $str 2]] [[$str 1! = $STR 2]]

?    ? ? Compare the size between two strings:

?    ? ? [[$str 1 > $str 2]] or [[$str 1 < $STR 2]]

?    ? ? [[-Z $STR 1]] If str1 is an empty string, returns the True

?    ? ? [[-N $str 1]] Returns True if STR1 is a non-empty string?

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.