【问题标题】:import variables into bash script from a config file将变量从配置文件导入 bash 脚本
【发布时间】:2017-08-07 12:08:02
【问题描述】:

我想从 config.cfg 文件中导入用户名、密码、主机名等变量,以便我可以在 bash 脚本文件 script.sh 中使用它们

user = ""
password = ""
host = ""

我该怎么做,以便在 script.sh 文件中使用在 config.cfg 文件中设置的凭据在脚本中执行以下操作:

mysqldump --user="$user" --password="$password" --host="$host" $db $tabl > "./backups/$db/$db-$tabl-$time"

【问题讨论】:

    标签: mysql bash shell


    【解决方案1】:

    删除配置文件中 = 符号周围的空格,从脚本中获取它,并使用如此定义的 shell 变量:

    source config.cfg
    echo "$user"
    

    【讨论】:

      【解决方案2】:

      你必须来源它。在bash 中,您可以使用source. 来做到这一点,它们是等价的。例如:

      source config.cfg
      

      . config.cfg
      

      编辑

      正如在另一个答案中正确指出的那样,您需要删除 = 周围的空格,以便您的 config.cfgbash 中工作。

      【讨论】:

      • 只是重申。该命令必须是bash <scriptname.sh>,而不是sh <scriptname.sh> .. 感谢您指出这一点!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多