V
V
Vyacheslav Shevchenko2018-12-23 19:16:17
linux
Vyacheslav Shevchenko, 2018-12-23 19:16:17

How to copy a file with the replacement of some sections in it?

Hello. There is a configured server. And files for virtual hosts. I want to make a script that asks the user for several parameters and then copies the files for the host with the replacement of some values ​​in it. Tell me how this can be done. In particular, the replacement within the file.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vyacheslav Shevchenko, 2018-12-23
@WebDev2030

In general, I found a solution here . I am using the sed command.

D
Dmitry Shitskov, 2018-12-23
@Zarom

It seems you need not a script. You need to master, for example, Ansible and use configuration file templates.

J
jcmvbkbc, 2018-12-23
@jcmvbkbc

replacement within a file

I use this construct for this:
#! /bin/bash

subst()
{
  eval "cat <<EOF
`cat \"$1\"`
EOF"
}

. ./config
subst ./dhcpd.conf.in > ./dhcpd.conf

In this case, dhcpd.conf.in looks like this:
ddns-update-style interim;
ignore client-updates;

subnet ${NET}.0 netmask 255.255.255.0 {
        option routers                  ${NET}.1;
        option subnet-mask              255.255.255.0;

        option domain-name-servers      192.168.248.21;

        option root-path                "${NET}.1:/tftpboot/tensilica/rootfs/${ROOTFS}${NFS_MOUNT_OPTIONS}";

        range dynamic-bootp ${NET}.128 ${NET}.128;
        default-lease-time 21600;
        max-lease-time 43200;
}

and config like this:
NET=192.168.135
ROOTFS=cp0_latest
NFS_MOUNT_OPTIONS=${NFS_MOUNT_OPTIONS},noac
IF_CONFIG="$NET.1 up"
CONFIG=debug-kc705-cp0_latest
KERNEL_IMAGE=uImage

Those. in the subst function, the inner cat substitutes the body of the shiblon file, and eval executes the outer cat with the substitution of shell variables read from config. You need to understand that both the template and the config in this script can execute arbitrary commands, i.e. you can not take them from an untrusted user.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question