如果我正確理解你的目標是驗證存在於每個File
領域,考慮以下爲相關的例子:
#!/bin/bash
# ^^^^- IMPORTANT: not /bin/sh
sep=$'\v' # pick a character that can't be in your data
while IFS="$sep" read -r Name EmailWhenMissing CustomerEmail; do
# the line below this provides verbose logging when running with bash -x
: Name="$Name" EmailWhenMissing="$EmailWhenMissing" CustomerEmail="$CustomerEmail"
[[ $EmailWhenMissing ]] || { echo "File $Name is missing EmailWhenMissing"; }
[[ $CustomerEmail ]] || { echo "File $Name is missing CustomerEmail"; }
done < <(xmlstarlet sel -t -m '//File' \
-v ./@Name -o "$sep" \
-v ./EmailWhenMissing -o "$sep" \
-v ./CustomerEmail -n)
鑑於以下輸入文件:
<root>
<File Name="something.txt">
<EmailWhenMissing>Customer</EmailWhenMissing>
<CustomerEmail>[email protected]</CustomerEmail>
</File>
<File Name="somethingElse.txt">
<EmailWhenMissing>Customer</EmailWhenMissing>
<CustomerEmail>[email protected]</CustomerEmail>
</File>
<File Name="NoEmailWhenMissing.txt">
<CustomerEmail>[email protected]</CustomerEmail>
</File>
<File Name="NoCustomerEmail.txt">
<EmailWhenMissing>Customer</EmailWhenMissing>
</File>
<File Name="EmptyFile.txt"/>
</root>
...它的輸出是:
File NoEmailWhenMissing.txt is missing EmailWhenMissing
File NoCustomerEmail.txt is missing CustomerEmail
File EmptyFile.txt is missing EmailWhenMissing
File EmptyFile.txt is missing CustomerEmail
隨着這裏對於bash的代碼,一些有用的閱讀:
- BashFAQ #1 - 我怎樣才能讀取一個文件(數據流,可變)逐行(和/或逐場?)
- BashFAQ #24 - 我在變量中設置了一個循環管道。爲什麼它們在循環終止後消失?或者,爲什麼我不能讀取數據? - 解釋了
< <(...)
循環表單的推理。
我建議您使用XML模塊在perl或python中編寫腳本。 –