简体   繁体   English

需要Shell脚本来获取创建的文件名

[英]Need Shell script for getting the file name created

I am writing a script to get output if a specific file is created successfully or failed. 我正在编写脚本以获取成功创建或失败的特定文件的输出。

Example: In a path /x/y/z/ everyday a file with name abc.out_timestamp will be created. 示例:每天都会在路径/ x / y / z /中创建一个名称为abc.out_timestamp的文件。

The script that i am working needs check if the file is created today or not and show the output accordingly. 我正在工作的脚本需要检查文件是否今天创建,并相应地显示输出。

Path : /x/y/z have the below files
abc.out0114181500
abc.out0115181600
abd.out0116182100

script to be run: 要运行的脚本:

#!/bin/bash
TODAY=$(date +%m%d%y)

cd /x/y/z

if [ -f abc.out_$TODAY ]
then
      echo "$file creation successful"
else
     echo "$file creation not successful"
fi

output received: file creation not successful (though a file is created today) expected output: file creation successful 收到的输出:文件创建不成功(尽管今天已创建文件)预期的输出:文件创建成功

How can i correct this, can anyone please let me know how i can the output as file creation successful. 我该如何纠正,有人可以让我知道如何成功创建文件时输出。

Thanks 谢谢

You're 98% of the way there 您已经完成了98%

#!/bin/bash
TODAY=$(date +%m%d%y)

cd /x/y/z

if [ -f abc.out${TODAY}???? ]
then
     echo "$file creation successful"
else
     echo "$file creation not successful"
fi

The pattern abc.out${TODAY}???? 模式abc.out${TODAY}???? matches the filename with today's date followed by 4 more characters (and without the underscore, which was spurious). 将文件名与今天的日期匹配,后跟另外4个字符(并且没有下划线,这是虚假的)。

If you want more validation, use 如果您想进行更多验证,请使用

if [ -f abc.out${TODAY}[0-2][0-9][0-5][0-9] ]

I hope the abd. 我希望abd. was a typo and it's really abc. 是一个错字,而且确实是abc. However, if it could be abc or abd you can handle that with 但是,如果它可以是abcabd ,则可以使用

if [ -f ab[cd].out${TODAY}[0-2][0-9][0-5][0-9] ]

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

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