英文:
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 'for loops' in the shell script. As I am new to shell script, I don'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 "$DB"`
with `for Tab in $(hive -e "use $DB;show tables;")`
It should work.
</details>
# 答案2
**得分**: 0
for Tab in "$DB"
如果Tab是DB中的一个表,那么该表在数据库中不存在。
<details>
<summary>英文:</summary>
for Tab in "$DB"
it is Tab is one of DB, the table does not exist in database.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论