Bash 错误报告

发布于 2024-12-21 05:47:22 字数 1625 浏览 0 评论 0原文

好的,我可以让它运行并将文件放在远程服务器上。但是,当它运行时,我希望它回显“传输失败”、“传输正常”、“文件中的零字节”和“没有这样的文件或目录”。它只是运行代码并发送最后一个条目的电子邮件,当明确文件传输到服务器时,该条目不是这样的文件或目录。我做错了什么? 提前谢谢你。

#!/bin/sh
HOST=10.10.1.2
USER='test'
RECIP="[email protected]"
cd /home/test
FILE=ARG$(date '+%Y%m%d').txt
BYTES=`stat -c%s $FILE`
CONNECTION=`netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' |   sort | uniq -c | awk '{printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++)    {printf("*")}; print ""}'`
RUN_AS=`whoami`
RESULT=`sftp $USER@$HOST <<EOF
FILE=ARG$(date '+%Y%m%d').txt
cd /998979/DES
if [ -e $FILE ]; then
put $FILE
EOF`

echo $CONNECTION "Connection to network is established."

ls | xargs wc -wl for file in *; echo $FILE done  
if [ $? -eq 0 ]; then
echo "$RESULT" "Transfer of file failed. `date`"
SBJ="Transfer of file failed `date`"
fi
echo "$RESULT" "Transfer OK"
if [ $? -eq 0 ]; then
SBJ="Transfer OK. `date`"
echo "$RESULT" "Transfer OK"
fi
if \[ ! -s ${FILE} \]; then
echo "File: ${FILE} is not present or a ZERO byte file"
SBJ="Failed to upload. Zero bytes in file. `date`" # zero bytes sent
fi
if [ -a $FILE ]; then
echo "$RESULT" "No such file or directory"
SBJ="No such file or directory. `date`"
fi

BODY="Process Report....... 

SRC SERVER:`hostname` 
DST SEVER: TESTSERVER
SCRIPT: /usr/local/bin/put.sh
RUN AS: "$RUN_AS" 

RESULT: "$RESULT" "$FILE"

CONNECTION: "$CONNECTION" 

FILENAME: "$FILE" 

BYTES: "$BYTES" 
DATE/TIME: `date`"

echo "$BODY" | mail -s "$SBJ" "$RECIP"

Ok, I can get this to run and put the file on the remote server. However when it does run, I want it to echo a Transfer failed, Transfer OK, Zero bytes in file and No such file or directory. It just runs down the code and sends an email of the last entry which is no such file or directory when clearly the file transferred to the server. What am I doing wrong ??
Thx in advance, I thank you.

#!/bin/sh
HOST=10.10.1.2
USER='test'
RECIP="[email protected]"
cd /home/test
FILE=ARG$(date '+%Y%m%d').txt
BYTES=`stat -c%s $FILE`
CONNECTION=`netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' |   sort | uniq -c | awk '{printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++)    {printf("*")}; print ""}'`
RUN_AS=`whoami`
RESULT=`sftp $USER@$HOST <<EOF
FILE=ARG$(date '+%Y%m%d').txt
cd /998979/DES
if [ -e $FILE ]; then
put $FILE
EOF`

echo $CONNECTION "Connection to network is established."

ls | xargs wc -wl for file in *; echo $FILE done  
if [ $? -eq 0 ]; then
echo "$RESULT" "Transfer of file failed. `date`"
SBJ="Transfer of file failed `date`"
fi
echo "$RESULT" "Transfer OK"
if [ $? -eq 0 ]; then
SBJ="Transfer OK. `date`"
echo "$RESULT" "Transfer OK"
fi
if \[ ! -s ${FILE} \]; then
echo "File: ${FILE} is not present or a ZERO byte file"
SBJ="Failed to upload. Zero bytes in file. `date`" # zero bytes sent
fi
if [ -a $FILE ]; then
echo "$RESULT" "No such file or directory"
SBJ="No such file or directory. `date`"
fi

BODY="Process Report....... 

SRC SERVER:`hostname` 
DST SEVER: TESTSERVER
SCRIPT: /usr/local/bin/put.sh
RUN AS: "$RUN_AS" 

RESULT: "$RESULT" "$FILE"

CONNECTION: "$CONNECTION" 

FILENAME: "$FILE" 

BYTES: "$BYTES" 
DATE/TIME: `date`"

echo "$BODY" | mail -s "$SBJ" "$RECIP"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-12-28 05:47:22

您可以使用 /bin/sh -xv yourshellscript.sh 来了解发生了什么。

您可以使用 logger 将重要消息记录到系统日志中

You can use /bin/sh -xv yourshellscript.sh to understand what is happening.

And you could log important messages to the system log using logger

童话 2024-12-28 05:47:22
count=$?

if [ $count -eq 0 ]; then 
 echo "$RESULT" "Transfer of file failed. 
 `date`" SBJ="Transfer of file failed `date`" 
else 
 echo "$RESULT" "Transfer OK" 
fi

将结果分配给一个变量,另外,当不需要做更多处理时,尝试使用 exit。

count=$?

if [ $count -eq 0 ]; then 
 echo "$RESULT" "Transfer of file failed. 
 `date`" SBJ="Transfer of file failed `date`" 
else 
 echo "$RESULT" "Transfer OK" 
fi

Assign the result to a variable, Also, try to use exit when you don't need to do more processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文