简体   繁体   English

如何在shell脚本中存储IP地址?

[英]How to store IP Address in shell script?

I want to scp a file from a remote machine to my machine using a shell script: 我想使用Shell脚本将文件从远程计算机scp到我的计算机:

SRC_PASS="pass" 
SRC_PORT="22" 
SRC_USER="root" 
SRC_IP="x.x.x.x"
TAR_SRC="/tmp/abc.gz" 

echo $SRC_IP 
sshpass -p $SRC_PASS scp -P $SRC_PORT $SRC_USER@$SRC_IP:$TAR_SRC /tmp/xy.gz

I have debugged and found out that the $SRC_IP for some reason isn't storing the IP address properly. 我进行了调试,发现$SRC_IP由于某种原因未正确存储IP地址。

For example, it works if I use this: 例如,如果我使用以下方法,它将起作用:

sshpass -p $SRC_PASS scp -P $SRC_PORT $SRC_USER@x.x.x.x:$TAR_SRC /tmp/xy.gz 

I believe you need to enclose your variable names in curly braces so the shell does not consider "@" or ":" as part of the variable name. 我相信您需要将变量名用大括号括起来,以便外壳程序不会将“ @”或“:”视为变量名的一部分。 Example: 例:

sshpass -p $SRC_PASS scp -P $SRC_PORT ${SRC_USER}@${SRC_IP}:${TAR_SRC} /tmp/xy.gz

If that's not the issue and the script is in bash, use 如果那不是问题,并且脚本在bash中,请使用

bash -x /path/to/script.txt

To execute it and see the issue. 要执行它并查看问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM