I know I could just grep it for the arguments I'm looking for or so As mbiber said, source another file. For example, your config file say some. This file is used to set configuration options for GRUB, since grub-mkconfig is a shell script that sources it:. You could inadvertently break that more easily than a sourced file.
It's also recommended to check if the file exists before loading it because you don't want to continue running your script if a configuration file is not present. Obviously, I am not the bash specialist here, but the concept should not be different in whatever language you use:. In the example below, you can use a very basic script to either set a string, or print a string, as set in your config file:.
Of course, in a real applied script, you need to add a lot of stuff to make sure the arguments are correct, decide what to do when input is incorrect, settings file does not exist etc, but:. Ubuntu Community Ask! Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Learn more. Best way to read a config file in bash Ask Question. Whenever a Bash shell starts, its environment must be configured so that it is usable. There are five main files and one directory that are used to configure the Bash environment. They are listed here along with their main functions:. Try to trace the execution sequence through these files and determine which sequence it uses for a non-login Bash initialization versus a log-in Bash initialization.
This article explored sourcing for pulling code and variable assignments into a Bash program. This method of parsing variables from a configuration file is fast, easy, and flexible. It provides a method for separating Bash code from variable assignments to allow non-programmers to set the values of those variables.
Why use '. Parsing config files with Bash Opensource. Separating config files from code enables anyone to change their configurations without any special programming skills. Image by :. Get the highlights in your inbox every week. Programming and development. Sourced code and variables echo "This is the sourced code from the data file.
What is a config file? It specifies all defaults in a separate file, to avoid the need for a huge, cluttered, duplicated "defaults" config function in all of your shell scripts. And it lets you choose between reading with or without default fallbacks:.
In the shell script the "application" , this is what I do to get at the username more or less, I've put it in a shell function :. On some systems, it's installed as xml. The most common, efficient and correct way is to use source , or. For example:. Something to consider, however, is the security issues that using an additional externally-sourced configuration file can raise, given that additional code can be inserted.
Anything else works. Most users, although not many containers, already have the git binary. Why not therefore use git config for application configuration management using a dedicated non-conflicting configuration file as in the examples below? For additional commands, see its man page. It is wise to first ensure the configuration file exists:. This one seems safe and short.
Feel free to crack this ruthlessly. I'd like to know of a better way. For my scenario, source or. It sources this altered version of the config file and continues on. Future work could make the sed script more robust, filter out lines that look weird or aren't definitions, etc, not break on end of line comments - but this is good enough for me for now.
I filter for lines that look like an expected parameter definition, then use export to bring them in other variable-defining keywords don't seem to work with this method, unfortunately. Ignores all lines that don't match what it's looking for, is compact, easily extended and I think secure against malicious config files. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Learn more. Use config file for my shell script Ask Question. Asked 7 years ago. Active 2 months ago. Viewed k times. I can confirm this script fails if there's a in the value. I do in this way. If someone can do this, so, the system admin are sloppy. Zombo Zombo 1. Yordan Georgiev Yordan Georgiev 4, 1 1 gold badge 45 45 silver badges 52 52 bronze badges. Rich Armstrong Rich Armstrong 26 4 4 bronze badges. For example, "GNU bash, version 3.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
0コメント