How to remove multiple lines from file in bash? - bash

I am adding several lines to a file like this:
echo "server {
listen 80;
server_name ${brand}.mydomain.com;
root /srv/www/clients/${brand}/soon;
}" >> default
The result is like this (brand is passed via parameter) :
server {
listen 80;
server_name cola.mydoman.com;
root /srv/www/clients/cola/soon;
}
The question is how to remove this specifically if this file contains lots of similar other values. Is it possible to do this with sed, awk or something else?

Related

How to process or excape variables inside of EOF to write file content?

This is how I create a file (nginx.conf) via shell.
As there are $ characters in the filecontent I'm using EOF.
if [ $type == "nginx" ]; then
cat > ${path}/nginx.conf <<'EOF'
server {
listen 3000;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
include /etc/nginx/extra-conf.d/*.conf;
}
EOF
fi
Now I have to use a dynamic port value, so instead of listen 3000 I need to use listen $port.
But this won't work, as in the content there is also $uri, which should be handled as text, not as a variable.
Using only the delimiter itself, either all parameters are expanded or none. You'll have to allow expansion, but escape the dollar signs for $uri to inhibit their expansion.
if [ "$type" = "nginx" ]; then
cat > "${path}/nginx.conf" <<EOF
server {
listen $port;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files \$uri \$uri/ /index.html = 404;
}
include /etc/nginx/extra-conf.d/*.conf;
}
EOF
fi
The here document behaves like a double-quoted string:
$ foo=bar
$ echo "$foo"
bar
$ echo "\$foo"
$foo

Bash - Grab the output and put it in the particular place in the file

I have python script generating AWS Signature key for S3. It generates two values:
GZkXNl6Leat71ckcwfxGuiHxt9fnkj47F1SbVjRu/t0=
20190129/eu-west-2/s3/aws4_request
Both are valid for 7 days. What I want is to run that script for every five days using cron inside the Docker container, grab the output and place/replace values in the Nginx config
config:
server {
listen 80;
aws_access_key 'AKIDEXAMPLE';
aws_signing_key FIRST_VALUE;
aws_key_scope SECOND_VALUE;
aws_s3_bucket s3_bucket_name;
location / {
aws_sign;
proxy_pass http://s3_bucket_name.s3.amazonaws.com;
}
Then restart nginx in the container
Assuming the values are stored in val_file, slotting them into nginx.conf, this simplistic solution ought to do -
$: cat script
#! /bin/env bash
{ read -r val1 # read the first line
read -r val2 # read the second line
sed -i "s!FIRST_VALUE!$val1!;
s!SECOND_VALUE!$val2!;
" nginx.conf
} < val_file
$: script
$: cat nginx.conf
server {
listen 80;
aws_access_key 'AKIDEXAMPLE';
aws_signing_key GZkXNl6Leat71ckcwfxGuiHxt9fnkj47F1SbVjRu/t0=;
aws_key_scope 20190129/eu-west-2/s3/aws4_request;
aws_s3_bucket s3_bucket_name;
location / {
aws_sign;
proxy_pass http://s3_bucket_name.s3.amazonaws.com;
}
The curlies make the single input supply both reads. Then it's just a sed, using !'s because you have standard forward slashes in your data. Double quotes on the sed to allow embedded vars - not ideal, but seems ok here.

creating file in shell then replacing text inside

I have a script where i declare variables then create a file and then replace a variable within that file, this is my example script
#!/bin/bash
DMNAME = mydomain.com
cat <<EOF > /etc/nginx/conf.d/default.conf
server_name DMNAME;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/DMNAME/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/DMNAME/privkey.pem;
EOF
sed -i 's/DMNAME/mydomain.com/g' /etc/nginx/conf.d/default.conf
#
Would this be the correct way of replacing DMNAME with mydomain.com ?
#!/bin/bash
DMNAME="mydomain.com"
cat <<EOF > /etc/nginx/conf.d/default.conf
server_name $DMNAME;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/$DMNAME/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DMNAME/privkey.pem;
EOF
#Bor is right that he fills /etc/nginx/conf.d/default.conf with the correct values when creating it.
When you want to use default.conf more than once, you shouldn't let sed change the file with the -i option, but redirect the results to the file you want.
# Answer: do not use this here: DMNAME = mydomain.com
cat <<EOF > /etc/nginx/conf.d/default.conf
server_name DMNAME;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/DMNAME/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/DMNAME/privkey.pem;
EOF
# And now DMNAME="mydomain.com" (without spaces, quotes are optional here).
# or for different domains
confpath="/etc/nginx/conf.d"
for domain in mydomain.com yourdomain.com hisdomain.com; do
sed "s/DMNAME/${domain}/g" "${confpath}"/default.conf > "${confpath}"/${domain%.*}.conf
done

change multiple files content by the file name

I have few file, the name of the files if vhost-<someport>.conf, I want to replace the line with the port number in each file by the filename $port vhost-$port.conf.
For example vhost-8081.conf file will contain the line listen 8081, vhost-8082.conf file will contain the line listen 8082;, etc..
vhost-8081.conf
vhost-8082.conf
etc..
server {
listen 8081;
}
server {
listen 8082;
}
If you have files named vhost-NUM.conf, then you can strip the vhost- prefix and the .conf suffix to extract the port number, and then use sed -i to replace listen .* with the number in-place, updating the file:
for file in vhost-*.conf; do
num=${file#vhost-}
num=${num%.conf}
sed -i "s/listen .*/listen $num;/" "$file"
done
This may be what you want:
for file in vhost-*.conf
do num=${file#vhost-}
sed -i "s/listen .*/vhost-$num;/" "$file"
done
cat vhost*
server {
vhost-8081.conf;
}
server {
vhost-8082.conf;

How to pass bash variable to a string?

I am doing everything in a bash file. I am grabbing to variables from a parameter:
brand="$1"
email="$2"
Afterwards, I want to include on of them inside of a string:
cd /etc/nginx/sites-available/
echo 'server {
listen 80;
server_name $brand.mydomain.com;
root /srv/www/clients/$brand/soon;
}' >> default
But it echo's $brand.mydomain.com. How to echo the actual value which I am passing as parameter?
Single quotes don't allow for expansion of anything. Double quotes allow for expansion of variable, but you best enclose the name with parenthesis as shown.
echo "server {
listen 80;
server_name ${brand}.mydomain.com;
root /srv/www/clients/${brand}/soon;
}" >> default

Resources