シェル内のHDFSファイルとディレクトリの判断

シェルでは、次のスクリプトのように、ファイルが存在するかどうかを判断したり-f $fileName、フォルダが存在するかどうかを判断したりできます。ファイルとフォルダのシェル判断判断-d $directory

#!/bin/sh

file="/opt/cdh-5.7.6/hadoop-2.6.0-cdh5.7.6/stop_all.sh"
if [ ! -f "$file" ]; then
  echo "文件不存在!"
else 
  echo "文件存在!"
fi

directory="/opt/cdh-5.7.6/hadoop-2.6.0-cdh5.7.6/"
if [ ! -d "$directory" ]; then
  echo "文件夹不存在!"
else 
  echo "文件夹存在!"
fi

では、hdfsファイルとフォルダーが存在するかどうかを判断する方法は?
まず、hdfsに組み込まれているコマンドとパラメーターを見てみましょう。

[fuyun@bigdata-training shell]$ hdfs dfs -help
Usage: hadoop fs [generic options]
	[-appendToFile <localsrc> ... <dst>]
	[-cat [-ignoreCrc] <src> ...]
	[-checksum <src> ...]
	[-chgrp [-R] GROUP PATH...]
	[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
	[-chown [-R] [OWNER][:[GROUP]] PATH...]
	[-copyFromLocal [-f] [-p] [-l] <localsrc> ... <dst>]
	[-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-count [-q] [-h] [-v] <path> ...]
	[-cp [-f] [-p | -p[topax]] <src> ... <dst>]
	[-createSnapshot <snapshotDir> [<snapshotName>]]
	[-deleteSnapshot <snapshotDir> <snapshotName>]
	[-df [-h] [<path> ...]]
	[-du [-s] [-h] <path> ...]
	[-expunge]
	[-find <path> ... <expression> ...]
	[-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-getfacl [-R] <path>]
	[-getfattr [-R] {
    
    -n name | -d} [-e en] <path>]
	[-getmerge [-nl] <src> <localdst>]
	[-help [cmd ...]]
	[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...]]
	[-mkdir [-p] <path> ...]
	[-moveFromLocal <localsrc> ... <dst>]
	[-moveToLocal <src> <localdst>]
	[-mv <src> ... <dst>]
	[-put [-f] [-p] [-l] <localsrc> ... <dst>]
	[-renameSnapshot <snapshotDir> <oldName> <newName>]
	[-rm [-f] [-r|-R] [-skipTrash] <src> ...]
	[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
	[-setfacl [-R] [{
    
    -b|-k} {
    
    -m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]
	[-setfattr {
    
    -n name [-v value] | -x name} <path>]
	[-setrep [-R] [-w] <rep> <path> ...]
	[-stat [format] <path> ...]
	[-tail [-f] <file>]
	[-test -[defsz] <path>]
	[-text [-ignoreCrc] <src> ...]
	[-touchz <path> ...]
	[-usage [cmd ...]]

...........
...........
-test -[defsz] <path> :
  Answer various questions about <path>, with result via exit status.
    -d  return 0 if <path> is a directory.
    -e  return 0 if <path> exists.
    -f  return 0 if <path> is a file.
    -s  return 0 if file <path> is greater than zero bytes in size.
    -z  return 0 if file <path> is zero bytes in size, else return 1.
..........
..........

パラメータの説明:

  • -dパスがディレクトリの場合、0を返します
  • -eパスが存在する場合は、0を返します
  • -fパスがファイルの場合、0を返します
  • -sは、ファイルのサイズが0バイトより大きい場合に0を返します。
  • -zファイルのサイズが0の場合は0を返し、そうでない場合は1を返します。

パスが存在するかどうかを確認します

#!/bin/sh

hdfs dfs -test -e "/fuyun/"
if [ $? -eq 0 ]; then
  echo "parth is exists!"
else 
  echo "Error! parth is not exists!"
fi

testコマンドは、ファイルがフォルダーであるかどうか、ファイルであるかどうか、ファイルのサイズが0より大きいか0に等しいかどうかを判別することもできます。

#!/bin/sh

hdfs dfs -test -e "/fuyun/"
# 判断路径是否存在
if [ $? -eq 0 ]; then
  echo "parth is exists!"
else 
  echo "Error! parth is not exists!"
fi

# 判断是否为文件夹
hdfs dfs -test -d "/fuyun/"
if [ $? -eq 0 ]; then
  echo "parth is directory!"
else 
  echo "parth is not directory!"
fi

# 判断是否为文件
hdfs dfs -test -f "/fuyun/"
if [ $? -eq 0 ]; then
  echo "parth is file!"
else 
  echo "parth is not file!"
fi

# 判断路径的大小是否大于0 bytes
hdfs dfs -test -s "/fuyun/"
if [ $? -eq 0 ]; then
  echo "Is greater than zero bytes in size!"
else 
  echo "Is not greater than zero bytes in size!"
fi

# 判断路径的大小是否等于0 bytes
hdfs dfs -test -s "/fuyun/"
if [ $? -eq 0 ]; then
  echo "Is zero bytes in size!"
else 
  echo "Is not zero bytes in size!"
fi

おすすめ

転載: blog.csdn.net/lz6363/article/details/115034645