Bash 脚本来测试它是否是本月的第一个星期一

the*_*btm 1 linux shell-script date

我有一个 bash 脚本,它接收一些文件并将它们设置为 FTP 到一个处理安装文件之一的站点。我们希望在本月的第一个星期一找到另一个文件,但我不确定如何将其放入 bash 脚本中。我见过使用 crontab 的东西,但脚本的第一部分和最后一部分完全相同,如果我们有 2 个不同的脚本,可能会导致问题。

只放入我正在考虑进行更改的脚本的一部分。

#!/bin/bash
...

e_file="/tmp/tmpemail.$(date +%s).txt"
file1='/usr/local/filename1'
file2='/usr/local/filename2'
relayserver='relay-server.example.com'

#ftp info
FTP_USER='ftpuser' #not the actual FTP User Name
FTP_DEST_PATH='/'

...

echo -e "Starting Tunnel and SFTP Process"
# make ssh tunnel for access to SFTP Site
ssh -L 9022:ftp.example.com:22 serviceaccount@$relay_server -Nf >/dev/null 2&>1
proc=`ps -ef | grep "ssh -L 9022\:ftp.example.com\:22" | awk '{print $2}'`

#checks to see if the tunnel opened correctly then proceeds to push to FTP Site
if [ "${proc}" != "" ]; then

    #looking for first monday, was thinking of first day but the crontab only runs on monday to friday
    ifStart=`date '+%d'`
    if [ $ifStart == 01 ]; then 

        echo -e "File 1 & File2 sent to FTP Site" >> $e_file
            $SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
        cd $FTP_DEST_PATH
        put $file1
        put $file2
        bye
END

    else

        echo -e "file 2 sent to FTP" >> $e_file
            $SFTP_CMD -oPort=9022 -b /dev/stdin $FTP_USER@localhost << END
        cd $FTP_DEST_PATH
        put $file2
        bye
END

    fi

    echo "killing ssh tunnel - $proc"
    kill $proc

else

...
Run Code Online (Sandbox Code Playgroud)

我希望得到正确的方向,以便在我必须发表评论的月份的第一个星期一获得 if 语句。有什么想法可以解决这个问题吗?

添加注意:此​​脚本必须在每月的每个工作日运行以上传要处理的文件。

Rom*_*nov 12

我没有时间阅读所有脚本,但这里有一个想法:使用date命令获取星期几的名称:

we=$(LC_TIME=C date +%A)
Run Code Online (Sandbox Code Playgroud)

LC_TIME=C用于获取星期几的英文名称)

然后在一个月中得到一天

dm=$(date +%d)
Run Code Online (Sandbox Code Playgroud)

然后检查当天是否小于 8 并且一周中的某天是否为星期一:

if [ "$we" = "Monday" ] && [ "$dm" -lt 8 ]
then 
.....
fi
Run Code Online (Sandbox Code Playgroud)

  • 您只需要使用进程替换调用 date 一次:`read we dm &lt; &lt;(date "+%A %d")` (3认同)