【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.