2017-10-10 52 views
0

我在Linux中有一个文件。该文件包含表格名称。根据条件在Linux中创建新文件

现在我想检查这个文件并根据条件创建文件。

table=${} 
validateTable=$(hive --database "${hivedb}" -e "SHOW TABLES LIKE '$table'") 
if [[ -z $validateTable ]]; then 
    Add to file New_imports 
else 
    Add to file Already exists 
fi 

例如:

该文件包含

table1 
table2 
table3 
table4 

在上述表table1table2已经现有。

所以我想两个文件

1)New_imports为不存在的 2表)已经存在,存在

new_imports

table3 
table4 

already exists

table1 
table2 

我该如何交流hieve我的结果

#!/bin/bash 
while read table ; do 
    table=${1:1:-1} 
    validateTable=$(hive --database "${hivedb}" -e "SHOW TABLES LIKE '$table'") 
    if [[ -z $validateTable ]]; then 
     echo "$table" >> New_imports 
    else 
     echo "$table" >> Already_exists 
    fi 
done < tableFile 
+0

不知道为什么有人甚至低估你的原始Q.哦,好! – shellter

回答

2
#!/bin/bash 
while read table ; do 
    table=$(echo "$table" | sed 's/[][]//g;s/'"'"'//g') 
    validateTable=$(hive --database "${hivedb}" -e "SHOW TABLES LIKE '$table'") 
    if [[ -z $validateTable ]]; then 
     echo "$table" >> New_imports 
    else 
     echo "$table" >> Already_exists 
    fi 
done < tableFile 

应该让你开始。做出这样的防弹,并且可以接受争论还需要做更多的工作。

管理输出文件可能需要一些操作。请注意,使用>>意味着追加,所以每次运行时都需要删除这些文件或进行管理。我建议嵌入在文件名中的日期 - 时间标记让他们像

echo "$table" >> new_imports.$(/bin/date +%Y-%m-%d.%H:%M) 

另外,我没有反正有hive来测试这一点,所以这是一个通用的解决方案。

IHTH

+0

我在表格中的文件名就像'['testing_1234']'。从这里我想获得'testing_1234'作为表名并传递给脚本。当我这样做时,脚本不会读取表名,请检查我的问题的编辑部分 –

+0

如果运行'hive --database“$ {hivedb}”-e“会发生什么情况SHOW TABLES LIKE'['testing_1234']' “'(或''\ ['testing_1234'\]'')(或''\ [testing_1234 \]''(或其他引用))。如果它不起作用,那么编辑你的Q以显示cmd-line中的'testing_1234'的工作方式。我今天晚上出去了一段时间,会尽量检查。祝你好运。 – shellter

+0

唯一适用于'hive --database“的形式$ {hivedb}”-e“SHOW TABLES LIKE testing_1234”'是'testing_1234'只有当表格像'testing_1234'这样的表不能工作与其他报价 –