YLR_Really 发表于 2012-09-18 15:51

shell脚本处理sql结果

#!/bin/sh
sqlplus oracle/oracle_*\)*!%^ as sysoper <<EOF
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
commit;
exit;
EOF



比如得到上面的时间以后,我需要处理一下再返回结果,应该怎么样写脚本?比如我只要返回最后时间那一行。先感谢各路高手!

linux_c_py_php 发表于 2012-09-18 16:41

本帖最后由 linux_c_py_php 于 2012-09-18 16:46 编辑

目测此种情况下, 只有高端方案可以解决:

C实现:#fd = open(".sql_output.tmp", "w"); stdout_backup = dup(1); stderr_backup = dup(2); dup2(fd, 1); dup2(fd, 2);
#do you sql query here ...
#dup2(stdout_backup, 1); dup2(stderr_backup, 2);close(stdout_backup);close(stderr_backup);close(fd);SHELL实现:#!/bin/sh

exec 5>.sql_output.tmp
exec 3>&1
exec 4>&2
exec 1>&5
exec 2>&5

sqlplus oracle/oracle_*\)*!%^ as sysoper <<EOF
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
commit;
exit;
EOF

exec 1>&3
exec 2>&4
exec 3>&-
exec 4>&-
exec 5>&-

#在这里读取.sql_output.tmp即可.

YLR_Really 发表于 2012-09-19 10:26

回复 2# linux_c_py_php #!/bin/sh

exec 5>.sql_output.tmp
exec 3>&1
exec 4>&2
exec 1>&5
exec 2>&5

sqlplus oracle/oracle_*\)*!%^ as sysoper <<EOF
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
commit;
exit;
EOF

exec 1>&3
exec 2>&4
exec 3>&-
exec 4>&-
exec 5>&-

cat .sql_output.tmp这样读取不对么?请指点一下。

linux_c_py_php 发表于 2012-09-19 11:13

没有问题吧, 我都是测试过的.

比如:$ cat main.sh
#!/bin/bash

#!/bin/sh

exec 5>.sql_output.tmp
exec 3>&1
exec 4>&2
exec 1>&5
exec 2>&5

cat <<EOF
ni hao ma
wo shi liang dong
EOF

exec 1>&3
exec 2>&4
exec 3>&-
exec 4>&-

#在这里读取.sql_output.tmp即可.
cat .sql_output.tmp
$ ./main.sh
ni hao ma
wo shi liang dong

YLR_Really 发表于 2012-09-19 11:42

#!/bin/bash
#!/bin/sh

exec 5>.sql_output.tmp
exec 3>&1
exec 4>&2
exec 1>&5
exec 2>&5

cat <<EOF
ni hao!
wo de zhen bu ke yi...
EOF

exec 1>&3
exec 2>&4
exec 3>&-
exec 4>&-


cat .sql_output.tmp# ./main.sh
-bash: ./main.sh: Permission denied
# chmod 777 main.sh
# ./main.sh
-bash: ./main.sh: /bin/bash^M: bad interpreter: No such file or directory惭愧啊。。。我的真不可以。。。。求指教,感谢!

linux_c_py_php 发表于 2012-09-19 11:46

本帖最后由 linux_c_py_php 于 2012-09-19 11:46 编辑

你没有/bin/bash, 与我无关.
页: [1]
查看完整版本: shell脚本处理sql结果