以下是一个示例脚本,将验证输入的文件路径是否存在,匹配文件名,并显示在文件中的行数。
#!/bin/bash
#获取文件路径并验证是否存在
read -p "Enter the path of the file: " filePath
if [ ! -f "$filePath" ]; then
echo "File not found"
exit 1
fi
#获取文件名并验证是否匹配
read -p "Enter the file name pattern: " fileName
if ! find "$filePath" -name "*$fileName*" -print -quit | grep -q .; then
echo "No such file found"
exit 1
fi
#计算文件中的行数
lineCount=`wc -l < "$filePath"`
echo "File path: $filePath"
echo "File name pattern: $fileName"
echo "Line count: $lineCount"
使用该脚本,用户将被提示输入文件路径和文件名模式。脚本将验证文件路径是否存在,并在文件名不匹配时显示错误消息。然后脚本将计算文件中的行数并显示相应的输出。
示例输出:
Enter the path of the file: /home/user/testfile.txt
Enter the file name pattern: test
File path: /home/user/testfile.txt
File name pattern: test
Line count: 10
下一篇:bash脚本-无法创建目录?