0

I have a bash script like below. I want to redirect the logs to a specific directory based on the server where the script is running.

#!/bin/bash

# Host name of server
host=hostname

if [ "$host" == "XXXXXXX.com" ];then
        logs=devlogs
else
        logs=logs
fi

do something  > /home/$USER/"${logs}"/abc_"${Date}" 2>&1

Here in the script If the server is XXXXXXX.com I want to store the logs in /home/$USER/devlogs/abc_"${Date}" else /home/$USER/logs/abc_"${Date}"

But even when the server is XXXXXXX,com the logs are still being stored in /home/$USER/logs/abc_"${Date}"

What am I doing wrong here

User12345
  • 5,180
  • 14
  • 58
  • 105
  • FYI, `==` isn't guaranteed to work if your code is run with `sh` rather than `bash`. Safer to change it to `[ "$host" = "XXXXXX.com" ]`, with only one `=`. – Charles Duffy Jul 25 '18 at 00:05
  • Also, use `set -x` to log your script -- in that case, you'd see `[ hostname == XXXXXX.com ]` instead of `[ whatever.com = XXXXXX.com ]`, which would be a pretty good hint as to what's going on. – Charles Duffy Jul 25 '18 at 00:06

1 Answers1

0

You are unable to find the hostname correctly. Use like below to find

host=$(hostname)

Then when your hostname is XXXXXXX.com

if [ "$host" == "XXXXXXX.com" ] 

will become

if [ "XXXXXXX.com" == "XXXXXXX.com" ]

Then you will be able to achieve what you want

User12345
  • 5,180
  • 14
  • 58
  • 105
  • If that's the issue, the question is a duplicate of https://stackoverflow.com/questions/4651437/how-to-set-a-variable-to-the-output-of-a-command-in-bash and should be closed as such; see the "Answer Well-Asked Questions" heading within [How to Answer](https://stackoverflow.com/help/how-to-answer). – Charles Duffy Jul 25 '18 at 00:07