0

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
7
  • 1
    expand on "doesn't work". have you tried tracing it with bash -x Commented Mar 31, 2014 at 10:23
  • Why are you putting single quotes around $URL? (variables don't expand in single quotes in bash). Also if you are intending to use bash I would generally use a bash shebang, #!/bin/sh is dash on a number of systems (won't necessarily cause errors, but could for some bash specific cases). Commented Mar 31, 2014 at 10:38
  • @BroSlow Take a close look, $URL is actually outside the quotes - try it for yourself in a shell ... Commented Mar 31, 2014 at 11:08
  • Note: on the likely assumption you are not 'sourcing' this script, there is no point setting IFS in quit. Commented Mar 31, 2014 at 11:11
  • You seem to be going a little over the top with variables etc. - there is no need to have getInfo save its results in a variable then echoing it, that is redundant Commented Mar 31, 2014 at 11:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

There are no "error" messages really, It's not setting the $URL variable. So the curl request is: { "query" : { "match" : { "PROGRAMURL" : } } }
As far as I an tell it's not running tail. I edited the script above to just output tail -n 1 "$1" and no output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.