linux - bash:意外标记附近的语法错误 `|'

标签 linux bash shell awk

此命令在命令提示符下运行良好:

 ]$ ls -ltr ../cmxserver.log*|grep "`date | awk '{print $2" "$3}'`"|cut -d "/" -f2
 cmxserver.log.2
 cmxserver.log.1
 cmxserver.log

但是,在 for 循环中使用它会给出错误 bash:意外标记 `|' 附近的语法错误

   ]$for y in `ls -ltr ../cmxserver.log*|grep "`date | awk '{print $2" "$3}'`"|cut -d "/" -f2`

 -bash: syntax error near unexpected token `|'

有什么想法吗?

谢谢

最佳答案

整行可以通过find命令解决:

find .. -maxdepth 1 -mtime -1 -daystart -name 'cmxserver.log*' -printf "%f\n"
  • .. - 搜索目录
  • -maxdepth 1 - 不要递归到子目录
  • -mtime -1 - 只有今天的文件
  • -daystart - 从午夜算起,而不是过去 24 小时
  • -name 'cmxserver.log*' - 文件名
  • -printf "%f\n" - 只打印 basename

关于linux - bash:意外标记附近的语法错误 `|',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32583542/

相关文章:

linux - 如何在 zshell 中以关键字拆分字符串并保存结果?

Linux 终端 : how to copy output to a file and print at terminal simultaneously?

linux - 使用 Bash 统计我编写的代码行数

string - 从路径中提取目录

mysql - 停止来自 mysql 终端的警告

shell - 如何阻止 GNU make 在 MAKEFILE_LIST 中扩展美元符号?

c - pthread_self() 返回的线程 ID 与调用 gettid(2) 返回的内核线程 ID 不同

java - Kepler 2.3安装出现错误

linux - 在第一次出现特定单词后用 sed 替换 - Linux/Ubuntu

bash - 如何调试 Bash 脚本?