Creating a config file
I want to create a Bash script that creates config files. I want this file to be able to be called by another script that sets parameters in the config file. (For additional but unrelated info to keep in mind, the Bash script also runs a Matlab program that reads in that config file). So for example, I want to produce a config file with: Variable1 = 52 Variable2 = 77. I want a script, say «MainScript» that physically produces such a config file, and the variables are set by another script «CallScript» that calls MainScript. How do I go about implementing this?
1 Answer 1
Don’t know whether namely «CallScript» that calls «MainScript» is needed, but here is a script, which can do what is required (say it would be «MainScript»; if you’d like, you can put its calling: bash MainScript.sh — into another file, say «CallScript»).
Light-version
$ cat MainScript.sh #!/bin/bash CFG_FILE="matlab.cfg" function make_config() < arr=("$@") echo -n "" >$CFG_FILE for i in "$"; do echo "Variable$((i+1)) = $" >> $CFG_FILE; done > make_config $@
$ cat CallScript.sh bash MainScript.sh 1 11 27 23 44
Now change permissions of the «CallScript.sh»:
And you can launch it in a such way:
to produce a config file like this:
$ cat matlab.cfg Variable1 = 1 Variable2 = 11 Variable3 = 27 Variable4 = 23 Variable5 = 44
More complicated version
On the other hand, if you want to have (pre-defined) different custom names of variables, you can do smth like this:
$ cat MainScript.sh #!/bin/bash CFG_FILE="matlab.cfg" MLAB_VARS=("CustomVariable1" "CustomVariable2" "VeryCustomVariable11") function make_config() < arr=("$@") var_length=$entered_length=$# [ $var_length -gt $entered_length ] && \ echo "Please enter more than or equal $var_length number of values for setting variables" && \ usage && exit 1 echo -n "" > $CFG_FILE for i in "$"; do echo "$ = $" >> $CFG_FILE; done > function usage() < cat make_config $@
bash MainScript.sh 1 11 27
$ cat matlab.cfg CustomVariable1 = 1 CustomVariable2 = 11 VeryCustomVariable11 = 27
$ bash MainScript.sh 1 11 Please enter more than or equal 3 number of values for setting variables Usage: MainScript.sh [value [value [value [. ]]]] where each value will be used to set corresponding variable
Complex version
If you want to just pass all map of variables-values dynamically, you can use following approach:
$ cat MainScript.sh #!/bin/bash CFG_FILE="matlab.cfg" usage() < cat make_config() < arrays_str="$" array_var="$" # create new associative array with varname-values pairs from string eval "declare -A mlab_vars=$" array_ord="$" # create new array with vars orders from string eval "declare -a mlab_ords=$" echo -n "" > $CFG_FILE for key in "$"; do echo "$key" = "$" >> $CFG_FILE; done > declare -A var_array declare -a var_order for arg in "$@" do case $arg in -*=*|--*=*) arg_name="$" arg_name="$"; arg_name="$" var_array["$"]="$" var_order+=( "$" ) shift # past argument=value ;; h|--help) usage exit 0 ;; *) # unknown option shift ;; esac done # convert associative array to string assoc_array_string="$(declare -p var_array)" assoc_array_orders="$(declare -p var_order)" make_config "$assoc_array_orders # $assoc_array_string"
$ bash MainScript.sh --CustomVar=1230 --Var1=5613 --Var2="MatLab Rabbit" --Var3="1328"
$ cat matlab.cfg CustomVar = 1230 Var1 = 5613 Var2 = MatLab Rabbit Var3 = 1328
Although, don't forget to check your bash version:
It must be at least version 4 to have associative arrays
How do I create a .conf file?
I found an answer which stated I needed to create the file /etc/modprobe.d/local-b43.conf I don't know how to do this. At the moment it will boot the system without the Network Manager which I can then manually switch on by typing:
gksu service network-manager start
I believe it is suggesting that you add it to boot options, however, let's see if there is a fixable problem with your Broadcom wireless device. Please post: lspci -nn | grep 0280 Is your wireless working? Welcome to askubuntu!
Thank you @chili555 when I put in lspci-nn|grep0280 it says 'command not found' I'm sorry, I am new to all of this. Yes, my wireless is working after I put gksu service network-manager start in the terminal. I can also connect using Wicd Network Manager. But On Start Up It still says 'Booting System without Network Configuration'
2 Answers 2
I don't think your system in helped by having both Network Manager and Wicd competing for control. I recommend you remove Wicd. Let's create the .conf file. In a terminal:
gksudo gedit /etc/modprobe.d/local-b43.conf
Use nano or kate or leafpad if you haven't the text editor gedit. Add a single line:
options b43 allhwsupport=1
Proofread carefully, save and close the text editor. Reboot.
If it still doesn't work as expected, from the terminal:
sudo -i rm /etc/modprobe.d/local-b43.conf echo "blacklist b43" >> /etc/modprobe.d/blacklist.conf echo "blacklist ssb" >> /etc/modprobe.d/blacklist.conf exit
Reboot and tell us the result.
Your /etc/network/interfaces asks eth0 to start automatically, however, you are using wireless. Please do:
gksudo gedit /etc/network/interfaces
Change the file to comment out the eth0 stanza:
auto lo iface lo inet loopback #auto eth0 #iface eth0 inet dhcp
Proofread carefully, save and close the text editor. Reboot and test.
How do I create a configure script?
This may sound like a very generic question but here it goes. I have a requirement to create a configure script for my application, the result of this configure would be a generated makefile (basic configure , make , make install ). My question is, where do I start in building this? Is there an example I can follow?
4 Answers 4
To create the standard "configure" script you need GNU autoconf. You may need GNU automake and libtool too.
There are tons of documentation and howtos. Google for something like "autoconf automake howto". The good documentation is in the official manual pages:
- Autoconf: http://www.gnu.org/software/autoconf/
- Automake: http://www.gnu.org/software/automake/automake.html
- Libtool: http://www.gnu.org/software/libtool/libtool.html
Autoconf will create your configure script starting from the "configure.ac" file. The "Makefile.am" file will instruct automake on how to create your makefile by the configure string. Libtool is needed to simplify libraries handling around your code.
You can start creating a configure.ac file by hand or you may use the "autoscan" helper that may help you to create something semi-automatic for you.
Then, when you are ready, this one will do the magic:
GNU docs imply there could be other configure script tools : gnu.org/prep/standards/html_node/Makefile-Conventions.html , Are there any such other tools (apart from manual of course)?
Sometimes a software product will ship with no configure script. Look for an autogen.sh script. it will probably run:
aclocal || die "aclocal failed" automake --add-missing --force-missing --copy --foreign || die "automake failed" autoreconf || die "autoreconf failed"
All of these answers are about autoconf which is a GNU tool. However, configure shell scripts have been in UNIX long before GNU and can be created by hand or in other ways.
What does configure do?
configure is a just shell script. It's job is to generate a list of configuration variables that the makefile needs to build the software on that particular system. For example, it might determine the linker flags and header search paths needed to include a specific library.
configure can also accept options from the user, for example to control whether to build in debug or release mode.
Typically configure writes these variables to a file called config.mk which is included by the makefile . It may also generate a header config.h with some preprocessor defines. Note that configure is just a helpful automation for producing this. The user can always just hand edit config.mk themselves (especially on obscure systems).
How does configure detect features?
configure uses a variety of techniques to locate dependencies and detect system or hardware features. The least stable (but sometimes necessary way) is to check the uname to detect and operating system. One useful tool is pkg-config which can tell you where to find various installed libraries.
Lastly, configure scripts can always generate small snippets of code, and then trying to compile them to see if a feature is available.
Joe Nelson has a great article with examples for each of these ideas.
Should I write my own configure or use autoconf?
Most programs only have 1 or 2 small things they need to detect to get working. I think it makes sense to write a configure script in these cases, rather than try to figure out corner cases of the massive piece of software that is autotools. If you product a simple config.mk it can always be fixed by hand, and users of various systems will be helpful in getting your configure to work correctly.
For more complex dependencies, autoconf is probably useful.
To be fair to all parties, let's relate the argument made in autoconf 's documentation:
The primary goal of Autoconf is making the user’s life easier; making the maintainer’s life easier is only a secondary goal.
Autoconf is highly successful at its goal—most complaints to the Autoconf list are about difficulties in writing Autoconf input, and not in the behavior of the resulting configure. Even packages that don’t use Autoconf will generally provide a configure script, and the most common complaint about these alternative home-grown scripts is that they fail to meet one or more of the GNU Coding Standards (see Configuration in The GNU Coding Standards) that users have come to expect from Autoconf-generated configure scripts.
- your configure might not have all the commands some users expect.
- You might detect features incorrectly or using incorrect assumptions for example if macOS < use mac commands >else < use linux commands >, instead of if gnuTools < use gnu commands >else < use bsd/posix commands >.
You will have to determine whether that's important to you.