Mac 下 Sed 参数 i 出现 invalid command code

很少在 Mac 上调试 Shell,今天在 Mac 调试 Shell 的时候发现 sed 的用法还有点不一样,报错:

1
sed: 1: "02.data.sql": invalid command code .

解决

-i 后面增加'' 变成 -i '' 即可

Mac

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

public_domain="auth.xxx.com"
private_domain="authconsole.xxx.com"

if [ "$(sed -i '' s/ztsg.xxx.com/${public_domain}/ 02.data.sql)" ]; then
echo "public_domain update success"
fi

if [ "$(sed -i '' s/ztsgconsole.xxx.com/${private_domain}/ 02.data.sql)" ]; then
echo "private_domain update success"
fi

Linux

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

public_domain="auth.xxx.com"
private_domain="authconsole.xxx.com"

if [ "$(sed -i s/ztsg.xxx.com/${public_domain}/ 02.data.sql)" ]; then
echo "public_domain update success"
fi

if [ "$(sed -i s/ztsgconsole.xxx.com/${private_domain}/ 02.data.sql)" ]; then
echo "private_domain update success"
fi

MacLinux 下分别用以上两种写法就可以了