Linux Basic Script Building

内容概要: use multiple commands, creating script file, echo, variables, redirection, pipes, performing math, exiting the script

1.use multiple commands:
一次运行多个command
1
2
3
$ date;who
2017年 06月 05日 星期一 13:26:29 CST
jay tty7 2017-06-05 09:48 (:0)

2.creating a script file:
给用户添加执行权限: chmod u+x test
1
2
3
4
#!/bin/bash
# The first line is used to specify the shell you are using!
date
who

3.echo:
Displaying messages.
1
2
3
4
5
echo "The use is: $USER"

# display onthe same line.
echo -n "Today is: "
date

4.variables:
environment variables:
  • echo $UID
  • echo “User info for userid: $USER”
  • echo HOME: $HOME
  • echo “The cost of this item is : \$15”
user variables:
1
2
3
4
var1=1
var2="zhangie"
var3=$var1
echo "$var2 checked in $var1 days ago!"

5.backtick():
The slowly back quote character, usually called backtick() on the same key as the tilde symbol(~).
The backtick allows you to assign the output of a shell command to a variable
1
2
3
4
5
#!/bin/bash
var=`date`
guest=who
echo The date and time is: $var
echo Current user is: $guest
例如,执行一个命令,将其结果保存到一个今天日期命名的日志文件中:
1
2
3
#!/bin/bash
today=`date +%y%m%d`
ls -l > log.$today

6.Redirecting input and output:
Output redirecting
  • date > file : 覆盖file之前所有的内容
  • date >> file : 追加在file之前内容的后面
    Input redirecting
  • 1.Take the content of a file and redirect it to a command:
    • wc < file
  • 2.Inline input redirection: The inline input redirection symbol is the double less-than symbol(<<). Besides this symbol, you must specify a text marker that delineates the beginging and end of the data used for input
    • wc << EOF
      1
      2
      3
      4
      5
      6
      $ wc << EF
      > ZHANGJIE
      > SFDKLD
      > SDFL
      > EF
      3 3 21

7.Pipes:
comand1 | command2 | command3

Piping provides a way to link commands to provide more detailed output.
不要认为几个commands一个接一个执行,linux系统实际上是同时执行这几个命令.

ls -al | awk ‘{print $9}’ | more
awk ‘{if(NR>143&&NR<168) print $0}’ LinuxStructuredCommands.md | /bin/bash


8.Performing Math:
1.expr : a special command used for processing mathematical equations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
jay@17:05:31-> ~/blog/node$ expr 1 + 2
3
jay@17:05:42-> ~/blog/node$ expr 1 - 2
-1
jay@17:05:44-> ~/blog/node$ expr 1 / 2
0
# 使用转义字符
jay@17:05:48-> ~/blog/node$ expr 1 \* 2
2
jay@17:06:21-> ~/blog/node$ expr 1 == 2
0
jay@17:07:18-> ~/blog/node$ expr 1 != 2
1
jay@17:07:21-> ~/blog/node$ expr length zhangjhie
9

2.Using brackets ($[operation]) : a much easier way of performing mathematical equations.

But the bash shell mathematical operators only support integer arithmetic which is a huge limitation!
注意: 下面的script只能用/bin/bash test执行,不能使用sh test执行

1
2
3
4
5
6
#!/bin/bash
var1=100
var2=50
var3=45
var4=$[var1*($var2+$var3)]
echo "The final result is : $var4"


  • The floating-point arithmetic is controlled by a built-in variable called scale, default value of scale=0.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ bc
    3.44 / 5
    0

    scale=4
    3.44/5
    .6880

    var=1
    var2=var/5
    print var2
    .2000

    quit

4.using bc in scripts for short calculations: variable=`echo “options:expression”|bc`
1
2
3
4
5
6
7
#!/bin/bash
var1=20
var2=3.1415926
var3=`echo "scale=4;2*$var1*$var2"|bc`
var4=`echo "scale=4;$var2*$var1*$var1"|bc`
echo The circumference is $var3
echo The area is $var4
5.using bc in scripts for complicated calculations: variable=\’bc << EOF options statements expressions\’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
```shell
#!/bin/bash
var1=20
var2=3.1415926
var3=`bc<<EOF
scale=4
$var1*$var2*2
EOF
`
var4=`bc<<EOF
scale=4
$var1*$var1*$var2
EOF
`
echo The circumference is $var3
echo The area is $var4

9.Exiting the script:
The exit status is an integer value between 0 and 255.
Code Description
0 Successful completion of the command
1 General unknown error
2 Misuse of shell command
126 THe command cannot execute
127 Command not found
128 Invalid exit argument
128+ Fatal error with linux signal x
130 Command terminated with Ctrl-C
255 Exit status out of range
Every command that runs in the shell uses an exit status value from the last command that executed.
1
2
3
4
5
6
7
8
9
#!/bin/bash
var1=10
var2=30
#var3=$[$var1+$var2]
#var3=$[var1+var2]
var3=$[$var1*var2]
echo The answer is $var3
exit 5
exit $var3
You must view or use $? variable immediately after teh command you want to check.
1
2
3
4
5
6
7
8
jay@14:03:01-> ~$ date
2017年 06月 05日 星期一 14:03:09 CST
jay@14:03:09-> ~$ echo $?
0
jay@14:03:13-> ~$ sfsdf
sfsdf:未找到命令
jay@14:03:15-> ~$ echo $?
127
The exit status code is reduced to fit int the 0 to 255, the shell does this by using modulo arithmetic.
1
2
3
#!/bin/bash
# 执行之后,$?=44(300 mod 256)
exit 300