我有一个shell脚本myautoappupgrade.sh,我可以在其中自动执行应用程序升级过程。该脚本必须在几个不同的服务器上运行。不幸的是,应用程序位于每个服务器上的略有不同的目录中-父目录的数量在1-20之间变化。我如何修改脚本,以便目录可以被某种变量替换?我不想为每个服务器编辑脚本,因为自动化脚本中有许多目录查询。
示例:
cd /ae1/apps/myapp/upgradefiles/
unzip file.zip
./install.sh另一台服务器上的目录略有变化:
cd /ae2/apps/myapp/upgradefiles/
unzip file.zip
./install.sh还有另一个..。
cd /ae3/apps/myapp/upgradefiles/
unzip file.zip
./install.sh发布于 2021-02-04 18:18:41
尝试如下所示:
#!/bin/bash
num=$1
cd /ae${num}/apps/myapp/upgradefiles/file.zip
unzip file.zip
./install.sh然后使用数字作为第一个参数调用脚本:
myautoappupgrade.sh 1发布于 2021-02-04 18:39:02
简单而明显的解决方案是根本不对目录进行硬编码。修改脚本,使其接受父目录作为参数,或者在运行脚本之前直接通过cd进入父目录。
可能是这样的:
while read server dir; do
ssh "$server" "cd '$dir' && unzip apps/myapp/upgradefiles/file.zip/file.zip && ./install.sh"
done <<\:
ernie /ae1
bert /ae2
cookiemonster /home/cmonster/anN
:如果您将其解压到一个临时目录中,可能会更好,但希望这会让您朝着正确的方向前进。
当然,如果您可以确定/ae[0-1]始终在那里并且只有一个匹配,
cd /ae[0-9]/apps/myapp/upgradefiles/file.zip会照你说的做。
(您是否真的在名为file.zip的目录中有一个名为file.zip的文件?我猜实际上是从cd路径的末尾去掉了file.zip。)
发布于 2021-02-04 23:26:05
只需使用:
cd /ae*/apps/myapp/upgradefiles/*将扩展任何字符。
https://stackoverflow.com/questions/66043390
复制相似问题