3

我需要在 bash 脚本中运行 hadoop 命令,该命令会遍历亚马逊 S3 上的一堆文件夹,然后将这些文件夹名称写入 txt 文件,然后进行进一步处理。但问题是当我运行脚本时,似乎没有将文件夹名称写入 txt 文件。我想知道是否是hadoop命令运行时间过长,而bash脚本没有等到它完成并继续进行进一步的处理,如果是这样,我怎样才能让bash等到hadoop命令完成然后去做其他过程​​?

这是我的代码,我尝试了两种方式,都不起作用:

1. 
listCmd="hadoop fs -ls s3n://$AWS_ACCESS_KEY:$AWS_SECRET_KEY@$S3_BUCKET/*/*/$mydate | grep s3n | awk -F' ' '{print $6}' | cut -f 4- -d / > $FILE_NAME"                            
echo -e "listing... $listCmd\n"                                                                                                                                                   
eval $listCmd
...other process ...

2. 
echo -e "list the folders we want to copy into a file"
hadoop fs -ls s3n://$AWS_ACCESS_KEY:$AWS_SECRET_KEY@$S3_BUCKET/*/*/$mydate | grep s3n | awk -F' ' '{print $6}' | cut -f 4- -d / > $FILE_NAME
... other process ....

任何人都知道可能出了什么问题?是使用eval函数还是直接使用第二种方式运行hadoop命令更好

谢谢。

4

1 回答 1

2

在这种情况下,我更愿意eval将下一个命令附加到这个命令。我宁愿分解成几部分,这样你就知道,或级别listCmd没有错。grepawkcut

listCmd="hadoop fs -ls s3n://$AWS_ACCESS_KEY:$AWS_SECRET_KEY@$S3_BUCKET/*/*/$mydate > $raw_File"
gcmd="cat $raw_File | grep s3n | awk -F' ' '{print $6}' | cut -f 4- -d / > $FILE_NAME"
echo "Running $listCmd and other commands after that"
otherCmd="cat $FILE_NAME"
eval "$listCmd";
echo $?  # This will print the exit status of the $listCmd
eval "$gcmd" && echo "Finished Listing" && eval "$otherCmd"

otherCmd只有成功才会执行$gcmd。如果您有太多需要执行的命令,那么这会变得有点难看。如果您大致知道需要多长时间,您可以插入一个 sleep 命令。

 eval "$listCmd"
 sleep 1800  # This will sleep 1800 seconds
 eval "$otherCmd"
于 2013-10-03T02:17:43.893 回答