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?

  • 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 at 13:55
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 ">>".

| improve this answer | |
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
| improve this answer | |
  • sudo echo "$foo" >> /etc/... is going to execute echo with elevated privileges, but the redirection will still have the privileges of the invoking shell – steeldriver Oct 15 at 14:48
  • @steeldriver Right! Deleted then... I guess there's not the same problem with the first suggestion. – schrodigerscatcuriosity Oct 15 at 14:50
  • same issue with sudo cat >> ... you could use sudo tee -a /etc/... << EOL instead I think? – steeldriver Oct 15 at 14:52
  • 1
    That prints the output... sudo tee -a /etc/... > /dev/null << EOL? – schrodigerscatcuriosity Oct 15 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.