相关文章推荐

刚开始使用Ubuntu的时候,由于Ubuntu的GUI已经做的很好了,只是觉得和windows不是一个风格的界面而已。后来接触了命令行,感觉比用图形交互界面的客户端要随心多了。时间久了,开机是要启动的程序越来越多了,写程序时也越来越依赖命令行了,就发现一条一条的从终端输入命令有点儿麻烦了。当然了,Linux能流行到今天,那一定有它的解决办法咯!哈哈,那就是今天的主题: Linux脚本文件

the first shell file

create the .sh file

for example:
让我们创建一个 first.sh 文件

echo “hello word!”

然后打开文件,开始编写shell脚本
注意: #号后面表示注释

1
2
3
4
# 注意第一行的代码解释器的指定,这里使用的是/bin/bash/ 解释器
#! /bin/bash
# 输出hello word!
echo "hello word!"

然后保存文件即可

execute first.sh

然后我们在终端输入: sh first.sh
然后就会惊喜的发现终端输出了 hello word!
是的,这样我们的第一个脚本文件就算是执行成功了。

编写利用终端发送http请求的脚本文件

注意:在发送http请求之前,后台的服务要开启。我这里用的是 Spring , 当然你可以参考我写的另一篇文章 Accessing JPA Data with REST , 这样也许能够更明白我写这篇文章的意思了,当然如果读不懂也没关系啦!你可以参考下面 菜鸟教程 去编写自己需要的shell脚本。

create the curl.sh file

在终端输入命令: touch curl.sh
就会发现当前目录下已经建好了crul.sh文件,这是是不是要比右击,新建文件要快呢!哈哈

编写shell文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#! /bin/bash
# 环境
# 定义http请求的动词
POST="POST"
DELETE="DELETE"
# 查看spring server提供的方法
echo "RESTful is:"
curl http://localhost:8080 # crul命令行
echo "\n请输入url:" # \n是换行符
read url_name # 从屏幕上输入一个字符串并赋值给url_name
base_url="http://localhost:8080/" #最基本的url
your_url="$base_url$url_name" #拼接字符串
echo "拼接成的url是: $your_url"
echo "看一下我们可以测试除了REST之外还有哪儿些方法和所有的数据:"
curl $your_url
echo "\n开始测试REST方法: 请输入http的请求方法"
read method #读取屏幕上输入的方法
#if-else条件判断语句
if test $method = $POST #用test命令判断两个字符串是否相等
then
curl -i -X POST -H "Content-Type:application/json" -d '{"firstName":"Frodo","lastName":"Baggins","emailAddress":"mengynzhi@yunzhiclub"}' http://localhost:8080/people # 发送http请求保存数据
echo "\n保存数据成功"
elif test $method = $DELETE
then
echo "查看所有的数据"
curl $your_url
echo "\n输入想要删除的数据id"
read id
delete_url="$your_url/$id" #拼接删除的url
delete="-X DELETE $delete_url" #拼接删除命令
curl $delete #执行删除命令
fi

execute the curl.sh

让我们看一看执行的结果吧!终端输入命令行: sh curl.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
RESTful is:
{
"_links" : {
"people" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile"
}
}
}
请输入url:
people
拼接成的url是: http://localhost:8080/people
看一下我们可以测试除了REST之外还有哪儿些方法和所有的数据:
{
"_embedded" : {
"people" : [ {
"emailAddress" : "mengynzhi@yunzhiclub",
"lastName" : "Baggins",
"firstName" : "Frodo",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
},
"person" : {
"href" : "http://localhost:8080/people/1"
}
}
}, {
"emailAddress" : "mengynzhi@yunzhiclub",
"lastName" : "Baggins",
"firstName" : "Frodo",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
},
"person" : {
"href" : "http://localhost:8080/people/2"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile/people"
},
"search" : {
"href" : "http://localhost:8080/people/search"
}
}
}
开始测试REST方法: 请输入http的请求方法
DELETE
查看所有的数据
{
"_embedded" : {
"people" : [ {
"emailAddress" : "mengynzhi@yunzhiclub",
"lastName" : "Baggins",
"firstName" : "Frodo",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
},
"person" : {
"href" : "http://localhost:8080/people/1"
}
}
}, {
"emailAddress" : "mengynzhi@yunzhiclub",
"lastName" : "Baggins",
"firstName" : "Frodo",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
},
"person" : {
"href" : "http://localhost:8080/people/2"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile/people"
},
"search" : {
"href" : "http://localhost:8080/people/search"
}
}
}
输入想要删除的数据id
2

此时如果我们在执行一次的话就会发现id为person表中2的数据删除了。当然了,你也可以直接去数据库查看。当然,在这里我只写了 POST, DELETE 这两个方法,curl命令是如此丰富和功能强大,需要我们慢慢学习。路漫漫其修远兮呀!嘻嘻。

Code download

Dowload ZIP

参考文章

Shell 变量 菜鸟教程
Shell 流程控制 菜鸟教程
Shell test 命令 菜鸟教程
编写一个简单的linuxshell脚本 百度经验
Linux curl命令详解 Linux系统教程
shell怎么打印换行 segmentfault