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

Source: Internet
Author: User
Tags aliases 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 common arrays and associative arrays at the same time. An ordinary array can use integers only as an array index, and associative arrays can use strings as an array index. Associative arrays are quite useful in a very large number of 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为起始索引的连续位置上

Other than that. It is also possible to 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. Ability to use functions, or use the alias command

1.7.2 Practical Walkthrough

Alias new_command= ' command sequence '

Alias install= ' sudo apt-get install '

Of course. The alias for this change is only temporary and is only valid for the current terminal. Assuming you want to keep working, you can put it in ~/.BASHRC


1.8 Getting terminal information

1.8.1 Preparation is 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 the entire 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 the random date formatting option to print out the output. Using a date string as input can be used to know the day of the week for a given date.

Date--date ' Jan ' +%a

Can print out the date format according to your choice:

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 can call a function simply by using a function name:

FName

To call a function to pass a number of parameters:

fname arg1 arg2

Here is the syntax for the function to read the parameters:

FName () {

Echo $1,$2 #訪问參数1和參数2

echo "[email protected]" #以列表的方式一次打印全部的參数, i.e. "$" "$" "$"

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

}


1.11 Compare and test

? 1.11.1 Preparatory Knowledge

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

? 1.11.2 Practical Walkthrough

? ? if condition; Then

?    ? ? commands;

? ? fi

? The math is better.

? The condition is usually placed inside the brackets, be sure to note that there is a space between [or] and the operand.

Suppose you forget this space. The script will get an error. Like what:

? ? [$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

? Can be tested with multiple conditions according to the following method

? ? [$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] #假设给定的变量包括正常的文件路径和文件名称. The return True

? ? [-X $var] #假设给定的变量包括的文件可运行 returns the true

? ? [-D $var] #假设给定的变量包括的是文件夹. The return True

? ? [-e $var] #假设给定的变量包括的文件存在. The return True

? ? [-W $var] #假设给定的变量包括的内容可写. The return True

? ? [-D $var] #假设给定的变量包括的内容可读. The return True

? ? [-L $var] #假设给定的变量包括的是一个符号链接 returns True

? ?


? ? Comparison of strings:

?    ? ? be able to check if two strings are equal and unequal:

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

?    ? ? Compare the size between the two strings:

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

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

?    ? ? [[-N $STR 1]] assumes str1 is a non-empty string. Then return true?

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

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.