Print all cfg files linux

Best way to read a config file in bash

What is the best way to read a config file in bash?
For example, you have a script and aren’t willing to fill in all the config manually each time you call the script. Edit 1: I think I didn’t make it clear, so: what I want is. I have a configfile which is like

variable_name value variable_name value . 

and I want to read that. I know I could just grep it for the arguments I’m looking for or so. but maybe there’s a more intelligent way 🙂

5 Answers 5

As mbiber said, source another file. For example, your config file (say some.config ) would be:

And your script could look like:

#! /bin/bash # Optionally, set default values # var1="default value for var1" # var1="default value for var2" . /path/to/some.config echo "$var1" "$var2" 

The many files in /etc/default usually serve as configuration files for other shell scripts in a similar way. A very common example from posts here is /etc/default/grub . This file is used to set configuration options for GRUB, since grub-mkconfig is a shell script that sources it:

sysconfdir="/etc" #… if test -f $/default/grub ; then . $/default/grub fi 

If you really must process configuration of the form:

var1 some value 1 var2 some value 2 

Then you could do something like:

while read var value do export "$var"="$value" done < /path/to/some.config 

(You could also do something like eval "$var=$value" , but that's riskier than sourcing a script. You could inadvertently break that more easily than a sourced file.)

@IcyIcyIce That's how it's supposed to be. You can use shell code in the various files in /etc/default - and mostly to good use. If your clueless user has access to configuration for something running as root, it's already a lost cause.

@IcyIcyIce get your teachers to clarify what they want. A part of learning to develop software is getting to know the requirements clearly.

Use source or . to load in a file.

Читайте также:  Linux find process with pid

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:

An example

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:

#!/bin/bash # argument to set a new string or print the set string arg=$1 # possible string as second argument string=$2 # path to your config file configfile="$HOME/stringfile" # if the argument is: "set", write the string (second argument) to a file if [ "$arg" == "set" ] then echo "$string" > $configfile # if the argunment is "print": print out the set string, as defined in your file elif [ "$arg" == "print" ] then echo "$( cat $configfile )" fi 

Then

$ '/home/jacob/Bureaublad/test.sh' set "Een aap op een fiets, hoe vind je zoiets?" 
$ '/home/jacob/Bureaublad/test.sh' print Een aap op een fiets, hoe vind je zoiets? 

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:

Источник

Лучший способ прочитать конфигурационный файл в bash

Каков наилучший способ прочитать файл конфигурации в bash? Например, у вас есть сценарий и вы не хотите заполнять все настройки вручную каждый раз, когда вы вызываете скрипт.

Редактирование 1: Я думаю, что я не дал понять, что: я хочу . У меня есть файл configfile, похожий на

variable_name value variable_name value . 

, и я хочу прочитайте это. Я знаю, что я мог бы просто grep его для аргументов, которые я ищу, или так . но, возможно, есть более интеллектуальный способ:)

4 ответа

Очевидно, что я не специалист bash здесь, но концепция не должна отличаться на любом используемом вами языке:

Пример

В приведенном ниже примере вы может использовать (очень) базовый скрипт либо установить строку, либо напечатать строку, как указано в вашем файле конфигурации:

#!/bin/bash # argument to set a new string or print the set string arg=$1 # possible string as second argument string=$2 # path to your config file configfile="$HOME/stringfile" # if the argument is: "set", write the string (second argument) to a file if [ "$arg" == "set" ] then echo "$string" > $configfile # if the argunment is "print": print out the set string, as defined in your file elif [ "$arg" == "print" ] then echo "$( cat $configfile )" fi 

Then

$ '/home/jacob/Bureaublad/test.sh' set "Een aap op een fiets, hoe vind je zoiets?" 
$ '/home/jacob/Bureaublad/test.sh' print Een aap op een fiets, hoe vind je zoiets? 

Конечно, в реальном прикладном скрипте вам нужно добавить много материала для убедитесь, что аргументы верны, решите, что делать, если ввод неверный, файл настроек не существует и т. д., но:

Читайте также:  File table in linux

Источник

How to list all configuration files for an already installed package?

I am running Ubuntu 12.10 and, if possible, I would like to get the list of configuration files available for a given package that is already installed. I would prefer to do this using dpkg or apt if possible, but no problem if other tools such as aptitude or dselect are needed/recommended.

3 Answers 3

There's no need to use anything other than cat , if I understood your intention correctly:

cat /var/lib/dpkg/info/.conffiles 

should give you what you're after. For instance for package zsh:

% cat /var/lib/dpkg/info/zsh.conffiles /etc/zsh/zlogin /etc/zsh/zlogout /etc/zsh/zprofile /etc/zsh/zshenv /etc/zsh/zshrc /etc/zsh/newuser.zshrc.recommended 

Regarding a case where there's no such file for a given package - it's up to the package maintainer to designate certain files as configuration. If this hasn't been done properly, you should file a bug where appropriate.

In such cases you have a couple of options.

    List files belonging to the package that are in /etc/:

apt-get source package less package-x.y.z/debian/rules 

If a given package is not having such a file .conffiles under this path, does it mean it is no using configuration files?

Not necessarily - it might be that there are indeed configuration files used by the package but the packager did not designate any files as part of the packaged application's configuration files.

