脚本中多个参数就无法执行?

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
slysmart
帖子: 7
注册时间: 2024-05-24 13:00
系统: 22.04

脚本中多个参数就无法执行?

#1

帖子 slysmart » 2024-05-24 13:02

OUT_SQL =“/home/abc/backup/”$(date +"%Y%m%d").dump
pg_dump -h localhost -U postgres -d test -F c -b -f $OUT_SQL --这句执行没问题
----------------------------------
DB_USER="postgres "
DB_NAME="test"
OUT_SQL =“/home/abc/backup/”$(date +"%Y%m%d").dump
pg_dump -h localhost -U $DB_USER -d DE_NAME -F c -b -f $OUT_SQL
执行”pg_dump: error: connection to at "localhost"“
头像
qy117121
论坛版主
帖子: 50559
注册时间: 2007-12-14 13:40
系统: Winbuntu
来自: 志虚国乌由市
联系:

Re: 脚本中多个参数就无法执行?

#2

帖子 qy117121 » 2024-05-24 14:27

你下面数据库名称那里错了啊
渠月 · QY   
本人只会灌水,不负责回答问题
无聊可以点一下→ http://u.nu/ubuntu

邮箱 chuan@ubuntu.org.cn
头像
slysmart
帖子: 7
注册时间: 2024-05-24 13:00
系统: 22.04

Re: 脚本中多个参数就无法执行?

#3

帖子 slysmart » 2024-05-24 14:41

没有错,但就是用参数形式无法执行
头像
slysmart
帖子: 7
注册时间: 2024-05-24 13:00
系统: 22.04

Re: 脚本中多个参数就无法执行?

#4

帖子 slysmart » 2024-05-24 14:57

只是用户名和数据库名变成形参就无法执行了
头像
astolia
论坛版主
帖子: 6569
注册时间: 2008-09-18 13:11

Re: 脚本中多个参数就无法执行?

#5

帖子 astolia » 2024-05-24 19:03

slysmart 写了: 2024-05-24 14:41 没有错,但就是用参数形式无法执行
你再好好看看?
slysmart 写了: 2024-05-24 13:02 DB_USER="postgres "
DB_NAME="test"
OUT_SQL =“/home/abc/backup/”$(date +"%Y%m%d").dump
pg_dump -h localhost -U $DB_USER -d DE_NAME -F c -b -f $OUT_SQL
执行”pg_dump: error: connection to at "localhost"“
还有一处没法用颜色标记的地方:DB_USER赋值的时候引号里多加了一个空格
等于是你用户名和数据库名全部没有设置对就去连接了
头像
lilydjwg
论坛版主
帖子: 4252
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 脚本中多个参数就无法执行?

#6

帖子 lilydjwg » 2024-05-25 10:27

说句题外话:竟然有人专门用 localhost 连接 postgresql 数据库。
另外 postgresql 用户名不支持空格的吧?
头像
slysmart
帖子: 7
注册时间: 2024-05-24 13:00
系统: 22.04

Re: 脚本中多个参数就无法执行?

#7

帖子 slysmart » 2024-05-28 8:35

astolia 写了: 2024-05-24 19:03
slysmart 写了: 2024-05-24 14:41 没有错,但就是用参数形式无法执行
你再好好看看?
slysmart 写了: 2024-05-24 13:02 DB_USER="postgres "
DB_NAME="test"
OUT_SQL =“/home/abc/backup/”$(date +"%Y%m%d").dump
pg_dump -h localhost -U $DB_USER -d DE_NAME -F c -b -f $OUT_SQL
执行”pg_dump: error: connection to at "localhost"“
还有一处没法用颜色标记的地方:DB_USER赋值的时候引号里多加了一个空格
等于是你用户名和数据库名全部没有设置对就去连接了
感谢解答!
这个错误是我手误打错,实际环境中没有这个两个问题。
完整报错为:pg_dump: error: connection to server at "localhost"(127.0.01),port 5432 faile "does not exist"postgres
头像
astolia
论坛版主
帖子: 6569
注册时间: 2008-09-18 13:11

Re: 脚本中多个参数就无法执行?

#8

帖子 astolia » 2024-05-28 9:37

slysmart 写了: 2024-05-28 8:35 感谢解答!
这个错误是我手误打错,实际环境中没有这个两个问题。
完整报错为:pg_dump: error: connection to server at "localhost"(127.0.01),port 5432 faile "does not exist"postgres
你别手打了,直接把命令和结果拍照/截屏发上来
头像
slysmart
帖子: 7
注册时间: 2024-05-24 13:00
系统: 22.04

Re: 脚本中多个参数就无法执行?

#9

帖子 slysmart » 2024-05-28 13:07

error.jpg
error.jpg (8.51 KiB) 查看 313 次
,错误见截图
shell.jpg
,shell脚本
头像
astolia
论坛版主
帖子: 6569
注册时间: 2008-09-18 13:11

Re: 脚本中多个参数就无法执行?

#10

帖子 astolia » 2024-05-28 14:31

检查一下这段脚本的换行是不是dos/win风格,即回车符+换行符。
如果是的话,去掉回车符将其转换成unix风格。可以用dos2unix,或者用sed -i 's/\r//' xxx.sh
头像
slysmart
帖子: 7
注册时间: 2024-05-24 13:00
系统: 22.04

Re: 脚本中多个参数就无法执行?

#11

帖子 slysmart » 2024-05-28 16:02

astolia 写了: 2024-05-28 14:31 检查一下这段脚本的换行是不是dos/win风格,即回车符+换行符。
如果是的话,去掉回车符将其转换成unix风格。可以用dos2unix,或者用sed -i 's/\r//' xxx.sh
非常感谢,就是此问题导致的了。
头像
slysmart
帖子: 7
注册时间: 2024-05-24 13:00
系统: 22.04

Re: 脚本中多个参数就无法执行?

#12

帖子 slysmart » 2024-05-28 16:28

lilydjwg 写了: 2024-05-25 10:27 说句题外话:竟然有人专门用 localhost 连接 postgresql 数据库。
另外 postgresql 用户名不支持空格的吧?
刚开始使用postgresql,实际应用中为IP地址
host all all 192.168.1.0/24 md5 方式
这样安全性够不够?
头像
astolia
论坛版主
帖子: 6569
注册时间: 2008-09-18 13:11

Re: 脚本中多个参数就无法执行?

#13

帖子 astolia » 2024-05-28 17:22

slysmart 写了: 2024-05-28 16:28 刚开始使用postgresql,实际应用中为IP地址
host all all 192.168.1.0/24 md5 方式
这样安全性够不够?
看你的软件架构了。如果是直接开放数据库给远端连,建议将连接方式换成hostssl,认证方式换成scram-sha-256。如果是只供本机上服务端程序访问,连接方式换成local
回复