BASH shell 脚本回显到同一行输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8228807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:13:45  来源:igfitidea点击:

BASH shell script echo to output on same line

bashcurlredhat

提问by chocksaway

I have a simple BASH shell script which checks the HTTP response code of a curl command. The logic is fine, but I am stuck on "simply" printing out the "output".

我有一个简单的 BASH shell 脚本,用于检查 curl 命令的 HTTP 响应代码。逻辑很好,但我坚持“简单”打印“输出”。

I am using GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)

我正在使用 GNU bash,版本 3.2.25(1)-release (x86_64-redhat-linux-gnu)

I would like to output the URL with a tab - then the 404|200|501|502 response. For example:

我想输出带有选项卡的 URL - 然后是 404|200|501|502 响应。例如:

http://www.google.co.uk<tab>200

I am also getting a strange error where the "http" part of a URL is being overwritten with the 200|404|501|502. Is there a basic BASH shell scripting (feature) which I am not using?

我还收到一个奇怪的错误,其中 URL 的“http”部分被 200|404|501|502 覆盖。是否有我没有使用的基本 BASH shell 脚本(功能)?

thanks

谢谢

Miles.

英里。

#!/bin/bash

NAMES=`cat `
for i in $NAMES
do

    URL=$i
    statuscode=`curl -s -I -L $i |grep 'HTTP' | awk '{print }'`

    case $statuscode in
    200)
        echo -ne $URL\t$statuscode;;
    301)
        echo -ne "\t $statuscode";;
    302)
        echo -ne "\t $statuscode";;
    404)
        echo -ne "\t $statuscode";;
    esac
done

回答by Lewis Norton

From this answeryou can use the code

这个答案你可以使用代码

response=$(curl --write-out %{http_code} --silent --output /dev/null servername)

response=$(curl --write-out %{http_code} --silent --output /dev/null servername)



Substituted into your loop this would be

代入你的循环,这将是

#!/bin/bash

NAMES=`cat `
for i in $NAMES
do

    URL=$i
    statuscode=$(curl --write-out %{http_code} --silent --output /dev/null $i)

    case $statuscode in
        200)
            echo -e "$URL\t$statuscode" ;;
        301)
            echo -e "$URL\t$statuscode" ;;
        302)
            echo -e "$URL\t$statuscode" ;;
        404)
            echo -e "$URL\t$statuscode" ;;
        * )
            ;;
    esac
done

I've cleaned up the echo statements too so for each URL there is a new line.

我也清理了 echo 语句,因此每个 URL 都有一个新行。

回答by Raihan

try

尝试

 200)
    echo -ne "$URL\t$statuscode" ;; 

回答by Shawn Chin

I'm taking a stab here, but I think what's confusing you is the fact that curlis sometimes returning more than one header info (hence more than one status code) when the initial request gets redirected.

我在这里尝试了一下,但我认为让您感到困惑的是,curl当初始请求被重定向时,有时会返回多个标头信息(因此多个状态代码)。

For example:

例如:

[me@hoe]$ curl -sIL www.google.com | awk '/HTTP/{print }'
302
200

When you're printing that in a loop, it would appear that the second status code has become part of the next URL.

当您在循环中打印它时,似乎第二个状态代码已成为下一个 URL 的一部分。

If this is indeed your problem, then there are several ways to solve this depending on what you're trying to achieve.

如果这确实是您的问题,那么根据您要实现的目标,有多种方法可以解决此问题。

  1. If you don't want to follow redirections, simple leave out the -Loption in curl

    statuscode=$(curl -sI $i | awk '/HTTP/{print }')
    
  2. To take only the last status code, simply pipe the whole command to tail -n1to take only the last one.

    statuscode=$(curl -sI $i | awk '/HTTP/{print }' | tail -n1)
    
  3. To show all codes in the order, replace all linebreaks with spaces

    statuscode=$(curl -sI $i | awk '/HTTP/{print }' | tr "\n" " ")
    
  1. 如果您不想遵循重定向,只需-Lcurl

    statuscode=$(curl -sI $i | awk '/HTTP/{print }')
    
  2. 要仅获取最后一个状态代码,只需将整个命令通过管道传输tail -n1到仅获取最后一个。

    statuscode=$(curl -sI $i | awk '/HTTP/{print }' | tail -n1)
    
  3. 要按顺序显示所有代码,请将所有换行符替换为空格

    statuscode=$(curl -sI $i | awk '/HTTP/{print }' | tr "\n" " ")
    

For example, using the 3rd scenario:

例如,使用第三个场景:

[me@home]$ cat script.sh
#!/bin/bash
for URL in www.stackoverflow.com stackoverflow.com stackoverflow.com/xxx 
do
     statuscode=$(curl -siL $i | awk '/^HTTP/{print }' | tr '\n' ' ')
     echo -e "${URL}\t${statuscode}"
done

[me@home]$ ./script.sh
www.stackoverflow.com   301 200 
stackoverflow.com       200 
stackoverflow.com/xxx   404