我有一个检查文件夹并输出三行的脚本:
1st line : Changes to the subfolders 2nd line : Changes to the number of files 3rd line : Changes of file size in the folder
这是输出的示例:
1 sub-folders added Number of files didn't change Files size didn't change
我想使用命令逐行检查输出并测试脚本。
就像是
line 1 == "1 sub-folders added" && line 2 == "Number of files didn't change" && line 3 == "Files size didn't change" then print success if it matches.
哪个命令可以做到这一点?
谢谢
答案1
您可以使用head
和tail
来获取相应的行和值。
例子:
OUTPUT=`yourcommand` ; if [ "`echo \"$OUTPUT\" | head -n 1`" = "1 sub-folders added" ] && [ "`echo \"$OUTPUT\" | head -n 2 | tail -n 1`" = "Number of files didn't change" ] && [ "`echo \"$OUTPUT\" | tail -n 1`" = "Files size didn't change" ] ;then echo "success" ; else echo "error" ; fi
(不确定关键字didn't
;您可能需要用反斜杠转义撇号。(例如\'
)。
看起来您希望将所有内容都集中在一句话中,所以就这样了。
请注意,您的第一行在数字“1”之前有一个空格;您可能希望修改您的输出或此脚本。