sed中的可变替换

时间:2013-12-11 20:52:13

标签: mysql macos bash ssh sed

有谁知道为什么这个脚本不起作用?

filename:db

#!/usr/local/bin/bash
if [ -z "$1" ]; then
    echo "Must specify source DB environment"
    echo "One of s (for Staging), p (for Production)"
    exit
elif [ "$1" == "s" -o "$1" == "p" ]; then
    if [ "$1" == "s" ]; then
        echo "Connecting to Staging DB"
        URL='staging\.example\.com'
        PATH='/var/www/vhosts/staging\.example\.com'
    else
        echo "Connecting to Production DB"
        URL='example\.com'
        PATH='/var/www/vhosts/example\.com'
    fi
    ssh host "mysqldump -udbuser -pdbpass staging | gzip -9" | gzip -d | sed -e "s+$URL+example\.local+g" -e "s+$PATH+/Library/WebServer/Documents/example+g" | mysql -uroot -proot example_local
fi

我收到以下错误:

./db: line 14: ssh: command not found
./db: line 14: gzip: command not found
./db: line 14: sed: command not found
./db: line 14: mysql: command not found

注意:

  • ./db的权限为777。
  • host已保存在.ssh/config中。我可以运行ssh host并进入远程机器没有问题。
  • mysql凭据对于远程和本地
  • 都是正确的
  • 我可以在命令行上运行最后的ssh one-liner就好了。我很确定这是sed中的变量替换,它搞砸了。

谢谢!

1 个答案:

答案 0 :(得分:3)

问题是你的脚本中的这样的行:

PATH='/var/www/vhosts/staging\.example\.com'

您将保留路径env variable PATH设置为其他内容,并从/bin, /usr/bin, /sbin, /usr/sbin, /usr/local/bin中删除PATH等,这是运行sed, awk, grep, find等核心unix二进制文件所需的内容。< / p>

最好使用PATH以外的其他变量名称,如:

MYPATH='/var/www/vhosts/staging\.example\.com'