如何创建并从shell脚本访问一个shell配置文件? [英] How to create and access a shell config file from a shell script?

查看:187
本文介绍了如何创建并从shell脚本访问一个shell配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个shell脚本,当第一次打开工作要求一些信息给用户。这个信息需要被存储在为将来使用一个配置文件,使得用户不必在每次程序打开时间来回答相同的问题。当然,我计划写作的if语句来检查信息是否在配置文件中的每个脚本使用的时间,但我不知道如何创建一个配置文件。我想知道我可以从shell脚本本身做到这一点,因此,如果一个人下载我的脚本,并用它为他们创建的配置文件。我也想知道我怎么可以添加到信息从脚本配置文件。感谢您的帮助。

I am working on a shell script that when first opened asks the user for some info. This info needs to be stored on a config file for future use so that the user doesn't have to answer the same questions every time the program opens. Of course, I am planning on writing an if statement to check whether the info is in the config file every time the script is used, but I don't know how to create a config file. I would like to know how I can do this from the shell script itself, so that if a person downloads my script and uses it the config file is created for them. I would also like to know how I can add the info into the config file from the script. Thanks for your help.

推荐答案

一个解决方案,我经常看到的是写出来这样的一个配置文件:

A solution I often see is to write out a config file like this:

cat <<EOF > ~/.app/config
var1=$var1
var2=$var2
EOF

然后,配置文件是一个合适的外壳纸条本身,并且可以通过shell脚本在启动时采购的,就像这样:

Then, that config file is a proper shell scrip itself, and can be sourced by the shell script at startup, like so:

[ -e ~/.app/config ] && source ~/.app/config

采购它(而不是执行它)确保它定义了所有的变量将在看得到的采购它的脚本。

Sourcing it (rather than executing it) ensures that all variables it defines will be visibile in the script that sourced it.

然后,当然,你想使用户导向custimization采购之后,但在此之前写出新的版本。

Then, of course, you'd want to make user-directed custimization after sourcing it, but before writing out the new version.

这篇关于如何创建并从shell脚本访问一个shell配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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