把grep&;在bash脚本中插入变量

把grep&;在bash脚本中插入变量,bash,curl,grep,cut,Bash,Curl,Grep,Cut,我不熟悉Stackoverflow,也不熟悉bash脚本,所以请原谅我问了这样一个愚蠢的问题。我在这里浏览了很多答案,但似乎没有什么对我有用 我正在尝试制作一个小脚本来检查wordpress.org的最新版本,并检查我是否已经在脚本所在的同一目录中拥有该文件: #!/bin/bash function getVersion { new=$(curl --head http://wordpress.org/latest.tar.gz | grep Content-Disposition | cu

我不熟悉Stackoverflow,也不熟悉bash脚本,所以请原谅我问了这样一个愚蠢的问题。我在这里浏览了很多答案,但似乎没有什么对我有用

我正在尝试制作一个小脚本来检查wordpress.org的最新版本,并检查我是否已经在脚本所在的同一目录中拥有该文件:

#!/bin/bash

function getVersion {
new=$(curl --head http://wordpress.org/latest.tar.gz | grep Content-Disposition | cut -d '=' -f 2)
echo "$new"
}

function checkIfAlreadyExists {
    if [ -e $new ]; then
        echo "File $new does already exist!"
    else
        echo "There is no file named $new in this folder!"
    fi
}

getVersion
checkIfAlreadyExists
它的工作原理如下:

jkw@xubuntu32-vb:~/bin$ ./wordpress_check 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
wordpress-3.4.1.tar.gz
 in this folder! named wordpress-3.4.1.tar.gz
jkw@xubuntu32-vb:~/bin$ 
因此,我使用curl&grep&cut获得了正确的文件名,但是变量有问题。当我把它打印在第5行时,它看起来不错,但当打印在第12行时,它看起来很有趣。另外,if语句不起作用,我的文件确实在同一个目录中

如果我在一个文本文件中输出curl--head | grep Content Disposition | cut-d'=''-f2的结果,我最后似乎得到了一行新行,这可能是问题所在吗?如果通过管道将命令传输到xdd,则如下所示:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
0000000: 776f 7264 7072 6573 732d 332e 342e 312e  wordpress-3.4.1.
0000010: 7461 722e 677a 0d0a                      tar.gz..
…我不明白

我已经尝试过通过tr'\n'\0'或tr-d'\n'来传递命令,正如在这里的许多类似问题中所建议的那样,但它似乎没有任何作用。有什么想法吗

附言:我也在想线路在哪里

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

…来看看我的shell输出。当我只运行curl--head-in-terminal命令时,输出中没有任何类似的行。

这里是代码的工作版本,注释了更改的原因

#!/bin/bash

function latest_file_name {
    local url="http://wordpress.org/latest.tar.gz"

    curl -s --head $url | # Add -s to remove progress information
    # This is the proper place to remove the carridge return.
    # There is a program called dos2unix that can be used as well.
    tr -d '\r'          | #dos2unix
    # You can combine the grep and cut as follows
    awk -F '=' '/^Content-Disposition/ {print $2}'
}


function main {
    local file_name=$(latest_file_name)

    # [[ uses bash builtin test functionality and is faster.
    if [[ -e "$file_name" ]]; then
        echo "File $file_name does already exist!"
    else
        echo "There is no file named $file_name in this folder!"
    fi
}

main

new
变量以HTTP发送的“\r\n”行结尾。你需要去掉它,但我不知道如何在
bash
中这样做。谢谢@chepner-我刚刚将第4行更改为:
new=$(curl--head)http://wordpress.org/latest.tar.gz |grep内容配置| cut-d'='-f2 | tr-d'\r\n')
它就像一个符咒+1对于格式良好的问题,清晰的问题描述,样本输出,先前的研究,该死的我希望我能做+4!欢迎继续发帖。谢谢你的回答,接受。看起来效果不错,学习awk和这样更好的编码实践非常有用。伟大的