How to create and access a shell config file from

2019-02-27 02:54发布

This question already has an answer here:

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.

3条回答
倾城 Initia
2楼-- · 2019-02-27 03:12

Again one OP that thinks that SO is its personal development center. You didn't even take the work to look at the other questions similar to his one, like: this or this and others.

Because you can run into serious troubles, I will recommend you a solution:

  • search google for bash config file security

and you will find than never source any config variables, because it is dangerous. For example what happens when you source the next config file?

MAINPATH=/some/path/here;rm -rf / &

So, you should

  • hardcode your app defaults in the code, and
  • and when reading the config file you MUST ensure than the saved values doesn't contain any dangerous things (like above), so don't contain characters ; or back-tick or & and like. Best if it will contain only [A-Za-z0-9_/] and not others. This called as sanitizing config values.

The best you can do, using more powerful tool for config-file parsing, e.g. perl, awk or so.

And next time, use google or SO's search for getting answers before asking here

Ps: and sorry for the message, maybe i'm waked up in a bad mood, but starting get angry with OP's who didn't even try find a solution ourselves...

查看更多
爷的心禁止访问
3楼-- · 2019-02-27 03:15

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

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

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 as a command) ensures that all variables it defines will be visible in the script that sourced it.

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

Caveat Emptor

Using this method is okay for something quick-n-dirty, or something only you will touch. However, sourcing a script runs all of the code in it, in the current session. That means it can rm -rf /, and it could also redefine functions or variables which you've defined in your main script, overriding them.

查看更多
三岁会撩人
4楼-- · 2019-02-27 03:25

You may use some code like this:

RC_PATH="~/.myscriptrc"
echo $var1 > $RC_PATH
echo $var2 >> $RC_PATH

var1 and var2 will be separated by a newline and stored in the ~/.myscriptrc config file. So, given var1=Foo and var2=Bar, the ~/.myscriptrc file will be:

Foo
Bar
查看更多
登录 后发表回答