天天看點

【hadoop fs指令】if,then,else,fi測試

test語句:(help test與man test均是一種提示)

【hadoop fs指令】if,then,else,fi測試
【hadoop fs指令】if,then,else,fi測試

執行語句之前:

【hadoop fs指令】if,then,else,fi測試

執行語句1:

if $(/opt/hadoop/bin/hadoop fs -test -e test99.txt);

then $(/opt/hadoop/bin/hadoop fs -touchz test66.txt );

else $(/opt/hadoop/bin/hadoop fs -touchz test55.txt);

fi

執行語句之後:

【hadoop fs指令】if,then,else,fi測試

執行語句2:

if $(/opt/hadoop/bin/hadoop fs -test -e test77.txt);

then $(/opt/hadoop/bin/hadoop fs -touchz test66.txt );

else $(/opt/hadoop/bin/hadoop fs -touchz test55.txt);

fi

執行語句之後:

【hadoop fs指令】if,then,else,fi測試

執行語句3:

if $(/opt/hadoop/bin/hadoop fs -test -e /home/hadoop/test77.txt);

then $(/opt/hadoop/bin/hadoop fs -touchz test33.txt );

else $(/opt/hadoop/bin/hadoop fs -touchz test44.txt);

fi

執行語句之後:

【hadoop fs指令】if,then,else,fi測試

執行語句4:

if $(/opt/hadoop/bin/hadoop fs -test -e /home/hadoop/test3.txt);

then $(/opt/hadoop/bin/hadoop fs -touchz test33.txt );

else $(/opt/hadoop/bin/hadoop fs -touchz test44.txt);

fi

執行語句之後:(與想象不符,test33不出來)

【hadoop fs指令】if,then,else,fi測試

執行語句5:

if $(/opt/hadoop/bin/hadoop fs -test -e /home/hadoop/test1.txt);

then $(/opt/hadoop/bin/hadoop fs -touchz test33.txt );

else $(/opt/hadoop/bin/hadoop fs -touchz test44.txt);

fi

執行語句之後:(依舊沒有反應,test33依舊不出來)

【hadoop fs指令】if,then,else,fi測試

執行語句6:

if $(/opt/hadoop/bin/hadoop fs -test -e /home/hadoop/test77.txt);

then $(/opt/hadoop/bin/hadoop fs -touchz test333.txt );

else $(/opt/hadoop/bin/hadoop fs -touchz test444.txt);

fi

執行語句之後:(反應正常)

【hadoop fs指令】if,then,else,fi測試

執行語句7:

if $(/opt/hadoop/bin/hadoop fs -test -e /home/hadoop/test3.txt);

then $(/opt/hadoop/bin/hadoop fs -touchz test333.txt );

else $(/opt/hadoop/bin/hadoop fs -touchz test444.txt);

fi

執行語句之後:(test333依舊不出來,與想象不符)

【hadoop fs指令】if,then,else,fi測試

執行語句8:

if $(/opt/hadoop/bin/hadoop fs -test -e test3.txt);

then $(/opt/hadoop/bin/hadoop fs -touchz test333.txt );

else $(/opt/hadoop/bin/hadoop fs -touchz test444.txt);

fi

執行語句之後:(檢測HDFS上的檔案,反而出來了)

【hadoop fs指令】if,then,else,fi測試

總結:

  1. ./hadoop fs -test -e <檔案路徑>可以檢測HDFS檔案(是否存在),也可以檢測本地檔案(是否存在),以檢測HDFS檔案為主,本地容易發生莫名其妙的錯誤。
  2. If【-test -e】,then,else就像正常邏輯使用,if檔案存在,則執行then,否則執行else。Help文檔中的true為0,可能是整體的函數return 0.

繼續閱讀