我有一个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
而且:
#!/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
答案 0 :(得分:8)
hostname
实用程序通常可以满足您的需求。