Read configuration file linux

How to Read Config Files with Section in Bash Shell

How to read config files with section in bash shell

To use —exclude-from you will have to isolate the relevant section of the config into a temporary file. This is easy to do with a bit of sed:

tmp_file=$(mktemp)
sed -n '1,/rsync_exclude/d;/\[/,$d;/^$/d;p' config.file > $tmp_file
rsync -avz --exclude-from $tmp_file source/ destination/

I am omitting error checking and cleanup for clarity.

Note that rsync can read the exclude pattern from the stdin for an — input, so this is even shorter:

sed -n '1,/rsync_exclude/d;/\[/,$d;/^$/d;p' config.file | \ 
rsync -avz --exclude-from - source/ destination/

Explanation

  • The 1,/rsync_exclude/d excludes all lines up to the rsync_exclude section entry
  • The /\[/,$d excludes everything from the start of the next section to the end of the file
  • The /^$/d excludes empty lines (this is optional)

All of the above extracts the relevant section from the config.

Parsing variables from config file in Bash

Just FYI, another pure bash solution

IFS=" Content of $name is $"
done

bash reading and writing a config file with an unusual layout

For the first question, pre-process your .cfg file and then read it, like this:

CONFIG_FILE=test.cfg
sed 's/ = /=/g' < test.cfg >/tmp/processed.cfg
. /tmp/processed.cfg

Now, all your name_1 , name_2 pairs will be available to your shell scripts.

For the second question, do it like this:

CONFIG_FILE=test.cfg
TARGET_KEY='name_1 '
REPLACEMENT_VALUE='"true" '
sed -i -e "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIG_FILE

Reading a config file from a shell script

You don’t want source it, so you should:

1.read the config, 2.verify lines 3.eval them

CONFIGFILE="/path/to/config"
echo "=$ADMIN= =$TODO= =$FILE $CONFIGFILE" | while read -r key val
do
#verify here
#.
str="$key='$val'"
echo "$str"
done)
echo =$ADMIN= =$TODO= =$FILE= #here are defined
ADMIN: root
TODO: delete

var=badly_formtatted_line_without_colon

#comment
FILE: /path/to/file

if you run the above sample should get (not tested):

sure this is not the best solution — maybe someone post a nicer one.

Edit a configuration file with sections using bash

Your current attempt has some problems.

After $1 you want at least one whitespace (you don’t want to match key2), use ‘+’.

You would like the keep first whitespace(s) after the replacement, put it in the match.

Maybe you get additional spaces after a short key, so put the spaces after the key in the match too.

You don’t want to match $ . With the backslash it is a character. You don’t need to match end-of-line, the .* will match everything until end-of-line.

How about a config file with a space in its name? Quote $3 .

Читайте также:  Устанавливаем htop на linux

And the \s doesn’t work in plain sed . Try -r .

I deleted the -i , so you can test without changing the file:

When you want to limit this code to a section, use /start/,/end/ . How do you know something is a header? In your example the header lines are called header, but that will not be the case in your real config file. When the headers look like [section] , change the solution beneath. The solution beneat assumes that all lines that don’t start with whitespace is a header.

# Set a configuration value 
# Args:
# 1) The config key
# 2) The new value
# 3) The config section header
# 4) The config file
function set_config_value() key="$1"
val="$2"
header="$3"
file="$4"
# first test this without the -i flag
sed -ir "/^$$/,/^[^\s]/ s/^(\s*$\s+).*$/\1$/" "$"
>

This solution should work for your sample config, but will fail when $ or $ has a special character (try key=/ ).

You should use a solution that doesn’t try to understand the strings given. My first thought would be awk , but make your own choice. Look at https://unix.stackexchange.com/q/137643/57293 .

how to parse a config file (*.conf) in shell script?

pw=$(awk '/^password/' app.conf)

user=$(awk '/^user/' app.conf)

echo $pw
root123

echo $user
root

The $() sets the variable pw to the output of the command inside. The command inside looks through your app.conf file for a line starting password and then prints the 3rd field in that line.

If you are going to parse a bunch of values out of your config file, I would make a variable for the config file name:

CONFIG=app.conf
pw=$(awk '/^password/' "$")
user=$(awk '/^user/' "$")

Here’s how to do the two different ports. by setting a flag to 1 when you come to the right section and exiting when you find the port.

mport=$(awk '/^\[MySQL\]/ f==1&&/^port/' "$")
sport=$(awk '/^\[Server\]/ f==1&&/^port/' "$")

bash: how to source array from section config file after sed?

source requires a file for an argument. Thus, replace:

source $(sed -n '1,/animals/d;/\[/,$d;/^$/d;p;' config)

The bash construct <(. ) is called process substitution. It creates a file-like object that source can read from. It is in contrast to $(. ) which is called command substitution which creates a string.

Источник

Parsing config files with Bash

Separating config files from code enables anyone to change their configurations without any special programming skills.

bash logo on green background

Keeping program configurations separate from code is important. It enables non-programmers to alter configurations without having to modify the program’s code. With compiled binary executables, that would be impossible for non-programmers because it not only requires access to source files (which we do have with open source programs) but also a programmer’s skill set. Few people have that, and most people don’t want to learn.

Programming and development

With shell languages such as Bash, source code access is available by definition since shell scripts are not compiled into binary formats. Despite that openness, it is not a particularly good idea for non-programmers to root around in shell scripts and alter them. Even knowledgeable developers and sysadmins can make accidental changes that cause errors or worse.

