Search code examples
shellfor-loophive

Nested for loop error in shell script coding for Hive execution


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!


Solution

  • You need to replace for Tab in "$DB" with for Tab in $(hive -e "use $DB;show tables;")

    It should work.