Run config file 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:

Читайте также:  Linux d link dns 325

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:

Источник

Sysctl Config File

Generally, in Linux, when there are various approaches to a problem, the flexible solution is usually the most suitable or the most efficient. However, it often needs precise settings and customizations.

It is absolutely true with the Linux OS and the processes related with boot-up settings and configurations. There are numerous questions at start-up like what kind of system hardware will this OS be running, what will it be used for specifically? etc. The configuration file that lets the Linux kernel respond to these queries is the sysctl.conf config file. Systems that runs Linux may be configured to be even more efficient by the administrators who are familiar with the sysctl.conf file.

What Will We Talk About?

In this article, we will explore the sysctl.conf file in Linux. Let’s start with the basic intro of this file.

The Sysctl Config File: What Is It?

The Linux operating system’s /etc/sysctl.conf file, which is also sometimes called as the sysctl.conf file, has configuration and startup instructions for the Linux operating system. As per the sysctl.conf file, the Linux OS or the system manager (such as SystemD) adds or modifies the parameters to the Linux kernel during startup.

The Linux kernel, which runs Linux, makes this crucial. The kernel functions are much like its brain. It has complete control over Linux’s backbone. The remaining part of the OS is built on top of the kernel using different applications like Samba, SSH, Cups, etc.

Читайте также:  Postgresql установка linux manjaro

The Linux kernel is an evolving entity that may be configured to operate with particular hardware such as a smart device. Or it might be a one-size-fits-all solution that supports a wide range of PC setups, as with Ubuntu or Red Hat. Kernels may be modified with particular parameters or with a wide range of settings. Drivers, for example, may be directly integrated into the kernel or as modules that can be inserted or detached at any moment.

Because of its adaptability and flexibility, Linux is used in a wide range of devices. The Linux core operating system can be greatly modified for IoT devices (Internet of things) or smartphones, or it can run the laptops and Desktops.

However, when it comes to making modifications to the core kernel, you have two choices. The first way is to recompile or upgrade the kernel using your distribution provider. Or you may adjust the particular parameters for it in the sysctl.conf file.

Configuring the Sysctl Configuration File

Note of caution: This article does not get into the specifics of customizing the sysctl.conf file since they are too complex and subtle. There are too many possible and manifold applications to consider for every use case of the sysctl.conf file.

The sysctl.conf file, however, may be easily modified. It simply takes a lot of time and effort to set up. Here, we go through the file format and editing procedures that you should follow. Your search is not yet complete if you require the details on editing specific entries. An additional investigation on your part is required.

However, Linux makes it easy to edit the sysctl.conf file. Open the sysctl.conf file in your preferred text editor. Since sysctl.conf is a system-protected file, it must be opened and edited with superuser permissions:

Sample Output:

# Functions previously found in netbase

# Uncomment the next two lines to enable Spoof protection (reverse-path filter)

# Turn on Source Address Verification in all interfaces to

# prevent some spoofing attacks

# Uncomment the next line to enable TCP/IP SYN cookies

# Note: This may impact IPv6 TCP sessions too

# Uncomment the next line to enable packet forwarding for IPv4

In the sysctl.conf file, enter the necessary instructions. Here, instructions are a particular kernel parameter followed by its value.

Читайте также:  Dead space on linux

Since Linux does not care about the white space in the sysctl.conf file, you can feel free to include as much as you need for organizational purposes. You shouldn’t be afraid to make it comprehensible and attractive at the same time.

For sysctl.conf, Linux interprets all lines that start with a semicolon or the pound/hash sign as comments. These lines are simply ignored:

If a line starts with a dash (-) and that line fails, Linux skips over it and goes on to the next one.

The sysctl.conf file contains the new values for each kernel option. The following is an example of a correct entry:

This command enables the Linux OS to enable the IP forwarding on Ubuntu/Debian systems. After adding or uncommenting this line, execute the following command:

This ensures that the IPv4 forwarding is properly configured on your system.

Note of Caution: Please beware that sysctl.conf is a critical file. If you don’t know what you’re doing, playing around with the etc/sysctl.conf file might have serious consequences.

Location of the Sysctl Config File

First, you should be aware that your Linux distribution may not have a sysctl.conf file. Additionally, it has the flexibility to be saved under a variety of aliases and placed in a number of different directories.

The sysctl.conf file may be called or stored in any of the following places, as per your system and kernel:

  • /etc/sysctl.d/*.conf
  • /run/sysctl.d/*.conf
  • /usr/local/lib/sysctl.d/*.conf
  • /usr/lib/sysctl.d/*.conf
  • /lib/sysctl.d/*.conf
  • /etc/sysctl.conf*

In general, Linux tries to read the files in that sequence. If several files with the same name exist in different folders, a valid file that is encountered with valid entries for the first time will only be considered. Therefore, you may need to search in each of those folders for those particular files, depending on the Linux distribution that you’re using.

Similarly, the sysctl.conf file is used as a symlink in some Linux systems such as REHL. The sysctl.conf file in the /etc directory may be created from scratch if it is missing.

Conclusion

This tutorial is just scratching the surface of what you can do with the sysctl.conf file. We advise you to gain a sufficient knowledge prior to manipulating this file.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.

Источник

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