[Linux] Bash script

1.1 Variable Types

The concept almost all programming languages have variables, Bash is certainly no exception. He said variables can not escape talk about types. But the difference is that other languages Bash belong untyped language, if a strong say in one model and are considered a string . Here, you might say, Bash and there was an integer type it, it seems to have integer arithmetic. But through some operators, expressions or treated as integer processing command string, its nature or character string.

1.2 declare variables

Bash variables and other scripting languages, are not required to declare, directly used in. So the first occurrence of variables are associated with initialization.

1.3 Variable access

Before the variable plus a $ symbol. Fu called the dollar, that dollar sign. This is the most basic kind of access way, the other way after another table.

1
2
a="hello world"  
echo $a

The terminal will print out hello world. Of course, you can put a string of variables and constants together print, such as

1
2
boy=Jelly
echo "$boy:hello world!"

The results for the Jelly: hello world!

1.4 initialization / bind variables

Bash the variables since no statement, so for the first time will be accompanied by initialization. Variable initialization (or assignment) in a manner bash There are three types: direct assignment, and a read input command substitution.

1.4.1 direct assignment

A direct assignment is to use the equal sign, which is common in other programming languages, such as:

1
2
3
4
a=123
b=abc
c=‘123
d=“hello world”
Note the space

Note that the left and right of the equal sign without spaces ! ! ! For example, if there is a = 22, it will direct the implementation of the .sh error

image-20180918165431334

About quotes

Second, we must understand that, because all variables have said are viewed as strings, so in fact, a = 123 and a = "123" is no different. But if your variable contains spaces you will certainly need to use quotation marks to wrap up.

Single and double quotation marks acting generally the same , except for some special cases:

1
2
3
4
a="I'm Jelly"
b='Jelly:"Hi"'
echo $a
echo $b

When a string containing single quotes outside then use double quotes, and vice versa.

1.4.2 read input

Reading terminal input to the variable assignment is to use the read command. read and echo are the same as the embedded command. Direct look at the code:

1
2
3
echo -n "Please Input your name:"
read name
echo "Hi,$name,welcome to uncle Jelly's cabin!"

Or you may use the read command -p option to simplify the above code:

1
2
read -p "Please Input your name:" name
echo "$name,welcome to uncle jelly's cabin!"

1.4.3 Replace command

This is a very practical way of initialization or assignment. Use other commands to the output of a variable assignment. This need to use anti-references symbol ( "` ")

1
2
3
4
= `dir pwd ` 
Tim date` = `
echo " in my $ dir directory "
echo " the time is now $ Time "

Here called pwd and date two commands. You can type directly in the terminal these two commands, look at the print results. Backticks is to output `` in command assigned to the variable.

image-20180918164616012

note

使用反引号进行命令替换赋值,且直接打印该变量时,变量中的n换行符会被替换成空格:

1
2
3
4
5
ps1=`ps`
echo $ps1

ps2=`ps`
echo "$ps2"

image-20180918165220708

除此之外,也可以通过$()的方式进行命令替换赋值:

1
2
3
4
dir=$(dir)
tim=$(date)
echo "$dir"
echo "$tim"

image-20180918165725699

Note:
  • 修改shell脚本权限或执行shell脚本的时候,在shell脚本名前加./,即当前目录下查找,否则会在PATH下开始查找,避免不必要的麻烦

    image-20180918170334310

  • 当shell脚本执行过程中发现shell脚本存在错误时(比如使用了一个未定义的变量),该错误代码会被跳过,并且继续执行错误代码之后的所有代码(而不是立刻执行当前shell脚本的执行)

image-20180918171320281

2 数值计算

前面提到,Bash把所有变量都视为字符串。比如,a=1+2,$a并不等于3,而是等于字符串1+2。因此Bash中的数学计算并不如其他语言那样简便。

运算符[]

为了解决这个问题,我们可以使用运算符[]

在中括号中引用变量可以直接使用变量名,也可以使用$加变量名

1
2
3
4
5
6
7
8
9
10
11
a=2
b=3

c1=$[$a+4]
echo $c1

c2=$[a+5]
echo $c2

c3=$[$a+$b]
echo $c3

image-20180918192233448

expr及其反引用

1
2
3
4
5
6
7
a=2
b=3
expr $a + $b
expr $a - $b
expr $a * $b
expr $a / $b
expr $a % $b

需要注意的是:

  • 操作符和操作数之间一定要有空格间隔
  • 操作数(即变量)前必须有$符
  • 乘号*,要用反斜杠进行转义
  • 该命令会将计算结果打印到标准输出
  • 仅支持整数运算
  • 也可以直接使用数字的字面值

将expr的计算结果赋值给一个变量呢:

1
c=`expr $a + $b`

Reference

Original: Big Box  [Linux] Bash script


Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11639502.html