如何使用shell脚本知道运行代码的当前服务器名称?

tan*_*507 2 unix bash shell hostname

我有一个shell脚本代码,可以在6个服务器中的任何一个上运行,一个文件得到更新.但是我需要维护一个公共文件来获取文件中最新的更改并在最新文件上应用某些逻辑.那么,我想知道当前服务器名称在特定时刻运行的位置?

这是我的代码,我已经修复了一个abc.com服务器我的公共服务器,我保存文件,下次我从中检索文件.但我还需要当前的服务器名称,因为某些操作需要基于当前服务器完成.所以,我只想添加一行代码,它让我回到运行它的当前服务器.

#!/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
Run Code Online (Sandbox Code Playgroud)

还有这个:

#!/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
Run Code Online (Sandbox Code Playgroud)

Jak*_*uje 8

hostname 效用通常做你需要的.