The .conffiles file will list all configuration files that ship with the package and are marked as such by the packager. An automatically-installed one that does not appear there is a packaging bug, but be aware that some files (particularly user-specific configuration in a user's dotfiles) cannot ship with the package and can't really be automatically located unless you already know what they are.

Читайте также:  Linux and windows on laptop

Let's for example test the package apt to get the config file(s).

It can be tricky to understand what .conffiles you should check to see the info so I suggest using grep to find the clue.

locate *.conffiles | grep apt /var/lib/dpkg/info/apt-config-icons.conffiles /var/lib/dpkg/info/apt.conffiles /var/lib/dpkg/info/aptdaemon.conffiles /var/lib/dpkg/info/apturl-common.conffiles /var/lib/dpkg/info/libatk-adaptor:amd64.conffiles 

And to cat any of these in particular if you are interested according to Marcin's Kaminski answer.

Another trick is to read the manual, for instance man apt will lead you to SEE ALSO section from where you can call man apt.conf where you will see the location of the config file for apt in this case: /etc/apt/apt.conf .

However, config file /etc/apt/apt.conf may not even exist. Be aware of that when searching for the config files.

In Linux config files should be inside the /etc/ directory. You may use dpkg-query -L your_package | grep etc to list all package files and directories inside the /etc/ directory.

How about dpkg -S [package-name]? Cant see it being mentioned here.

root@homehub:/# dpkg -S nginx nginx-common: /usr/share/nginx/html nginx-full: /usr/share/man/man8/nginx.8.gz libnginx-mod-http-xslt-filter: /usr/share/doc/libnginx-mod-http-xslt-filter/copyright nginx-common: /etc/logrotate.d/nginx libnginx-mod-mail: /usr/share/doc/libnginx-mod-mail/changelog.gz libnginx-mod-http-upstream-fair: /usr/share/doc/libnginx-mod-http-upstream-fair libnginx-mod-http-auth-pam: /usr/share/doc/libnginx-mod-http-auth-pam/changelog.Debian.gz libnginx-mod-http-image-filter: /usr/lib/nginx/modules/ngx_http_image_filter_module.so nginx-common: /etc/nginx/snippets libnginx-mod-mail: /usr/share/doc/libnginx-mod-mail/copyright nginx-common: /etc/nginx/snippets/fastcgi-php.conf libnginx-mod-http-xslt-filter: /usr/share/doc/libnginx-mod-http-xslt-filter/changelog.Debian.gz nginx-full: /usr/share/doc/nginx-full/copyright nginx-common: /usr/share/doc/nginx-common nginx-common: /etc/ufw/applications.d/nginx libnginx-mod-http-upstream-fair: /usr/share/doc/libnginx-mod-http-upstream-fair/changelog.gz libnginx-mod-stream: /usr/share/nginx/modules-available/mod-stream.conf nginx-common: /etc/nginx/sites-enabled libnginx-mod-http-upstream-fair: /usr/share/doc/libnginx-mod-http-upstream-fair/copyright libnginx-mod-http-subs-filter: /usr/share/doc/libnginx-mod-http-subs-filter/changelog.gz libnginx-mod-http-dav-ext: /usr/share/nginx/modules-available/mod-http-dav-ext.conf nginx-common: /etc/nginx/koi-utf nginx-common: /etc/nginx/mime.types nginx-common: /usr/share/vim/addons/syntax/nginx.vim nginx-common: /usr/share/doc/nginx-common/NEWS.Debian.gz nginx-common: /usr/share/doc/nginx-common/README.Debian nginx-full: /usr/share/doc/nginx-full libnginx-mod-mail: /usr/share/nginx/modules-available/mod-mail.conf libnginx-mod-http-auth-pam: /usr/share/doc/libnginx-mod-http-auth-pam/changelog.gz nginx: /usr/share/doc/nginx nginx-common: /etc/nginx/win-utf libnginx-mod-http-dav-ext: /usr/share/doc/libnginx-mod-http-dav-ext . 

And sometimes depending on filename structure it might be good if piped out to grep names containing ".conf":

root@homehub:/# dpkg -S nginx |grep ".conf" nginx-common: /etc/nginx/snippets/fastcgi-php.conf libnginx-mod-stream: /usr/share/nginx/modules-available/mod-stream.conf libnginx-mod-http-dav-ext: /usr/share/nginx/modules-available/mod-http-dav-ext.conf libnginx-mod-mail: /usr/share/nginx/modules-available/mod-mail.conf libnginx-mod-http-auth-pam: /usr/share/nginx/modules-available/mod-http-auth-pam.conf nginx-common: /etc/nginx/fastcgi.conf nginx-common: /etc/init/nginx.conf nginx-common: /etc/nginx/conf.d libnginx-mod-http-subs-filter: /usr/share/nginx/modules-available/mod-http-subs-filter.conf libnginx-mod-http-geoip: /usr/share/nginx/modules-available/mod-http-geoip.conf libnginx-mod-http-echo: /usr/share/nginx/modules-available/mod-http-echo.conf libnginx-mod-http-upstream-fair: /usr/share/nginx/modules-available/mod-http-upstream-fair.conf nginx-common: /etc/init/nginx.conf libnginx-mod-http-xslt-filter: /usr/share/nginx/modules-available/mod-http-xslt-filter.conf nginx-common: /etc/nginx/snippets/snakeoil.conf nginx-common: /etc/nginx/nginx.conf libnginx-mod-http-image-filter: /usr/share/nginx/modules-available/mod-http-image-filter.conf 

Источник

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