2016-12-04 9 views
0

IFスクリプトに記載されているコマンドの1つを実行できないシェルスクリプトで1つの小さな問題が発生しました。スケジュールされたタスクとして実行したときにUNIXシェルスクリプトが失敗する

ターミナルウィンドウを開いてスクリプト全体をコピーして貼り付けた場合、スクリプトは100%機能です。

あなたはそれを呼び出すことで、スクリプトを実行すると、すべてが動作しますあなたが一番下に表示されます。この行を除く

はエコー$ NEWID> $ファイルID

環境はGitBASHを実行するWindows Server 2012です。

なぜスクリプトが "スクリプト"として実行されたときだけ、スクリプトを実行してそのコードを端末にコピー&ペーストするのですか?


ここにスクリプト全体があります。 はありがとう

# 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

関連する問題