2016-12-04 84 views
0

我遇到1个小问题,如果shell脚本未能执行IF语句中列出的命令之一。作为计划任务运行时,UNIX shell脚本失败

如果您打开一个终端窗口并复制\粘贴整个脚本,该脚本是100%功能。

如果你只是通过调用它运行脚本,一切工作除了这条线,你会在最底层看到:

回声$ NEWID> $ FILEID

环境是运行GitBASH的Windows Server 2012。 (这将解释C:/文件路径)

任何想法为什么脚本无法执行该1任务只有当作为“脚本”运行相比复制\粘贴代码到终端?


这是整个脚本。 谢谢

# Clean directory 
cd C:/Scripts/Shoretel_Export/ 
rm -f C:/Scripts/Shoretel_Export/output/*.csv 

# <---- Define: Date variables (not all dates used but available if needed \ just modify the wget string) ----> # 

twoDaysAgo=$(date +%Y-%m-%d -d '2 days ago') 
yesterday=$(date +%Y-%m-%d -d 'yesterday') 
today=$(date +%Y-%m-%d) 
tomorrow=$(date +%Y-%m-%d -d 'tomorrow') 

# <---- Define: Date timestamp variables for file naming ----> # 
stampTwoDaysAgo=$(date +%m-%d-%Y -d '2 days ago') 
stampTomorrow=$(date +%m-%d-%Y -d 'tomorrow') 

# <---- Define: Credentials variable ----> # 
account="--http-user=APIuser --http-password=APIpassword" 

# <---- Define: Export output directory variable ----> # 
sourceFile="C:/Scripts/Shoretel_Export/output/currentLastID.csv" 
output=$"C:/Scripts/Shoretel_Export/output/"${stampTwoDaysAgo}"_"${stampTomorrow}".csv" 
tempIDsource=$"C:/Scripts/Shoretel_Export/output/archive/"${stampTwoDaysAgo}"_"${stampTomorrow}".csv" 

# <---- Define: Export lastID variable ----> # 
callID=$(awk -F'","|^"|"$' 'BEGIN { max=0 } $1 > max { max=$1 } END {print max}' "C:/Scripts/Shoretel_Export/output/ID/ID.txt") 

# <---- Execute WGET function to export Shoretel calls from the specified date range ----> # 
wget --auth-no-challenge --no-check-certificate --output-document=${output} "https://portal.shoretelsky.com/DataExport/CDRData?startDate=${twoDaysAgo}&endDate=${tomorrow}&lastId=${callID}" ${account} 

# Perform empty file validation, if file is 0 kb the script will terminate and retry 
if [ -s "$output" ] 
then 
    echo "File is not empty continue to next IF statement" 
else 
    ./retry.sh 
fi 


#### At this point we should have a valid file 1 kb or larger. If the export contains 0 new records the script will terminate and the previous callID will remain #### 

# Define tempID to check for invalid or empty ID field 
tempID=$(awk -F'","|^"|"$' 'BEGIN { max=0 } $18 > max { max=$18 } END {print max}' $output) 

# Define ID paramaters and rename files 
fileID="C:/Scripts/Shoretel_Export/output/ID/ID.txt" 

if [ "$tempID" -gt "$callID" ] 
then 
    cp $output C:/Scripts/Shoretel_Export/output/archive/ 
    mv $output $sourceFile 
    newID=$(awk -F'","|^"|"$' 'BEGIN { max=0 } $18 > max { max=$18 } END {print max}' $tempIDsource) 
    echo $newID > $fileID 
    echo "Export and Timestamp complete" 
else 
    echo "Export contains 0 new call records \ the script will now terminate" 
fi 
+0

确保它运行与使用,当你在控制台中运行它,你使用相同的用户计划任务。或者确保'System'用户(或任何用户运行计划任务;我手边没有Windows系统)具有写入文件所需的权限。 – axiac

+0

我知道它必须是Windows任务的一些问题。我会更新这个问题,以显示我是如何工作的。感谢您的输入! – CWB

回答

0

不知道它在Windows要去工作,但你应该尝试在调试模式下执行该脚本:

#!/bin/bash -x 

所以,你可以检查线是什么发生线。 你也可以显示这个脚本的输出吗?

+0

脚本的输出是一个呼叫日志.csv文件。没什么特别的,但它包含我认为是保密的信息,所以我不能分享它。 我试过了。如果您已打开终端窗口并运行“bash -x ./Export.sh”,则脚本将无任何问题地完成。 只有当脚本运行无头时,它才不能通过ECHO>命令执行那个1.txt文件更新。 – CWB

1

已解决:该问题与Windows中的计划任务有关。以下是我做的工作。

任务计划>修改时间表:启动程序>程序/脚本: CMD添加参数(可选):/K C:\脚本\ Shoretel_Export \ Shoretel-Export.sh &出口


+0

如果问题如此,您应该将问题标记为已回答:-) – andlrc

相关问题