SHELL脚本

脚本中需要用sqlplus获取数据库的一行数据,语句很简单,不想传递sql文件,可以使用Here Document重定向查询语句:

sqlplus -s $user/$token@$sid <<EOF
select localtimestamp from dual;
exit;
EOF

但是使用了Here Document后,脚本的缩进格式有点乱了😢

那么我们把语句放进变量里面吧。

stmt="select localtimestamp from dual;exit;"
sqlplus -s $user/$token@$sid <<EOF
$stmt
EOF

噫,这不还是乱的吗,有啥区别。还不直接用文件当变量。

echo "select localtimestamp from dual;exit;" > stmt.sql
sqlplus -s $user/$token@$sid @stmt.sql

有必要生成这么多中间产物吗?况且这文件名不随机化,以后会搞大系统啊。

能不能直接上管道?

echo "select localtimestamp from dual;exit;" | xargs -I {} .sqlplus -s $user/$token@$sid {}

不行。因为标准输入和参数输入是两个概念。

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0