tanmayghosh2507
tanmayghosh2507

Reputation: 763

How to know current server name where the code is running using shell script?

I have a shell script code, which can run on any one of the 6 servers and one file gets updated. But I need to maintain a common file to get the most updated changes in the file and apply certain logic on the latest file. So, I want to know the current server name where it is running at that particular moment?

Here is my code, I have fixed a abc.com server my common server where I am saving the file and next time I am retrieving the file from it. But I also need the current server name, as some operations need to be done based on current server. So, I just want to add one line of code which gives me back the current server where it is running.

#!/bin/bash
# This code copies the file with latest changes to a common location.

server_name=abc.com

# backlog.txt is my file name
echo - removing the old files , if any
rm $server_name:/home/backlog.txt

echo "$server_name to copy the file"

scp /location/backlog.txt $server_name:/home/

echo "Copy complete."

exit 0

And this:

#!/bin/bash
# This code copies back the common file from common server location "abc" and then make the changes into it. As I always need to work on the latest file.

server_name=abc.com

echo - removing the old files , if any
rm /location/backlog.txt

echo "Copying the files from $server_name to local server..."

scp $server_name:/home/backlog.txt /location/

echo "Copy Complete"

exit 0

Upvotes: 5

Views: 15305

Answers (1)

Jakuje
Jakuje

Reputation: 25956

hostname utility usually does what you need.

Upvotes: 13

Related Questions