根据条件在Linux中创建新文件
我在根据条件在Linux中创建新文件,linux,bash,shell,Linux,Bash,Shell,我在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 t
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
在上述表格中,table1
和table2
已经存在
所以我想要两个文件
1) 为不存在的表导入新的\u
2) 对于已存在的表,已存在
new\u导入
table3
table4
已存在
table1
table2
我怎样才能达到我的结果
#/bin/bash
一边读表格;做
表=${1:1:-1}
validateTable=$(配置单元--数据库“${hivedb}”-e“显示类似于“$table”的表)
如果[-z$validateTable]];然后
echo“$table”>>新的\u导入
其他的
echo“$table”>>已存在
fi
完成<表格文件
另外,我不必用hive
来测试这一点,所以这是一个通用的解决方案
IHTH
另外,我不必用hive
来测试这一点,所以这是一个通用的解决方案
IHTH文件中我的表名类似于
['testing_1234']
。从这里我想得到testing_1234
作为表名并传递给脚本。当我执行此操作时,脚本不会读取表名,请检查问题的编辑部分如果从cmd行运行hive--database“${hivedb}”-e“显示诸如“['testing\'u 1234']”“
(或“\['testing\'u 1234'\]”
(或“\[testing\'u 1234\]”或其他引号)之类的表,会发生什么情况。如果它不起作用,那么编辑你的Q以显示什么形式的testing_1234
在cmd行中起作用。我今晚出去一会儿,我会尽可能地办理入住手续。祝你好运。hive——数据库“${hivedb}”-e“像testing_1234一样显示表”
是testing_1234
只有当表像testing_1234
时,hive语句才起作用,它不与其他引用一起工作,但表文件中的实际值正好是['testing_1234']
?我可以很容易地想出一个sed
命令来消除额外的字符,但是你不能先在文件中输入“普通”名称吗?(或者这是一个更大系统的一部分,或者…?)。过一会儿。是的,表格文件中的值与['testing_1234']
完全相同。我在文件中的表格名称与['testing_1234']
相似。从这里我想得到testing_1234
作为表名并传递给脚本。当我执行此操作时,脚本不会读取表名,请检查问题的编辑部分如果从cmd行运行hive--database“${hivedb}”-e“显示诸如“['testing\'u 1234']”“
(或“\['testing\'u 1234'\]”
(或“\[testing\'u 1234\]”或其他引号)之类的表,会发生什么情况。如果它不起作用,那么编辑你的Q以显示什么形式的testing_1234
在cmd行中起作用。我今晚出去一会儿,我会尽可能地办理入住手续。祝你好运。hive——数据库“${hivedb}”-e“像testing_1234一样显示表”
是testing_1234
只有当表像testing_1234
时,hive语句才起作用,它不与其他引用一起工作,但表文件中的实际值正好是['testing_1234']
?我可以很容易地想出一个sed
命令来消除额外的字符,但是你不能先在文件中输入“普通”名称吗?(或者这是一个更大系统的一部分,或者…?)。过了一会儿。是的,表格文件中的值与['testing_1234']
完全相同。我不知道为什么有人甚至否决了你原来的Q。哦,好吧!不知道为什么有人甚至否决了你原来的问题。哦,好吧!
#!/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
#!/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)