如何使用Bash引用文件中的变量? [英] How to reference a file for variables using Bash?

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

问题描述

我想调用一个变量的设置文件,如何在bash中执行此操作?

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

我如何让bash做类似的事情?我需要使用awk/sed等吗?

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

推荐答案

简短答案

使用source命令.

例如:

#!/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

这是因为source命令实际上运行程序. config.sh中的所有内容均已执行.

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

您可以使用内置的export命令,获取和设置环境变量"也可以完成此操作.

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

运行exportecho $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天全站免登陆