简体   繁体   English

ECHO 在“真”条件下不工作

[英]ECHO not working within a "TRUE" if condition

Being a relative beginner, I can't figure this out.作为一个相对初学者,我无法弄清楚这一点。 I have a script that is started via cron.我有一个通过 cron 启动的脚本。 Within this script is an if/fi where I check to see if a (yearly archive) directory does not exist.在此脚本中有一个 if/fi,我在其中检查(年度存档)目录是否不存在。 It it does not, I create the directory, and ATTEMPT to echo that to the cron's log file that is created for each run.它没有,我创建目录,并尝试将其回显到为每次运行创建的 cron 日志文件。 The directory is created, but the echo does not appear in the log file.目录已创建,但回显未出现在日志文件中。

Here is a snippet of the code in question.这是相关代码的片段。

035: yyyy=`date +%Y`
036: today=`date +%m/%d/%Y`
037: time=`date +%r` #+%l:%M:%S%P`
038: dayofweek=`date +%A`
039: numDayOfWeek=`date +%u`
040:
041: echo "Run Date/Time: $today $time"
042:
043: WFADIR="/data/ssa1/home1/NEI/GAP-EFT-FLAT/$yyyy"
044: if [ ! -d $WFADIR ] ; then
045:    mkdir /data/ssa1/home1/NEI/GAP-EFT-FLAT/$yyyy
046:    chmod 777 /data/ssa1/home1/NEI/GAP-EFT-FLAT/$yyyy
047:    echo ""
048:    echo "New folder $yyyy created in GAP-EFT-FLAT"
049: fi
050:
051: #display test variables for output
052: echo ""
053: echo "HOSTNAME..........: ${HOSTNAME^^}"
054: echo ""
055:

And here is the FULL log file.这是完整的日志文件。

Run Date/Time: 01/03/2023 08:00:01 AM

HOSTNAME..........: BASYSPROD

EFT contribution file found...
Calling expect script to transmit contribution file...
spawn sftp -P 22 -i privatekey.pem username@domain.com:/inbound/NATIO080_ACH_3
Connected to domain.com.
Changing to: /inbound/NATIO080_ACH_3
sftp> put B06737_CON_20230103
Uploading B06737_CON_20230103 to /inbound/NATIO080_ACH_3/B06737_CON_20230103

B06737_CON_20230103                             0%    0     0.0KB/s   --:-- ETA
B06737_CON_20230103                           100% 2470    70.0KB/s   00:00    
sftp> Returned from contribution expect script...
Archiving sent contribution file...
Sending email confirmation...
Process completed...
EFT 401K file found...
Calling expect script to transmit 401K file...
spawn sftp -P 22 -i privatekey.pem username@domain.com:/inbound/NATIO080_ACH_4
Connected to domain.com.
Changing to: /inbound/NATIO080_ACH_4
sftp> put B06736_401K_20230103
Uploading B06736_401K_20230103 to /inbound/NATIO080_ACH_4/B06736_401K_20230103

B06736_401K_20230103                            0%    0     0.0KB/s   --:-- ETA
B06736_401K_20230103                          100% 7980   216.4KB/s   00:00    
sftp> Returned from 401K expect script...
Archiving sent 401K file...
Sending email confirmation...

As you can see, the echo from line 41 is in the log file.如您所见,第 41 行的回显在日志文件中。 Then, as this was the first run for 2023, the 2023 directory did not yet exist.然后,由于这是 2023 年的第一次运行,因此 2023 目录尚不存在。 It WAS created and the permissions were changed as well, with lines 45 and 46, respectively.它已创建,权限也已更改,分别在第 45 和 46 行。

drwxrwxrwx. drwxrwxrwx。 2 neiauto staff 61 Jan 3 08:00 2023 2 neiauto 工作人员 61 Jan 3 08:00 2023

So why do lines 47 and 48 appear not to execute, and the next echo in the log file is from line 52, 53 and 54, with the hostname display, surrounded by blank lines?那么为什么第 47 和 48 行看起来没有执行,而日志文件中的下一个回显来自第 52、53 和 54 行,显示主机名,周围是空行?

I was expecting a blank line, and "New folder 2023 created in GAP-EFT-FLAT" to be echoed after the Run date/time (first) line of the log file, and before the host name display.我期待一个空行,并且在日志文件的运行日期/时间(第一)行之后和主机名显示之前回显“New folder 2023 created in GAP-EFT-FLAT”。

Very likely your directory already existed.您的目录很可能已经存在。 Add an else echo $WFADIR already exists to your code to have your answer next year:-).添加一个else echo $WFADIR already exists to your code 以获得明年的答案:-)。 My guess would be that the same code was run twice (on the same, or another host if shared disk-space was used).我的猜测是相同的代码运行了两次(如果使用共享磁盘空间,则在同一台主机或另一台主机上运行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM