bash脚本 - 如何引用变量的文件 [英] Bash script - How to reference a file for variables

查看:117
本文介绍了bash脚本 - 如何引用变量的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想打电话给一个变量的设置文件,我怎么能在bash做到这一点?

Hi I want to call a settings file for a variable, how can I do this in bash?

因此​​,设置文件将定义变量(例如:CONFIG.FILE):

So the settings file will define the variables (eg: CONFIG.FILE) :

production="liveschool_joe"
playschool="playschool_joe"

和脚本将使用这些变量的原因

And the script will use those variables in it

#!/bin/bash
production="/REFERENCE/TO/CONFIG.FILE"
playschool="/REFERENCE/TO/CONFIG.FILE"
sudo -u wwwrun svn up /srv/www/htdocs/$production
sudo -u wwwrun svn up /srv/www/htdocs/$playschool

如何才能庆典做这样的事情?我将不得不使用AWK / SED等...?

How can I get bash to do something like that? Will I have to use awk/sed etc...?

推荐答案

使用命令。

例如:

#!/usr/bin/env bash
production="liveschool_joe"
playschool="playschool_joe"
echo $playschool

script.sh

#!/usr/bin/env bash
source config.sh
echo $production

请注意,从 SH ./script.sh 在这个例子中,输出是:

Note that the output from sh ./script.sh in this example is:

~$ sh ./script.sh 
playschool_joe
liveschool_joe

这是因为命令实际上运行该程序。在一切 config.sh 执行

This is because the source command actually runs the program. Everything in config.sh is executed.

您可以使用内置的导出命令,并获取和设置环境变量也能做到这一点。

You could use the built-in export command and getting and setting "environment variables" can also accomplish this.

运行导出回声$ ENV 应该是所有你需要知道的关于访问的变量。访问环境变量,做的方式作为一个局部变量一样。

Running export and echo $ENV should be all you need to know about accessing variables. Accessing environment variables is done the same way as a local variable.

要设置他们,说:

export variable=value

在命令行。所有脚本都可以访问这个值。

at the command line. All scripts will be able to access this value.

这篇关于bash脚本 - 如何引用变量的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