So placing configuration items into easily maintained text files provides separation and allows non-programmers to edit configuration elements without the danger of making unintentional changes to the code. Many developers do this for programs written in compiled languages because they don’t expect the users to be developers. For many of the same reasons, it also makes sense to do this with interpreted shell languages.

Читайте также:  Command to check connection in linux

The usual way

As with many other languages, you can write code for a Bash program that reads and parses ASCII text configuration files, reads the variable name, and sets values as the program code executes. For example, a configuration file might look like this:

var1=LinuxGeek46 var2=Opensource.com

The program would read that file, parse each line, and set the values into each variable.

Sourcing

Bash uses a much easier method for parsing and setting variables called sourcing. Sourcing an external file from an executable shell program is a simple method for including the content of that file into a shell program in its entirety. In one sense, this is very much like compiled language include statements that include library files at runtime. Such a file can include any type of Bash code, including variable assignments.

As usual, it is easier to demonstrate than to explain.

First, create a ~/bin directory (if it does not already exist), and make it the present working directory (PWD). The Linux Filesystem Hierarchical Standard defines ~/bin as the appropriate place for users to store their executable files.

Create a new file in this directory. Name it main and make it executable:

[dboth@david bin]$ touch main [dboth@david bin]$ chmod +x main [dboth@david bin]$

Add the following content to this executable file:

#!/bin/bash Name="LinuxGeek" echo $Name

And execute this Bash program:

[dboth@david bin]$ ./main LinuxGeek [dboth@david bin]$

Create a new file and call it ~/bin/data . This file does not need to be executable. Add the following information to it:

# Sourced code and variables echo "This is the sourced code from the data file." FirstName="David" LastName="Both"

Add three lines to the main program so that it looks like this:

#!/bin/bash Name="LinuxGeek" echo $Name source ~/bin/data echo "First name: $FirstName" echo "LastName: $LastName"
[dboth@david bin]$ ./main LinuxGeek This is the sourced code from the data file. First name: David LastName: Both [dboth@david bin]$

There is one more really cool thing to know about sourcing. You can use a single dot ( . ) as a shortcut for the source command. Change the main file to substitute the . in place of source :

#!/bin/bash Name="LinuxGeek" echo $Name . ~/bin/data echo "First name: $FirstName" echo "LastName: $LastName"

And run the program again. The result should be exactly the same as the previous run.

Starting Bash

Every Linux host that uses Bash—which is pretty much all of them since Bash is the default shell for all distributions—includes some excellent, built-in examples of sourcing.

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:

  • /etc/profile : System-wide environment and startup programs
  • /etc/bashrc : System-wide functions and aliases
  • /etc/profile.d/ : Directory that contains system-wide scripts for configuring various command-line tools such as vim and mc and any custom configuration scripts a sysadmin creates
  • ~/.bash_profile : User-specific environment and startup programs
  • ~/.bashrc : User-specific aliases and functions
  • ~/.bash_logout : User-specific commands to execute when the user logs out
Читайте также:  Linux archive zip file

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. I did this in Chapter 17 of Volume 1 in my Linux training series, Using and administering Linux: Zero to sysadmin.

I’ll give you one hint. It all starts with the ~/.bashrc script.

Conclusion

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.

Computer screen with files or windows open

What is a config file?

There are several popular formats for configuration files, each with its own strengths. Find what works best for you.

Python programming language logo with question marks

Use Python to parse configuration files

The first step is choosing a configuration format: INI, JSON, YAML, or TOML.

Looking back with binoculars

Parse JSON config files with Groovy

Sidestep the debate on whether or not to use JSON as a configuration format and just learn how to parse it using Groovy.

bash logo on green background

Bash aliases you can’t live without

Tired of typing the same long commands over and over? Do you feel inefficient working on the command line? Bash aliases can make a world of difference.

Woman sitting in front of her computer

Parsing config files with Lua

Configure persistent application settings with the Lua programming language.

Источник

Reading configuration files in linux device driver

How to read configuration files in linux device driver? Experts say that reading and writing file in kernel space is a bad practise. For firmware download we have request_firmware kernel API. Is there a linux kernel API for reading and parsing configuration files for drivers? Eg: Reading baudrate and firmware file path for a particular driver.

There are many ways to configure devices, such as autoconfiguration, ioctl s, or module parameters. What exactly do you want to configure?

I am developing a driver to interact with UART. Need to set baudrate and firmware file location as configurable parameters to driver module. I wanted to explore if there was a Kernel interface for this purpose.

1 Answer 1

Most of the times doing file i/o from kernel space is discouraged, but if you still want the way to read files from kernel space, kernel provides a good interface to open and read files from kernel. Here is an example module.

 /* * read_file.c - A Module to read a file from Kernel Space */ #include #include #define PATH "/home/knare/test.c" int mod_init(void) < struct file *fp; char buf[512]; int offset = 0; int ret, i; /*open the file in read mode*/ fp = filp_open(PATH, O_RDONLY, 0); if (IS_ERR(fp)) < printk("Cannot open the file %ld\n", PTR_ERR(fp)); return -1; >printk("Opened the file successfully\n"); /*Read the data to the end of the file*/ while (1) < ret = kernel_read(fp, offset, buf, 512); if (ret >0) < for (i = 0; i < ret; i++) printk("%c", buf[i]); offset += ret; >else break; > filp_close(fp, NULL); return 0; > void mod_exit(void) < >module_init(mod_init); module_exit(mod_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Knare Technologies (www.knare.org)"); MODULE_DESCRIPTION("Module to read a file from kernel space"); 

I tested this module on linux-3.2 kernel. I used printk() function to print the data, but it will not be your actual case, this is just shown as an example.

Источник

Оцените статью
Adblock
detector