bash脚本不propperly运行 [英] bash script not running propperly

查看:134
本文介绍了bash脚本不propperly运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过它在它似乎很好地工作在命令行自运行它,但是当我有另一个脚本执行此,这是行不通的。有任何想法吗?我猜它有引号的事,但不知道。

 #!/ bin / sh的
从https://github.com/dbro/c​​svquote #REQUIRED csvquote
#TODO:使用CSVFix清洁CSV文件
3的#Version
回声文件名:$ 1
函数退出{
    回声退出脚本
    1号出口
}
功能FILEEXISTS {
    如果[! -f$ 1]
        然后
            echo文件$ 1不存在
            放弃
        科幻
}
功能的getInfo {
    #Returns网站的网址,如:http://www.website.com/info
    #Reads csv文件的最后一行,并获得第二个项目。
    RETURN = $(尾-n 1 $ 1 | csvquote |切-d','-f 2 | csvquote -u)
    回声$ RETURN
}
功能工作{
    CURLURL =htt​​p://127.0.0.1:9200/cj/_query
    URL = $(的getInfo)
    回声网址:$网址
    CURLDATA ='{查询:{匹配:{PROGRAMURL:'$ URL'}}}
    #URL显示为空白......?
    回声卷曲数据:$ CURLDATA
    RESPONSE = $(卷曲-XDELETE$ CURLURL-d$ CURLDATA-vn)
    回声$响应
    回声睡觉时间让删除
    睡眠5秒
}FILEEXISTS $ 1
工作$ 1


解决方案

我不能明白为什么一个简单的版本不会工作:功能是有用的,但我觉得有太多,过于复杂的事情,如果你所发布的全部你的脚本(在我看来)

您的脚本是干什么用破吉祥图案的东西: $ 1 变量也是争论壳功能以及主脚本。把它们当作局部变量的函数。所以,当你调用 $(的getInfo)它调用不带参数的功能,所以实际运行尾-n 1 这回退到标准输入,为其指定到工作< $ 1 。你可以通过把看到自己回声getInfo_arg_1 =$ 1>和2 在函数内部...

请注意,你也没有引用 $ 1 的任何地方,这个脚本是不是在文件安全空白,尽管这只是更可能是,如果您有要处理的一个问题从Windows计算机发送到您的文件。

在没有其他信息,下面的应当的工作:

#!/斌/庆典测试-z$ 1&安培;&安培; {echo请指定的文件。 ; 1号出口; }
测试-f$ 1|| {回声看不到文件'$ 1'。 ; 1号出口; }FILE =$ 1起作用的getInfo(){
    #Returns网站的网址,如:http://www.website.com/info
    #Reads csv文件的最后一行,并获得第二个项目。
    尾-n 1$ 1| csvquote |切-d','-f 2 | csvquote -u
}
CURLURL =htt​​p://127.0.0.1:9200/cj/_query
URL = $(的getInfo$ FILE)
回声网址:$网址
CURLDATA ='{查询:{匹配:{PROGRAMURL:'$ URL'}}}
卷曲-XDELETE$ CURLURL-d$ CURLDATA-vn
回声睡觉时间让删除
睡眠5秒

如果仍然失败,你真的需要张贴您的错误消息。

还有一件事,特别是如果你是从另外一个脚本调用此,使用chmod + X 脚本,所以你可以在不使用<$ C $调用它运行C>庆典直接。如果你想打开调试然后把设置-x 附近的某处开始

When I run this by its self in the command line it seems to work fine, but when I have another script execute this, it doesn't work. Any ideas? I'm guessing it has to do with quotes, but not sure.

#!/bin/sh
#Required csvquote from https://github.com/dbro/csvquote
#TODO:  Clean CSV File using CSVFix
#Version 3
echo "File Name: $1"
function quit {
    echo "Quitting Script"
    exit 1
}
function fileExists {
    if [ ! -f "$1" ]
        then
            echo "File $1 does not exists"
            quit
        fi
}
function getInfo {
    #Returns website url like:   "http://www.website.com/info"
    #Reads last line of a csv file, and gets the 2nd item.
    RETURN=$(tail -n 1 $1 | csvquote | cut -d ',' -f 2 | csvquote -u)
    echo $RETURN 
}
function work {
    CURLURL="http://127.0.0.1:9200/cj/_query"
    URL=$(getInfo)
    echo "URL: $URL"
    CURLDATA='{ "query" : { "match" : { "PROGRAMURL" : '$URL' } } }'
    #URL shows up as blank...???
    echo "Curl Data: $CURLDATA"
    RESPONSE=$(curl -XDELETE "$CURLURL"  -d "$CURLDATA" -vn)
    echo $RESPONSE
    echo "Sleeping Allowing Time To Delete"
    sleep 5s
} 

fileExists $1
work $1

解决方案

I cant see why a simpler version wont work: functions are useful, but I think there are too many, overcomplicating things, if what you are posting is the entirety of your script (in my opinion)

Your script is doing things using a broken lucky pattern: $1 variables are also arguments to shell functions as well as the main script. Think of them as local variables to a function. So when you are calling $(getInfo) it is calling that function with no argument, so actually runs tail -n 1 which falls back to stdin, which you are specifying to work as < $1. You could see this for yourself by putting echo getInfo_arg_1="$1" >&2 inside the function...

Note also you are not quoting $1 anywhere, this script is not whitespace in file safe, although this is only more likely to be a problem if you are having to deal with files sent to you from a Windows computer.

In the absence of other information, the following 'should' work:

#!/bin/bash

test -z "$1" && { echo "Please specify a file." ; exit 1; }
test -f "$1" || { echo "Cant see file '$1'." ; exit 1; }

FILE="$1"

function getInfo() {
    #Returns website url like:   "http://www.website.com/info"
    #Reads last line of a csv file, and gets the 2nd item.
    tail -n 1 "$1" | csvquote | cut -d ',' -f 2 | csvquote -u
}


CURLURL="http://127.0.0.1:9200/cj/_query"
URL=$(getInfo "$FILE")
echo "URL: $URL"
CURLDATA='{ "query" : { "match" : { "PROGRAMURL" : '$URL' } } }'
curl -XDELETE "$CURLURL"  -d "$CURLDATA" -vn
echo "Sleeping Allowing Time To Delete"
sleep 5s

If it still fails you really need to post your error messages.

One other thing, especially if you are calling this from another script, chmod +x the script so you can run it without having to invoke it with bash directly. If you want to turn on debugging then put set -x near the start somewhere.

这篇关于bash脚本不propperly运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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