linux - shell脚本中如何获取md5值并curl这个值
阿神
阿神 2017-04-17 14:36:47
0
2
791

需求是动态生成每天的日期,md5这个日期,然后curl到一个网址。
我第一次用shell,研究了下,初步代码如下:

#!/bin/bash
a=`date '+%Y-%m-%d'`
echo $a
code=$($a|md5sum|cut -d '' -f1)
echo $code
echo -n $a|md5sum
curl -v http://www.abc.com/report/?code=$code

问题1:md5sum赋值到变量和直接echo的值是不一致的
问题2:curl如何不打印请求的结果

阿神
阿神

闭关修行中......

reply all(2)
黄舟

Problem 1: I have never encountered a situation where the value assigned by md5sum to a variable and the value of direct echo are inconsistent

[root@localhost ~]# md5sum email.txt 
6b15e74bf464475b4e18a9aa208b10d8  email.txt
[root@localhost ~]# cat email.txt 
i@catscarlet.com
[root@localhost ~]# echo i@catscarlet.com|md5sum
6b15e74bf464475b4e18a9aa208b10d8  -
[root@localhost ~]# 

[root@localhost ~]# a='i@catscarlet.com'
[root@localhost ~]# echo $a|md5sum 
6b15e74bf464475b4e18a9aa208b10d8  -

Your code cannot run the fourth line at least under bash

[root@localhost ~]# bash -x test.sh  
++ date +%Y-%m-%d
+ a=2016-03-30
+ echo 2016-03-30
2016-03-30
++ 2016-03-30
test.sh: line 4: 2016-03-30: command not found
++ cut -d '' -f1
++ md5sum
+ code='d41d8cd98f00b204e9800998ecf8427e  -'
+ echo d41d8cd98f00b204e9800998ecf8427e -
d41d8cd98f00b204e9800998ecf8427e -
+ echo -n 2016-03-30
+ md5sum
a450f2d7030074e211755cdde0d41a57  -
[root@localhost ~]# 

Question 2:
curl -s 你的网址 -o /dev/null

Additional question 1:

[root@localhost www]# date '+%Y-%m-%d'
2016-03-30
[root@localhost www]# date '+%Y-%m-%d'|md5sum
3a2f84ded4cc91e1276040cf0f27a681  -
[root@localhost www]# echo 2016-03-30 |md5sum 
3a2f84ded4cc91e1276040cf0f27a681  -

In your script

a=`date '+%Y-%m-%d'`

This paragraph means copying the output result of date to a. Now the value of a is 2016-03-30
to the next line

code=`$a|md5sum`

actually became

code=`2016-03-30|md5sum`

2016-03-30 is not a command, it is a syntax error.

If you are lazy, you can also write like this:

#!/bin/bash
a=`date '+%Y-%m-%d'`
code=`echo -n $a|md5sum`
echo $code
echo -n $a|md5sum

PS: Echo needs to add -n, otherwise the output results will be affected by line breaks. I didn't add the previous code, and md5 became 3a2f84ded4cc91e1276040cf0f27a681. 2016-03-30 The correct md5 value should be a450f2d7030074e211755cdde0d41a57.

黄舟

I need to assign the generated md5 value to code, and then reference the code elsewhere
After organizing the code, the following output md5 is still inconsistent

#!/bin/bash
a=`date '+%Y-%m-%d'`
code=`$a|md5sum`
echo $code
echo -n $a|md5sum
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!