将 bash 变量传递给 sqlite3

问题描述 投票:0回答:1

我想将变量从 STD 输入传递到表/成员列,就像 @example.com 一样。 我的表架构是这样的:

0|ID|INTEGER|0||1     1|PolicyGroupID|INT8|0||0
2|Member|VARCHAR(255)|1||0     3|Disabled|SMALLINT|1|'0'|0    
4|Comment|VARCHAR(1024)|0||0

这是我的代码;

#!/bin/bash
#get SQlitedb Path     
DBPATH=./test.sqlitedb     
#first get domain vi STDIN     
echo Enter Your Domain:     
read Domain     
#add Domain To Database     
sqlite3 $DBPATH <<'END_SQL'     
.timeout 2000     
NUMID=$(SELECT ID FROM policy_group_members ORDER BY ID DESC LIMIT 1;)     
echo $NUMID     
INSERT INTO policy_group_members VALUES (52, 3, \@"${Domain}\", 0,"");     
END_SQL

如何导入我的域名以及如何逐一递增 id 列

bash sqlite
1个回答
0
投票

当您在 heredoc 中使用单引号时,您可以显式阻止 shell 变量扩展,因此可以使用:

sqlite3 $DBPATH <<'END_SQL'     
.timeout 2000
NUMID=$(SELECT ID FROM policy_group_members ORDER BY ID DESC LIMIT 1;)     
echo $NUMID     
INSERT INTO policy_group_members VALUES (52, 3, \@"${Domain}\", 0,"");     
END_SQL

sqlite3 $DBPATH <<END_SQL     
.timeout 2000
NUMID=$(SELECT ID FROM policy_group_members ORDER BY ID DESC LIMIT 1;)     
echo $NUMID     
INSERT INTO policy_group_members VALUES (52, 3, \@"${Domain}\", 0,"");     
END_SQL
© www.soinside.com 2019 - 2024. All rights reserved.