Variables/Math Operations/Sub-processes/basic operations, etc.

Source: Internet
Author: User
Tags echo command

Snake stick, I always forget the snake stick, the memory is getting worse

#!/bin/bash

#!/usr/bin/env Bash

The shell is also a language of weak data types, stating that variables are variable names = variable values, and of course all variables in the shell are strings by default

Variable_1=1;

If used, the variable name can be used directly before adding the $ symbol.

echo $variable _1;

When the variables in the shell need to be mathematical operations, because all the variables are strings, so you need to use the command mode to do mathematical operations, integer operations using let $[[]] ()

var_1=1;var_2=2;

Echo ' Let var_1+var_2 ';

echo $[[var_1+var_2]];#$[[$var _1+ $var _2]];

Echo (var_1+var_2); #$ ($var _1+ $var _2);

Decimal operations require BC commands, and BC can be used not only as a decimal calculation, but also as a binary conversion. Remember IBase obase "inbase/outbase" specifies the decimal point scale "but it looks like it only works when you do division."

var_1=1.3;var_2=1.2;

echo $var _1+ $var _2 | bc

echo "$var _1+ $var _2;scale=2" | bc

echo "SCALE=3;1/3" | BC #. 333;

echo $ (printf "%.5f" ' echo ' scale=5;1/3 "| BC '); # 0.333

The Shell has two ways (shell command) to fork the child process, the "shell command", even if the use of parentheses, the use of the anti-reference, sometimes when the child process return value, does not preserve the child process in the normal return string contained in the line-break symbol, You need to use it as a double quotation mark in order to display the line break correctly

When the echo command prints a string by default, it Surga line line breaks, common parameters

-N to remove line endings automatically add line breaks

-E to escape sequence[escape sequence] means output

echo "123\n321";

The output is as follows

123\n321

Echo-e "123\n321"

The output is as follows

123

321

echo $ (echo-e "123\n321");

The output is as follows, the newline character in the subprocess is lost, and it becomes a space

123 321

echo "$ (echo-e" 123\n321 ")"

The output is as follows

123

321

Gets the string length #符号

var_1= "Test";

echo ${#var_1}

Array

Arr1= (1 2 3 4)

There is no need for any delimiter between array elements (Touple) to be a python.

Print all elements in an array

echo ${arr_1[@]};# Echo ${arr_1[*]}

Print elements of an array individually

Echo ${arr_1[1]}

The length of the array, and hit the # number again.

echo ${#arr1 [@]}; #echo ${#arr1 [@]};

Associative arrays

Declare-a arr_2

Arr_2= ([Ind1]=val1 [Ind2]=val2 [IND3]=VAL3];

Echo ${arr_2[ind2]}

Get associative array all values are the same as indexed arrays

echo ${arr_2[@]};echo ${arr_2[*]}

Get associative array All keys yes, it's Zhou Dong's exclamation point ~ ~

echo ${!arr_2[@]}; echo {!arr_2[*]}

Stty I remember his name is daily surplus because there is a project called Daily surplus

He showed me just one user close and turn on Echo

#!/bin/bash

Stty-echo #关闭回显

Read passwd

Stty Echo #开启回显

Echo $passwd

Get user input

Read var_1

The keyboard input value is stored in the var_1 variable, but you need to press ENTER after entering the content to

Other options

-n Specifies the length of the character to get Read-n 3 var_2; #输入长度达到3 's enough.

-t Specifies the wait time to get input read-t 3 val_2; #两秒的时间获取变量值, outdated.

-d Specifies the delimiter to get the value read-d; var_2# "input val2;" Current_var_2=val2

-P will also output the name of the variable to the terminal

The-s input is not displayed in the terminal

Tput setting up terminal-related

Tput Cup x y #将当前光标位置置于x y-coordinate point

Tput SC #存储当前光标的位置 Store Current "should not be current"

Tput RC #置回之前存储到的光标位置 Restore C

Tput Ed #删除当前光标位置到行尾的内容

Here is a very interesting shell bar, receive input an integer "must be an integer, and then display on the screen count:1--Count:2---> count:n" The position of the number does not change

#!/bin/bash

Read number

Echo-n "Count:";

count=0;

while [[$count-lt $number]];

Do

Tput SC;

Echo-n $count;

Sleep 1;

Tput RC;

Tput Ed;

Let Count+=1;

Done

While loop, where conditions in the if branch are in [[]] or (), there must be a space between the conditional expression and the left and right brackets

if [[x < y]];

Then

Statement

Fi

Cat command

-T Display tab

-S compresses multiple empty rows

-N Displays the line number of each line

Cat File1 file2 #拼接文件

Cat-file1 #拼接标准输入和文件

Let's do it today ...

Variables/Math Operations/Sub-processes/basic operations, etc.

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.