代码之家  ›  专栏  ›  技术社区  ›  nsx

Bash输出消失

  •  0
  • nsx  · 技术社区  · 8 年前

    运行以下bash脚本时。。。

    #!/bin/bash
    
    ATTEMPTS=3
    DEST_DIR="/opt/satnet"
    ATTEMPTS=3
    URL="https://github.com/google/snappy/tarball/master"
    
    CURL_OPTS='-sIkL'
    SED_REGEX='/filename=/!d;s/.*filename=(.*)$/\1/'
    FILENAME="$( curl $CURL_OPTS $URL | sed -r $SED_REGEX )"
    DEST_FILE="$DEST_DIR/$FILENAME"
    
    printf "\t* URL = %s\n" $URL
    printf "\t* Remote tarball filename = %s\n" $FILENAME
    
    
    WGET_BIN='wget'
    WGET_OPTS_1="-t $ATTEMPTS -c -O $DEST_FILE"
    WGET_OPTS="-t $ATTEMPTS -c -O $DEST_FILE $URL"
    
    printf "\t* wget opts 1 = %s\n" "$WGET_OPTS_1"
    printf "\t* wget opts = %s\n" "$WGET_OPTS"
    
    echo "    * wget opts 1 = $WGET_OPTS_1"
    echo "    * wget opts = $WGET_OPTS"
    

    ... 第二个变量的输出应包括第一个变量的输出加上URL;但是,它只显示URL,甚至不显示URL之前的字符串部分:

    * URL = https://github.com/google/snappy/tarball/master
    * Remote tarball filename = google-snappy-1.1.7-0-gb02bfa7.tar.gz
    * wget opts 1 = -t 3 -c -O /opt/satnet/google-snappy-1.1.7-0-gb02bfa7.tar.gz
    https://github.com/google/snappy/tarball/mastergle-snappy-1.1.7-0-gb02bfa7.tar.gz
    * wget opts 1 = -t 3 -c -O /opt/satnet/google-snappy-1.1.7-0-gb02bfa7.tar.gz
    https://github.com/google/snappy/tarball/mastersnappy-1.1.7-0-gb02bfa7.tar.gz
    

    这就像,出于某种原因,添加URL“删除”了字符串的初始部分。

    我做错了什么?

    1 回复  |  直到 8 年前
        1
  •  1
  •   janos slartidan    8 年前

    老实说,我无法重现您问题中的奇怪显示错误。我在Linux shell中运行了相同的代码,它显示得很好。 但我可以确认 curl 在发布的代码输出结果中请求DOS行结束, 最终出现在 FILENAME 变量 这反过来可能会导致您所经历的奇怪输出。

    消除冒犯的简单方法 \r 要使用的字符 tr :

    FILENAME="$( curl $CURL_OPTS $URL | sed -r $SED_REGEX | tr -d '\r' )"