一、撰写第一个Scripts
(1)echo
[root@localhost dalianmao]# echo '12345';echo '6789'
12345
6789
[root@localhost dalianmao]# echo -n '12345';echo '6789'
123456789
\a 发出警告声;
\b 删除前一个字符;
\c 最后不加上换行符号;
\f 换行但光标仍旧停留在原来的位置;
\n 换行且光标移至行首;
\r 光标移至行首,但不换行;
\t 插入tab;
\v 与\f相同;
\ 插入\字符;
\nnn 插入nnn(八进制)所代表的ASCII字符;
[root@localhost dalianmao]# echo -e 'abc\ncdfr'
abc
cdfr
(2)Hello World
[dalianmao@localhost ~]$ mkdir scripts;cd scripts
[dalianmao@localhost scripts]$ vi HelloWorld.sh
[dalianmao@localhost scripts]$ ll
total 4
-rw-rw-r--. 1 dalianmao dalianmao 235 Nov 7 00:57 HelloWorld.sh
[dalianmao@localhost scripts]$ sh HelloWorld.sh
Hello World!
1、第一行#!/bin/bash 在宣告这个shell使用的shell名称
他能够加载bash的相关环境配置文件(一般来说就是non-login shell的~/.bashrc),执行bash来使下面的指令能够执行。
2、程序内容的说明
3、主要环境变量宣告
将重要的环境变量设定好,如PATH,这样,我们可以直接下达一些外部指令,而不必写绝对路径。
4、主要程序部分
5、执行成果告知
exit 0 代表离开script并且回传一个0给系统,echo $? 可以得到0值
二、开始实战
1)随日期变化,建立档案
[dalianmao@localhost scripts]$ clear
[dalianmao@localhost scripts]$ vi dalianmao.sh
[dalianmao@localhost scripts]$ ll
total 8
-rw-rw-r--. 1 dalianmao dalianmao 563 Nov 7 04:39 dalianmao.sh
-rw-rw-r--. 1 dalianmao dalianmao 235 Nov 7 00:57 HelloWorld.sh
[dalianmao@localhost scripts]$ sh dalianmao.sh
Please input your filename:dashabi
[dalianmao@localhost scripts]$ ll
total 8
-rw-rw-r--. 1 dalianmao dalianmao 563 Nov 7 04:39 dalianmao.sh
-rw-rw-r--. 1 dalianmao dalianmao 0 Nov 7 04:39 dashabi20181105
-rw-rw-r--. 1 dalianmao dalianmao 0 Nov 7 04:39 dashabi20181106
-rw-rw-r--. 1 dalianmao dalianmao 0 Nov 7 04:39 dashabi20181107
-rw-rw-r--. 1 dalianmao dalianmao 235 Nov 7 00:57 HelloWorld.sh
2)数据运算
1、var=$((运算内容))
2、declare -i var=运算内容
运算符:
+ - * / %
1、输入两个数,输出两个数的乘积和取余
[dalianmao@localhost scripts]$ vi dalianmao_caculate.sh
[dalianmao@localhost scripts]$ ll
total 12
-rw-rw-r--. 1 dalianmao dalianmao 469 Nov 8 11:02 dalianmao_caculate.sh
-rw-rw-r--. 1 dalianmao dalianmao 563 Nov 7 12:39 dalianmao.sh
-rw-rw-r--. 1 dalianmao dalianmao 0 Nov 7 12:39 dashabi20181105
-rw-rw-r--. 1 dalianmao dalianmao 0 Nov 7 12:39 dashabi20181106
-rw-rw-r--. 1 dalianmao dalianmao 0 Nov 7 12:39 dashabi20181107
-rw-rw-r--. 1 dalianmao dalianmao 235 Nov 7 08:57 HelloWorld.sh
[dalianmao@localhost scripts]$ sh dalianmao_caculate.sh
Please input two inter numbers!
fist number:13
second number:3
the first result is:39
the second result is:1
[dalianmao@localhost scripts]$ nl dalianmao_caculate.sh
1 #!/bin/bash
2 # Program:
3 # User inputs 2 integer numbers;Program will cross these two numbers
4 # History:
5 # 2018/11/8 dalianmao v1.0
6 PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:~/bin
7 export PATH
8 echo -e "Please input two inter numbers!"
9 read -p "fist number:" num1
10 read -p "second number:" num2
11 num3=$(($num1*$num2))
12 declare -i num4=$num1%$num2
13 echo -e "the first result is:$num3 \n"
14 echo -e "the second result is:$num4 \n"
15 exit 0
3) script 的执行方式差异(source,sh script, ./script)
1、用sh或者bash或者 ./script 来执行,程序都是在开启新的子进程中执行,定义的变量在父进程中无效。
[dalianmao@localhost scripts]$ bash dalianmao_caculate.sh
Please input two inter numbers!
fist number:13
second number:3
the first result is:39
the second result is:1
[dalianmao@localhost scripts]$ echo $num3
[dalianmao@localhost scripts]$ chmod 777 dalianmao_caculate.sh
[dalianmao@localhost scripts]$ ./dalianmao_caculate.sh
Please input two inter numbers!
fist number:13
second number:3
the first result is:39
the second result is:1
[dalianmao@localhost scripts]$ echo $num3
[dalianmao@localhost scripts]$
2、利用source来执行脚本:在父程序中执行,文件中定义的变量还能用。
[dalianmao@localhost scripts]$ ll
total 12
-rwxrwxrwx. 1 dalianmao dalianmao 469 Nov 8 11:02 dalianmao_caculate.sh
-rw-rw-r--. 1 dalianmao dalianmao 563 Nov 7 12:39 dalianmao.sh
-rw-rw-r--. 1 dalianmao dalianmao 0 Nov 7 12:39 dashabi20181105
-rw-rw-r--. 1 dalianmao dalianmao 0 Nov 7 12:39 dashabi20181106
-rw-rw-r--. 1 dalianmao dalianmao 0 Nov 7 12:39 dashabi20181107
-rw-rw-r--. 1 dalianmao dalianmao 235 Nov 7 08:57 HelloWorld.sh
[dalianmao@localhost scripts]$ vi dalianmao_caculate.sh
[dalianmao@localhost scripts]$ source dalianmao_caculate.sh
Please input two inter numbers!
fist number:13
second number:3
the first result is:39
the second result is:1
[dalianmao@localhost scripts]$ echo $num3 $num4
39 1
[dalianmao@localhost scripts]$ nl dalianmao_caculate.sh
1 #!/bin/bash
2 # Program:
3 # User inputs 2 integer numbers;Program will cross these two numbers
4 # History:
5 # 2018/11/8 dalianmao v1.0
6 PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:~/bin
7 export PATH
8 echo -e "Please input two inter numbers!"
9 read -p "fist number:" num1
10 read -p "second number:" num2
11 num3=$(($num1*$num2))
12 declare -i num4=$num1%$num2
13 echo -e "the first result is:$num3 \n"
14 echo -e "the second result is:$num4 \n"
注:把文件中的exit 0去掉哈!!