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
Related
I have a dictionary that has variable names in the value string.
I'm trying to lookup the dictionary value, then parse the result with the actual variable value in the string (not the variable name).
How would I do it?
Example:
asset_symbol='BTC'
counter_asset_symbol='ETH'
Dictionary entry:
['ct']=https://charts.cointrader.pro/charts.html?coin=$asset_symbol%3A$counter_asset_symbol
When calling the value I want it to parse $asset_symbol and $counter_asset_symbol" to "BTC" and "ETH"
So in this case I want:
https://charts.cointrader.pro/charts.html?coin=BTC%3AETH
Examples of what didn't work:
#!/bin/bash
chart_engines=('ct cg')
# No ''
declare -A search_urls=(
['ct']=https://charts.cointrader.pro/charts.html?coin=$asset_symbol%3A$counter_asset_symbol
['cg']=https://beta.coinigy.com/markets/$exchange_symbol/$asset_symbol/$counter_asset_symbol
)
# With single quotes
declare -A search_urls=(
['ct']='https://charts.cointrader.pro/charts.html?coin=$asset_symbol%3A$counter_asset_symbol'
['cg']='https://beta.coinigy.com/markets/$exchange_symbol/$asset_symbol/$counter_asset_symbol'
)
# With double quotes
declare -A search_urls=(
['ct']='https://charts.cointrader.pro/charts.html?coin=$asset_symbol%3A$counter_asset_symbol'
['cg']='https://beta.coinigy.com/markets/$exchange_symbol/$asset_symbol/$counter_asset_symbol'
)
asset_name='Bitcoin'
asset_symbol='BTC'
counter_asset_name='Ethereum'
counter_asset_symbol='ETH'
exchange_name='Binance'
exchange_symbol="BINA"
open_charts_urls(){
for i in ${chart_engines[#]}; do
# Get URL
# Dictonary lookup doesn't fill in the variables.
local charts_url="${search_urls[$i]}"
echo "$charts_url"
# Direct reference does.
# echo "https://beta.coinigy.com/markets/$exchange_symbol/$asset_symbol/$counter_asset_symbol"
# echo "https://charts.cointrader.pro/charts.html?coin=$asset_symbol%3A$counter_asset_symbol"
# local subs="$charts_url"
# echo "subs:$subs"
done
}
open_charts_urls
You are referencing $asset_symbol and $counter_asset_symbol before they are declared. Declare the $search_urls array below the declaration of these symbols. Also, use double quotes for the values within the array. Single quotes will prevent the parameter substitution. See quoting. In your example below the comment # With double quotes you are still using single quotes.
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
In Bash, I am trying to create a path with two variables within:
/path/to/my/file/${variable1_-}${variable2}/Still/some/path
My variable2 is always set, but the variable1 might be empty and in that case I don't want to print the "_"
I have tried the line above, but doesn't seem to be correct.
Can someone help in getting the right path printed?
Thanks in advance for your suggestions!
You have a simple typo (the underscore should be after the separator, not part of the variable name) and you want to include the underscore if variable1 is set, not it it's unset (so plus instead of minus in the parameter expansion; and add a colon to also cover the set but empty case). Presumably you also want to include the actual value of variable1 when it's set.
/path/to/my/file/${variable1}${variable1:+_}${variable2}/Still/some/path
or equivalently, nested
/path/to/my/file/${variable1:+${variable1}_}${variable2}/Still/some/path
where the braces before the underscore are necessary to separate the variable name from the literal text.
You can use this.
https://linux.die.net/man/1/bash
${parameter:+word}
set also variable1
variable1=VAR1
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
set only variable2
variable1=
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
With a few more lines of code this could work:
run () {
prefix="" # empty
if [ -n "$variable1" ]; then
prefix="${variable1}_"
fi
echo "/path/to/my/file/${prefix}${variable2}/Still/some/path"
}
# set only variable2
variable2=var2
run
# set also variable1
variable1=var1
run
output:
/path/to/my/file/var2/Still/some/path
/path/to/my/file/var1_var2/Still/some/path
description:
-n tests if the string is not empty, in that case I fill prefix with variable1 and the underscore
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.
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?