BASH shell脚本回声在同一线路输出 [英] BASH shell script echo to output on same line

查看:144
本文介绍了BASH shell脚本回声在同一线路输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的bash shell脚本检查一个curl命令的HTTP响应code。
其中的逻辑是很好,但我坚持的简单地打印出产出。

我使用GNU的bash,版本3.2.25(1) - 释放下(x86_64-红帽Linux的GNU的)

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

  HTTP://www.google.co.uk<标签&gt; 200

我也得到一个奇怪的错误,其中一个URL的HTTP部分正在与覆盖200 | 404 | 501 | 502。有没有这我不使用基本的BASH shell脚本(功能)?

感谢

万里。

 #!/斌/庆典名字=`$猫1`
因为我在$名字
做    URL = $ I
    状态code =`卷曲-s -I -L $ I | grep的HTTP| AWK'{打印$ 2}'`    案例$ STATUS code在
    200)
        回声-ne $ URL \\ T $状态code ;;
    301)
        回声-ne\\ T $状态code;;
    302)
        回声-ne\\ T $状态code;;
    404)
        回声-ne\\ T $状态code;;
    ESAC
DONE


解决方案

从<一个href=\"http://stackoverflow.com/questions/2220301/how-to-evaluate-http-response-$c$cs-from-bash-shell-script\">this答案 可以用code

响应= $(卷曲--write出%{HTTP_ code} --silent --output的/ dev / null的服务器名)


代入你的循环,这将是

 #!/斌/庆典名字=`$猫1`
因为我在$名字
做    URL = $ I
    状态code = $(卷曲--write出%{HTTP_ code} --silent --output的/ dev / null的$ I)    案例$ STATUS code在
        200)
            回声-e$ URL \\ T $状态code;;
        301)
            回声-e$ URL \\ T $状态code;;
        302)
            回声-e$ URL \\ T $状态code;;
        404)
            回声-e$ URL \\ T $状态code;;
        *)
            ;;
    ESAC
DONE

我已经清理了echo语句也让每个URL有一个新的生产线。

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".

I am using GNU bash, version 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:

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?

thanks

Miles.

#!/bin/bash

NAMES=`cat $1`
for i in $NAMES
do

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

    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

解决方案

From this answer you can use the code

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


Substituted into your loop this would be

#!/bin/bash

NAMES=`cat $1`
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.

这篇关于BASH shell脚本回声在同一线路输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