大家好,經過數小時的谷歌搜索,希望這里有人可以提供指導。
目前我有一個 .sh 檔案,我在 putty 中運行它來啟動一個行程。我目前的做法是:
sh /filepath/file.sh >> /filepath/file_date.log
雖然這有效;我發現確保對我呼叫的每個 .sh 檔案執行正確的 >> 和日志檔案路徑很麻煩。
我的 .sh 檔案的當前內容如下:
#!/bin/bash
set -x
frstdt=`date -d "last monday - 2 week" "%m%d%Y"`;
scnddt=`date -d "last sunday - 1 week" "%m%d%Y"`;
tday=`date -d "$date" "%m_%d_%Y"`;
frstdtem=`date -d "last monday - 2 week" "%m/%d/%Y"`;
scnddtem=`date -d "last sunday - 1 week" "%m/%d/%Y"`
cd /filepath/
/filepath/cloak-hive -f query.hql
--Here I use hive -e to get some results from the query and write them to a .csv file.
ehco "Contents of email" | mailx -s "Subject" -a "Attchement" [email protected]
我想了解的是我可以添加到 .sh 檔案的開頭以記錄發生的所有操作、map/reduce 更新 HIVE 等。我現在可以在使用 >> 命令時看到所有這些呼叫 .sh 但我希望為團隊中的其他人簡化此操作。我試過使用 exec trap exec; | tee 和其他版本,但無法正常作業。輸入 sh /filepath/filename.sh 后,我不需要在控制臺中看到任何內容我只想在日志中顯示所有內容,其中日志檔案名具有它在檔案名中運行的 dat(這就是為什么我有tday 作為變數;我希望它顯示為 /filepath/filename_$tday.log
uj5u.com熱心網友回復:
最好的方法是:
command >> /filepath/logfile.log 2>&1
把它放在你的腳本中,
謝謝
uj5u.com熱心網友回復:
我不建議這種做法,但如果你真的想這樣做,你可以做一個
exec >> /filepath/file_date.log 2>&1
作為腳本中的第一條陳述句。
uj5u.com熱心網友回復:
運行腳本sh是一個錯誤。請參閱和之間的區別shbash
如果我猜對了您要問的問題,我的建議是創建一個簡單的包裝器,例如
#!/bin/sh
/filepath/file.sh >> /filepath/file_$(date %F).log
您的腳本應該有一個有效的shebang并且您需要chmod x兩者都能夠使用該工具而不是拼寫shor bash。然后,您也不需要跟蹤哪個 shell 用于哪個腳本。
我還會稍微重構您的 Bash 腳本。
#!/bin/bash
set -x
# Use modern POSIX syntax for command substitution
tday=$(date -d "$date" "%m_%d_%Y")
frstdtem=$(date -d "last monday - 2 week" "%m/%d/%Y")
scnddtem=$(date -d "last sunday - 1 week" "%m/%d/%Y")
# Don't call the same date commands again; instead, just remove slashes
frstdt=${frstdtem//\//}
scnddt=$(scnddtem//\//}
# Probably avoid cd, but depends
# See https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory
cd /filepath/
/filepath/cloak-hive -f query.hql
--Here I use hive -e to get some results from the query and write them to a .csv file.
# Don't misspell echo or Attachment
echo "Contents of email" | mailx -s "Subject" -a "Attachment" [email protected]
使用最瘋狂的日期格式會在未來造成麻煩;我建議使用 ISO 8601 機器可讀的 YYYY-MM-DD 日期格式,除了可能的人類顯示(但更好的是,訓練你的人類),但我沒有改變那部分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/512836.html
標籤:重击壳蜂巢
上一篇:Perl用變數替換檔案的內容
