I'm using incron to check if XML files are uploaded in a folder Depot using :
/home/parallels/Desktop/Depot IN_CLOSE_WRITE /home/parallels/Desktop/Depot/add.sh $#
When a new file is detected, the following bash script add.sh (which is in the Depot folder) is called :
#!/bin/bash
fileContent="$(cat $1)"
curl \
--header "Content-type: application/json" \
--request POST \
--data '{
"user": "user@mail.com",
"fileName": "'"$1"'",
"content": "'"$fileContent"'"
}' \
http://url/of/the/api
The $1
var contains the name of the file, and the $fileContent
var contains the XML content of the file.
The thing is, when I manually call the bash script from the command line (when there's already a XML file in the Depot folder) using :
./add.sh test.xml
everything works fine. But when I try to trigger the script by dropping a new XML file into the folder, it doesn't do anything, despite showing-up in the log, tail /var/log/syslog
command giving the following result :
(parallels) CMD (/home/parallels/Desktop/Depot/add.sh test.xml)
The only way I could get it working (by dropping a file in the folder) is when the content var is already set in the script, like so :
#!/bin/bash
fileContent="$(cat $1)" <=== Not used anymore in this case
curl \
--header "Content-type: application/json" \
--request POST \
--data '{
"user": "user@mail.com",
"fileName": "'"$1"'",
"content": "
<node>
<subnode>
content
</subnode>
</node>
"
}' \
http://url/of/the/api
Am I missing something here ? Should I use another method than cat
to retrieve the XML content ?
I managed to make it work !
I changed the incron command to :
/home/parallels/Desktop/Depot IN_CLOSE_WRITE /home/parallels/Desktop/Depot/add.sh #@/$#
and the bash script to :
#!/bin/bash
fileContent=`cat $1`
fileName=$(basename $1)
curl \
--header "Content-type: application/json" \
--request POST \
--data '
{
"user": "user@mail.com",
"filename": "'"$fileName"'",
"content": "'"$fileContent"'"
}' \
http://url/of/the/api
I needed to send the full path of the file to the bash script (using $@/$#) for the cat function to work properly.