嵌套的for循环在Shell脚本编码中执行Hive时出错。

huangapple go评论60阅读模式
英文:

Nested for loop error in shell script coding for Hive execution

问题

我试图打印Hive数据库中所有表的DDL语句。
我在shell脚本中使用了2个嵌套的'for循环'。由于我对shell脚本不熟悉,我不知道自己犯了什么错误。

以下是代码:
#!/bin/bash
rm -f DDL_table_names.txt

declare -a db_name=("dev_cmt" "dev_cmt_fleet")

for DB in "${db_name[@]}"
    do
    echo "$DB:" >> DDL_table_names.txt
    for Tab in "$DB"
        do
	      hive -e "use $DB;show create table $Tab;" >> DDL_table_names.txt
  		  echo -e "\n" >> DDL_table_names.txt
	  done 
    done

echo "Table file generated"

我遇到的错误:

失败:SemanticException [错误10001]:找不到表dev_cmt

提前致谢!


<details>
<summary>英文:</summary>

I am trying to print DDLs of all the tables present in a database of Hive. 
I am using 2 nested &#39;for loops&#39; in the shell script. As I am new to shell script, I don&#39;t know what mistake I am making. 

the code is given below 

#!/bin/bash
rm -f DDL_table_names.txt

declare -a db_name=("dev_cmt" "dev_cmt_fleet")

for DB in "${db_name[@]}"
do
echo "$DB:">> DDL_table_names.txt
for Tab in "$DB"
do
hive -e "use $DB;show create table $Tab;">>DDL_table_names.txt
echo -e "\n" >> DDL_table_names.txt
done
done

echo "Table file generated"


The error I am getting:

FAILED: SemanticException [Error 10001]: Table not found dev_cmt


Thanks in advance!

</details>


# 答案1
**得分**: 1

需要将`for Tab in "$DB"`替换为`for Tab in $(hive -e "use $DB;show tables;")`。这应该可以正常工作。

<details>
<summary>英文:</summary>

You need to replace `for Tab in &quot;$DB&quot;`
with `for Tab in $(hive -e &quot;use $DB;show tables;&quot;)`

It should work.




</details>



# 答案2
**得分**: 0

for Tab in "$DB"

如果Tab是DB中的一个表,那么该表在数据库中不存在。

<details>
<summary>英文:</summary>

    for Tab in &quot;$DB&quot;

it is Tab is one of DB, the table does not exist in database.

</details>



huangapple
  • 本文由 发表于 2023年3月9日 15:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定