Open config 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.

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:

Читайте также:  Linux starting network manager

Источник

Open CFG File

All Linux programs have an executable file with operational codes that the CPU executes to perform specific tasks. The behavior of almost any linux program can be customized according to your preferences or needs by modifying the program configuration files.

Standard configuration file format in Linux

In Linux, each programmer is free to choose the configuration file format they prefer. The formatting options range from the 'shells' file, which contains a list of possible login shells separated by a new line, to the complex 'httpd.conf' Apache file with web server configuration.

System configuration files

The Linux system makes extensive use of the configuration files, regardless of their extension. Although it is more common to find the CONF files, you can also find, in certain occasions, CFG files. Below is an abbreviated list of processes and situations where Linux makes use of configuration files:

Access files


  • Booting and login/logout
  • File system
  • System administration
  • Networking
  • Daemons

It is very common task in Linux to manually edit configuration files. Typically, configuration files are stored in subdirectories as text files.

  • Gedit Editor
  • Nano Editor
  • Vim Editor
  • Gvim Editor
  • Emacs Editor
  • Leaf Pad Editor

For example, to edit the web configuration .conf file you must do the following procedure:

Step 1: Locate the file. Location will depend on the distribution and initial location of the Apache server installation. In our case of Debian Linux it is located in:


    Double click on it and open with 'Leaf Pad'.

  • Use a terminal, and type $ sudo nano javascript-common.conf Note that the Nano editor has a menu that can be seen at the bottom.
  • Step 3: Once finished editing, close and save the file keeping its extension.
  • The configuration files in Linux are extremely important and special care must be taken when modifying them, especially the system configuration files.

    Источник

    How to Edit Config Files in Ubuntu

    Ubuntu Edit Config File

    Configuration files contain the information needed to control the operation of a program. Linux users, especially Linux administrators, need to know how to edit configuration files to maintain an Ubuntu server or desktop. Configuration files on Ubuntu are stored in subdirectories of the /etc folder as text files and can be edited with text editors. Some examples of configuration files are /etc/hosts, host.conf, hosts. resolv.conf, /etc/networks, syslog.conf, etc. in Linux-based operating systems.

    To make configuration changes, you need to open the file with a text editor and edit it. In this article, I describe several ways to edit configuration files in Ubuntu. The steps described in this guide have been tested on Ubuntu 22.04, but will work on other Ubuntu versions as well.

    • Using Gedit Editor
    • Using Nano Editor
    • Using Vim Editor
    • Using Gvim Editor
    • Using Emacs Editor

    Now let’s take a look at these text editors. By using these editors, you can easily edit files in the terminal .

    Using Gedit Editor

    Gedit is the default text editor of the Gnome desktop environment. It supports tabs so you can open different files in multiple tabs. It is a simpler and easier text editor as compared to other available text editors.

    To edit a config file in the text editor, launch Terminal by pressing Ctrl+Alt+T key combinations. Then type the below command as sudo:

    $ sudo gedit /path/to/filename

    Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password.

    Example: editing of the /etc/resolv.conf file

    Edit file with gedit

    It is like the notepad editor in Windows OS. As soon as the editor window opens, you can start typing or editing without entering in any mode. Once you have done with the editing, press Ctrl+S to save the file.

    Editing configuration file with GEdit

    Using Nano Editor

    Nano is the simplest and the most powerful built-in editor in the many Linux distributions. You do not have to learn the Nano editor before using it and also there is no need to remember the commands. They are displayed at the bottom and can be triggered with the Ctrl key. For instance to save a file, press Ctrl+O.

    To edit any config file, simply open the Terminal window by pressing the Ctrl+Alt+T key combinations. Navigate to the directory where the file is placed. Then type nano followed by the filename that you want to edit.

    Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password.

    edit file in terminal

    Now you can edit and make changes in the config file using nano editor.

    Edit config file in nano editor

    Once you have done editing, press Ctrl+O to save and Ctrl+X to exit the Editor.

    Using Vim Editor

    Vim or Vi editor also comes pre-installed with most Linux distributions. It has no visible prompts or control that can be a little harder for beginners. However, once you learned it, it is a more quick and powerful tool for editing. Using Vim, you can edit and view multiple files in one session.

    To open a file in Vim editor, open the Terminal window by pressing the Ctrl+Alt+T key combinations. Then type vi as sudo followed by filename along with the file path like:

    Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter sudo password.

    Edit file in Vi editor

    Then edit the file using Vim editor. Note that when the editor opens, you enter command mode. Here are the few steps to edit a file in Vim editor:

    • To edit the file, Press I from the keyboard to enter into insert mode, here you can do editing just like the normal editor.
    • When done with editing, get out of this mode by pressing Esc. It will return you to normal mode.
    • To save the file, type :w in command mode
    • To quit the editor, type :q in command mode
    • To combine the above actions type :wq to save and quit simultaneously.

    Using Vi File Editor on Ubuntu

    Using Gvim Editor

    Gvim is a Vim editor with a graphical user interface. To install Gvim, launch Terminal by pressing Ctrl+Alt+T. then type the below command in Terminal to start the installation.

    $ sudo apt-get install vim-gnome

    Install Gvim

    Once done with the installation, you can open and edit any config file in it. Type the below command with sudo in Terminal to open any config file in Gvim:

    Edit file in linux terminal using GVim

    Press I to enter in insert mode where you can edit the file.

    GVim file editor

    In Gvim editor, you can do various functions using the toolbar just like any normal GUI application like saving the file, undo, redo, finding a keyword, etc.

    Using Emacs Editor

    Emacs is also a text editor but it is not a built-in editor. You have to manually install it using the Ubuntu Terminal. It has so many features including a built-in web browser, calculator, calendar, Terminal, etc.

    To install Emacs editor, launch Terminal by pressing Ctrl+Alt+T key combinations. Type the below command in Terminal as sudo:

    Install Emacs editor

    Once done with the installation, you can open and edit any config file in it.

    To open a config file using the emacs editor, type emacs followed by filename along with the file path in the below syntax:

    $ sudo emacs /path/to/filename

    Replace /path/to/filename with the actual file path of the configuration file that you want to edit. When prompted for a password, enter the sudo password.

    Editing text files in Emacs

    Edit the file as normal and save it using the toolbar.

    Emacs editor on Ubuntu Linux

    You can also open the config file in bash instead of a separate window.

    Type the following command as sudo in Terminal:

    Using emacs shell mode

    Edit the file as a normal text file. Then press Ctrl+X and Ctrl+C to save the file. When it prompts for saving the file, press y to save and quit the editor.

    Emavs in shell mode

    So these were some simple editors you can use to easily and safely edit config files in your Ubuntu operating system.

    About This Site

    Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

    Latest Tutorials

    Источник

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