1

I'm trying to make a script that enables proxy settings if the /etc/environment file is currently empty and disables the settings if the file has text. I've written some code but not sure why the /etc/environment file is not being edited. I have blanked out the actual proxies I am using. Any help would be greatly appreciated!

#!/bin/bash

        if [ -s /etc/environment ]
        then
            cat<<EOT >> /etc/environment
            http_proxy="blank"
            https_proxy="blank"
            ftp_proxy="blank"
            export http_proxy https_proxy ftp_proxy
            EOT
        else
            > /etc/environment
        fi
Cyrus
  • 84,225
  • 14
  • 89
  • 153
bwalsh434
  • 17
  • 3

2 Answers2

5

Use <<- instead of << if you want to indent the end token EOT or move EOT to first column.

Please take a look: http://www.shellcheck.net/

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • shellcheck is useful. It showed me the space after EOF and was able to fix the issue. Thanks. – AMS Jul 24 '22 at 00:34
0

You can also move the here document to the if statement itself.

if [ -s /etc/environment ]; then
  cat
fi <<EOT >> /etc/environment
http_proxy="blank"
https_proxy="blank"
ftp_proxy="blank"
export http_proxy https_proxy ftp_proxy
EOT

although if the if statement itself is indented, you'll still want to know how to indent the here document as Cyrus explains.

chepner
  • 497,756
  • 71
  • 530
  • 681