0

I'm trying to output text to a configuration file using variables, in a single elevated line to be included in a shell script.

So far I have the code below, that creates the file with the necessary line spacing etc, but I can't figure out how to get the variables to be pulled through.

sudo sh -c 'echo  /etc/grafana/provisioning/datasources/InfluxDB.yaml << EOL
 apiVersion: 1
 datasources:
  - name: $dbname
    type: influxdb
    url: http://localhost:8086
    user: $usrname
    password: $pssword
    database: $dbname
    access: proxy
    editable: true
    isDefault: true
EOL'

I have also tried this, with various combinations of ", ' or '' but can't seem to get it right. I either get nothing or the variables written as text as you see them above.

sudo sh -c 'echo "apiVersion: 1\n datasources:\n  - name: $dbname\n    type: influxdb\n    url: http://localhost:8086\n    user: $usrname\n    password: $pssword\n    database: $dbname\n    access: proxy\n    editable: true\n    isDefault: true" > /etc/grafana/provisioning/datasources/InfluxDB.yaml'

Where am I going wrong?

1
  • 2
    Welcome! Please create a minimal, reproducible example. One doesn't need all that line with all its variables to picture the problem. Supplying the variables is useful for testing.
    – Quasímodo
    Oct 15, 2020 at 13:55

2 Answers 2

1

The one of correct way is:

sudo sh -c "cat >> /etc/grafana/provisioning/datasources/InfluxDB.yaml << 'EOL'
 apiVersion: 1
 datasources:
  - name: $dbname
    type: influxdb
    url: http://localhost:8086
    user: $usrname
    password: $pssword
    database: $dbname
    access: proxy
    editable: true
    isDefault: true
EOL"

If you want to overwrite the file you should use ">" after cat command instead ">>".

0

If I understand you correctly, you can try this:

dbname=db
usrname=user
pssword=pass

sudo cat << EOL >> /etc/grafana/provisioning/datasources/InfluxDB.yaml
 apiVersion: 1
 datasources:
  - name: $dbname
    type: influxdb
    url: http://localhost:8086
    user: $usrname
    password: $pssword
    database: $dbname
    access: proxy
    editable: true
    isDefault: true
EOL

Output:

cat /etc/grafana/provisioning/datasources/InfluxDB.yaml

 apiVersion: 1
 datasources:
  - name: db
    type: influxdb
    url: http://localhost:8086
    user: user
    password: pass
    database: db
    access: proxy
    editable: true
    isDefault: true
4
  • sudo echo "$foo" >> /etc/... is going to execute echo with elevated privileges, but the redirection will still have the privileges of the invoking shell Oct 15, 2020 at 14:48
  • @steeldriver Right! Deleted then... I guess there's not the same problem with the first suggestion. Oct 15, 2020 at 14:50
  • same issue with sudo cat >> ... you could use sudo tee -a /etc/... << EOL instead I think? Oct 15, 2020 at 14:52
  • 1
    That prints the output... sudo tee -a /etc/... > /dev/null << EOL? Oct 15, 2020 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .