printf "%-10s %-60s %-30s %-30s\n" "$timestamp" "$subject" "$sender" "$recipient" >> $result_file > $result_log
时间: 2023-08-07 08:05:29 浏览: 50
根据您提供的代码,您想将对齐的结果追加到 `$result_file` 中,并将输出重定向到 `$result_log`。然而,`printf` 命令无法直接将输出同时追加到文件和重定向到另一个文件。您可以使用以下修正后的代码来实现您的需求:
```shell
printf "%-10s %-60s %-30s %-30s\n" "$timestamp" "$subject" "$sender" "$recipient" >> "$result_file" 2>&1 | tee "$result_log"
```
在这个修正后的代码中,我使用了 `tee` 命令,它可以将 `printf` 的输出同时追加到文件和输出到终端。使用 `2>&1` 将标准错误重定向到标准输出,这样可以确保错误信息也被写入到 `$result_log` 中。
请确保将 `$result_file` 和 `$result_log` 替换为实际的文件路径。如果还有其他问题,请随时告诉我。
相关问题
for ((i=1; i<=$count; i++)); do timestamp=$(sed -n "${i}p" <<< "$timestamps") subject=$(sed -n "${i}p" <<< "$subjects") sender=$(sed -n "${i}p" <<< "$senders") recipient=$(sed -n "${i}p" <<< "$recipients") if [[ "$timestamp" > "$time" && "$timestamp" < "$(date +"%Y-%m-%d %H:%M:%S")" ]]; then printf "%-10s %-60s %-30s %-30s\n" "$timestamp" "$subject" "$sender" "$recipient" printf "%-10s %-60s %-30s %-30s\n" "$timestamp" "$subject" "$sender" "$recipient" >> "$result_file" 2>&1 | tee -a "$result_log" fi done $result_log没有内容
根据您提供的代码,循环遍历日志信息,并根据时间范围条件将结果输出到终端和文件中。在您的代码中,您使用 `tee -a` 命令将结果追加到 `$result_log` 文件中。然而,您提到 `$result_log` 文件没有内容。
这可能是由于以下几种原因导致的:
1. 没有匹配到满足时间范围条件的日志条目。请确保时间范围设置正确,并且确保日志中存在满足条件的条目。
2. `$result_log` 文件不存在或不可写。请确保该文件存在并且具有适当的写入权限。
3. `$result_log` 文件路径设置不正确。请确保将 `$result_log` 替换为实际的文件路径。
您可以在循环之前添加一行 `echo $result_log` 来打印出 `$result_log` 的值,以确保它的值是正确的。另外,您还可以在循环之前添加一行 `echo "$filtered_logs"` 来打印出筛选后的日志内容,以查看是否有满足时间范围条件的条目。
如果问题仍然存在,请提供更多关于您的环境和日志文件的信息,以便更好地帮助您解决问题。
from_unixtime(timestamp,'%y-%m-%d')
from_unixtime(timestamp,'%y-%m-%d')是一种将Unix时间戳转换为日期的函数。Unix时间戳是从1970年1月1日00:00:00以来经过的秒数。
这个函数有两个参数。第一个参数是时间戳,可以是整数或浮点数。第二个参数是表示日期格式的字符串,其中'%y'代表两位数的年份,'%m'代表两位数的月份,'%d'代表两位数的日期。
函数的作用是将给定的时间戳转换为指定格式的日期。例如,如果时间戳是1612345678,那么函数的结果将是'21-02-03'。这意味着从1970年1月1日00:00:00到2021年2月3日的间隔是1612345678秒。
这个函数在处理时间戳时非常有用,可以将其转换为可读性更好的日期格式。它在许多计算机程序和数据库系统中都有提供。在处理时间相关的数据时,使用这个函数可以方便地将时间戳转换为日期。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)