Monday, October 31, 2022

[FIXED] How do I get EOT to work on IF ELSE statement in bash?

Issue

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

Solution

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.



Answered By - chepner
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.