linux - Bash 脚本输出到文件从脚本失败但从 bash 工作

标签 linux bash redirect scripting

我有这个脚本来检查 dcos 身份验证登录是否有效,但是我将输出重定向到的文件大小始终为零,当我从 bash shell 运行脚本时,文件大于零。我究竟做错了什么 ?? ,我在下面使用的两个函数:

try_to_login()
{
# first needs to be logged as skyusr
# try to login and log the result to tmp file
# Sometimes the file is empty so we try again to login
# if the second time is OK it jumps to check the output
cd /home/skyusr/scripts/
dcos auth login --username=admin --password=admin > /home/skyusr/scripts/tmp.sal
}

check_login_result()
{
# Checks if the output of the login is "Login Successful!"
# If YES then writes to log file, if not sends mail and writes to log.
#export mail_to="salim.bisharat@amdocs.com,anis.faraj@amdocs.com"
export mail_to="salim.bisharat@amdocs.com"
now=$(date)
text_to_check=$(cat /home/skyusr/scripts/tmp.sal)
if [ -s /home/skyusr/scripts/tmp.sal ]
then
if [ "$text_to_check" = "Login successful!" ]
        then
        echo "$now - Check Successful" >> /home/skyusr/scripts/logs/login_log.log
else
        cat /home/skyusr/scripts/logs/mail_temp.log | mailx -s "!!! CRITITCAL -- Check DCOS login !!!" $mail_to
        echo "$now - !! ERROR ! Sent mail !! " >> /home/skyusr/scripts/logs/login_log.log
        fi
fi
}

最佳答案

在此脚本中您定义,但不调用函数。简单地追加函数调用:

    # ...
    echo "$now - !! ERROR ! Sent mail !! " >> /home/skyusr/scripts/logs/login_log.log 
    fi
fi
} # ... the last line of your script here

try_to_login # calls here ...
check_login_result

关于linux - Bash 脚本输出到文件从脚本失败但从 bash 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907139/

相关文章:

linux - gvim pid 以 $!与 wmctrl 列表不同

c - socket(PF_INET, SOCK_RAW, AF_INET) 使用 IGMP?

linux - linaro-hwpack-create面临的问题

bash - 你如何在 Windows 上的 Bash 中禁用铃声?

linux - BASH:测试字符串是否有效

ruby-on-rails - 重定向 404、422、500 Ruby On rails

linux - 在 bash #2 中排序

linux - 如何使用 awk 比较 CSV 列?

html - 单击/提交到 iframe 后刷新整个页面

java - Liferay portlet(带有 JSF)- 在重定向期间将参数从一个 bean 传递到另一个 bean