Chapter 1, Terminal Printing, Arithmetic Operations, Common Variables

Chapter 1, Terminal Printing, Arithmetic Operations, Common Variables

1. Terminal printing

echo

[root@ceshi ~]# echo hello world

hello world

[root@ceshi ~]# echo 'hello world'

hello world

[root@ceshi ~]# echo "hello world"

hello world

printf

[root@ceshi ~]# printf "hello world\n"  # n代表换行
hello world
[root@ceshi ~]# vi info.sh        
#!/bin/bash

printf "%-5s %-10s %-4s\n" Num Name Mark
printf "%-5s %-10s %-4d\n" 1 aa 90
printf "%-5s %-10s %-4.2f\n" 1 bbb 80.1234
printf "%-5s %-10s %-4.3f\n" 1 ccccc 60.1234

%-5s 表示格式为左对齐,且宽度为5的字符串代替(-表示左对齐),不使用-则是右对齐(s表示字符串)
%d 代表十进制整数
%-4.3f 表示格式为左对齐宽度为4,'.3'代表保留3位小数,f代表浮点数

[root@ceshi ~]# /bin/bash info.sh 
Num   Name       Mark
1     aa         90  
1     bbb        80.12
1     ccccc      60.123

Format Alternatives

  1. %b means that the corresponding parameter is treated as a string with escape sequences to be processed
  2. %c ASCII character. Display the first character of the corresponding parameter
  3. %d, %i both represent decimal integers
  4. %e, %E, %f represent floating point types
  5. %g, %e or %f conversion, whichever is shorter, remove trailing zeros
  6. %G, %E or %F conversion, whichever is shorter, remove trailing zeros
  7. %o unsigned octal value
  8. %s represents string type
  9. %u unsigned decimal
  10. %x unsigned hexadecimal value, use af for 10-15
  11. %X unsigned hexadecimal value, use AF for 10-15
  12. %% literal %

escape sequence

  1. \a warning character, usually the ASCII bel character
  2. \b go back
  3. \c suppresses any trailing newline characters in the output (only valid in the %b designator parameter), and any characters left in the parameter, any subsequent parameters, and any characters left in the format string, are be ignored.
  4. \f form feed (English formfeed)
  5. \n newline
  6. \r carriage return (English Carriage return)
  7. \t horizontal tab, (in fact, the line where it is located does not wrap)
  8. \v vertical tab,
  9. \ literal backslash
  10. \ddd character representing a 1-3 digit octal value, valid only in format strings
  11. \0ddd represents a 1-3 digit octal value character

Escape newlines in echo contains

echo -e "string containing escape sequences"

-e : The escape sequence in the string can be recognized, and the escape sequence is not recognized by default without e

[root@ceshi ~]# echo -e "你好\t你好\t"
你好    你好
[root@ceshi ~]# echo -e "你好\v你好\v"
你好
    你好

# 不加-e 就没有识别转义序列
[root@ceshi ~]# echo "你好\t你好\t"   
你好\t你好\t
[root@ceshi ~]# echo "你好\v你好\v"   
你好\v你好\v

print color output

[root@ceshi ~]# echo -e "\033[5;31;42m你好\033[0m" 

Reference: http://blog.51cto.com/506554897/1932861

2. Arithmetic operations

Integer operations

let operation command

[root@ceshi ~]# vi let.sh
#!/bin/bash
num1=2
num2=3
let result=num1+num2
echo $result

运行:
[root@ceshi ~]# /bin/bash let.sh 
5
  1. Self-increment operation let num1++

  2. Decrement operation let num1--

  3. Short form: let no+=10let ; let no-=20

    Etc.: let no = no + 10; let no = no-20

operator[] operation method

[root@ceshi ~]# vi fangkuohao.sh
#!/bin/bash
num1=2
num2=3
result=$[$num1+num2]
echo $result

运行:
[root@ceshi ~]# /bin/bash fangkuohao.sh 
5

Note: The usage method is similar to let, and the $ prefix can be used in []

(()) operation method

[root@ceshi ~]# vi xiaokuohao.sh
#!/bin/bash
n1=2
n2=3
result=$((n1+n2))
echo $result

expr operation method

[root@ceshi ~]# expr 2 + 3         
5
[root@ceshi ~]# num1=5
[root@ceshi ~]# r=$(expr $num1 + 5)
[root@ceshi ~]# echo $num1
5
[root@ceshi ~]# echo $r
10

Common operators for expr

  • +
  • -
  • *
  • /
  • % Modulo operation, also called remainder

Precision calculation

Advanced computing tools: bc

It can perform floating point operations and some advanced functions

[root@ceshi ~]# echo "1.25*3" | bc
3.75

Set the decimal precision (that is, how many decimal places are displayed)

scale=2 means the decimal point is displayed with 2 digits

[root@ceshi ~]# echo "scale=2;7/3" | bc  
2.33

base conversion

十进制转二进制:
[root@ceshi ~]# a=192
[root@ceshi ~]# echo "obase=2;$a" |bc
11000000

二进制转十进制:
[root@ceshi ~]# b=11000000                   
[root@ceshi ~]# echo "obase=10;ibase=2;$b"|bc
192

Calculate squares and square roots

求2的三次方:
[root@ceshi ~]# echo "2^3"|bc  
8

求100的平方根
[root@ceshi ~]# echo "sqrt(100)"|bc
10

3. Common variables

Different ways to assign values ​​to variables

Double quotes "" allow other variable values ​​to be quoted using the $ symbol

Single quotes '' prohibit quoting other variable values, $ is treated as ordinary characters

The backtick `` prints the demerit of the command execution to a variable; for example:

sed -i s/"B"/"b"/g `grep -rl "B" --exclude="*.sql" ceshi/*`  将grep得到的文件传输给sed使用

user-defined variable

Set the scope of the variable

Format:

export variable name

export variable name = variable value

Clear variable names:

unset variable name

自定义变量
[root@ceshi ~]# export a            #自定义变量a
[root@ceshi ~]# export b=222        #自定义变量b
[root@ceshi ~]# a=111               #给变量a赋值

[root@ceshi ~]# echo $a             #打印变量a的值
111
[root@ceshi ~]# echo $b             #打印变量b的值
222

清除变量:
[root@ceshi ~]# unset a             #清除变量a
[root@ceshi ~]# unset b             #清除变量b
[root@ceshi ~]# echo $a             #输出为空

[root@ceshi ~]# echo $b

environment variable

environment variable configuration file

  • Global configuration file: /etc/profile

  • User profile: ~/.bash_profile

View environment variables:

The set command can view all shell variables, including environment variables

Common environment variables

  • $USER view and user information
  • $logname login related information
  • $UID
  • $Shell
  • $HOME
  • $pwd
  • $PATH Where to look for commands entered by the user
  • $ PS1
  • $PS2
  • $RANDOM random number

position variable

Represented as: $n (n is a number between 1-9)

#./test.sh one two three four five six
  • $0 means the filename itself

  • $1 means one

  • $2 means two

    And so on

predefined variables

  1. $# number of positional arguments on the command line
  2. $* contents of all positional arguments
  3. $? The status returned after the last command is executed, 0 means success, non-0 means abnormal execution or error
  4. $$ Process ID of the current process
  5. $! The last process number running in the background
  6. $0 currently executing process/program name

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326176560&siteId=291194637