LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash functioning with command line or crontab (https://www.linuxquestions.org/questions/linux-software-2/bash-functioning-with-command-line-or-crontab-4175444985/)

GianniC 01-10-2013 07:43 AM

bash functioning with command line or crontab
 
Hello all,

I have a strange problem with the following bash script:

Code:

currentdate=`date +"%Y/%m/%d"`
updateterradate=( $(awk -F, '{print $2}' "terrafinal") )
updateterratime=( $(awk -F, '{print $3}' "terrafinal") )
updateterraorbit=( $(awk -F, '{print $7}' "terrafinal") )
i=0
while [ ${updateterradate[i]} != $currentdate ]
do
        i=$((i+1))
done
inputdatetime=${updateterradate[i]}" "${updateterratime[i]}
orbitrefterra=${updateterraorbit[i]}
timerefterra=`date -d "$inputdatetime" +%s`
echo $orbitrefterra","$timerefterra > /Tasks/NewTmpPropagator/terraref.txt

In that script I set the current date and read some columns of a text file.
Then with the while-do cicle I skip some rows of the vectors read and select and index "i".
For this index I evaluate the UTC time in timerefterra variable.
At last I write orbitrefterra and timerefterra variables in a text file, separated by a ",".

The strange thing is that if I launch the script, it works fine, but if I launch it by means of a crontab, in the output text file the variable orbitrefterra is not written. In other words I find in the file /Tasks/NewTmpPropagator/terraref.txt only the ","$timerefterra part.

May you help me please?

unSpawn 01-10-2013 08:19 AM

In your cronjob temporarily add a separate line "set -vx" or in the shebang line change for example "#!/bin/bash" to "#!/bin/bash -vx". Easiest way to troubleshoot IMO.

Sydney 01-10-2013 08:47 AM

I think I would do something like:
For the user you would like to process this as
su username
crontab -e
then add the entry

# Filter Results by Todays Date Everyday at 2:23 PM.
# Change Made by GianniC on 20130110
14 23 * * * cd /home/GianniC/var && cat terrafinal | awk -F, '{print $7","$2" "$3}' | grep `date +"%Y/%m/%d"` > /Tasks/NewTmpPropagator/terraref.txt 2>&1

Then save the file it will be loaded into the users crontab.
This entry will run at 2:23 PM change directory into /home/GianniC/var (Whatever user you use make sure they have read and write permissions to the directory that you plan to use.) then the command to output the file in the format you would like filtering only for today's date will be run. Purest will tell you that you do not need the cat and they are correct but this is off the top of my head and I cannot think of how to exclude it.

colucix 01-10-2013 08:51 AM

Quote:

Originally Posted by Sydney (Post 4867054)
# Filter Results by Todays Date Everyday at 2:23 PM.
# Change Made by GianniC on 20130110
14 23 * * * cd /home/GianniC/var && cat terrafinal | awk -F, '{print $7","$2" "$3}' | grep `date +"%Y/%m/%d"` > /Tasks/NewTmpPropagator/terraref.txt 2>&1

FYI the % sign has a special meaning in crontab (see man 5 crontab for details). It should be escaped to make it work properly.

David the H. 01-11-2013 06:26 AM

Code:

cd /home/GianniC/var && cat terrafinal | awk -F, '{print $7","$2" "$3}' | grep `date +"%Y/%m/%d"` > /Tasks/NewTmpPropagator/terraref.txt 2>&1
Looks good, except for the Useless Use Of Cat you mentioned. Just add the filename as an argument to awk. grep can be eliminated too, if gawk is available. You can use the built in strftime function instead.

And do we even need to cd to the directory first?

Code:

gawk -F, '($0 ~ strftime("%Y/%m/%d")){print $7","$2" "$3}' /home/GianniC/var/terrafinal > /Tasks/NewTmpPropagator/terraref.txt 2>&1

GianniC 01-14-2013 05:14 AM

Thank U all,

your suggestion give me the chance to do a correct troubleshooting!


All times are GMT -5. The time now is 12:59 PM.