5

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
Kenster
  • 23,465
  • 21
  • 80
  • 106
tanmayghosh2507
  • 773
  • 3
  • 12
  • 31
  • 1
    Did you try `hostname`? – Jakuje Nov 06 '15 at 11:07
  • Not tried earlier. Thanks. It's working. – tanmayghosh2507 Nov 06 '15 at 11:14
  • Also, Can you also just check whether the use of "scp" is correct or not. Like whether it can do such operations which I intend to do. Because I can't test it using my local system. And I do not want to repeat the whole long process of deploying into production if something won't work. – tanmayghosh2507 Nov 06 '15 at 11:18
  • yes, it looks good, except `rm $server_name:/home/backlog.txt`. What do you expect it will do? – Jakuje Nov 06 '15 at 11:21
  • It's just that I want to delete the older file that is present there. As this is a daily operation. So, there may be a same file of some earlier day present there. Though it's not that required. it will anyway replace it. – tanmayghosh2507 Nov 06 '15 at 11:22
  • There is no such syntax for remote `rm`, but the existing file will be overwritten by the `scp` call, so you can remove that line. – Jakuje Nov 06 '15 at 11:24

1 Answers1

13

hostname utility usually does what you need.

Jakuje
  • 24,773
  • 12
  • 69
  • 75
  • Thanks! Should work. Also, Can you also just check whether the use of "scp" is correct or not. Like whether it can do such operations which I intend to do. Because I can't test it using my local system. And I do not want to repeat the whole long process of deploying into production if something won't work. – tanmayghosh2507 Nov 06 '15 at 11:20
  • 1
    One question per question, please; or post to https://codereview.stackexchange.com/ if you want your code reviewed – tripleee Nov 06 '15 at 12:48