gpt4 book ai didi

linux - UNIX shell 脚本在作为计划任务运行时失败

转载 作者:太空宇宙 更新时间:2023-11-04 10:27:54 27 4
gpt4 key购买 nike

我遇到 1 个小问题,shell 脚本无法执行 IF 语句中列出的命令之一。

如果您打开终端窗口并复制\粘贴整个脚本,脚本将 100% 正常运行。

如果您只是通过调用它来运行脚本,那么一切正常除了您将在最底部看到的这一行:

echo $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

最佳答案

已解决: 问题与 Windows 中的计划任务有关。这是我为使其正常工作所做的工作。

Task Scheduler > Modify the schedule to: Start a Program > Program/Script: cmd Add arguments (optional): /K C:\Scripts\Shoretel_Export\Shoretel-Export.sh & exit


关于linux - UNIX shell 脚本在作为计划任务运行时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40961724/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com