Bash 如果需要多次安装的条件
我正在尝试用这个shell脚本创建一个蓝图,以便在不同的系统上安装不同的RPM,并匹配系统类型。在本例中,我总共有两个系统,分别命名为centos1和suse1。每个系统都有一个存储在变量“id”中的相应资源id,并且应该运行与每个系统匹配的命令。所有系统名称都从名为vm.txt的文本文件中读取Bash 如果需要多次安装的条件,bash,shell,Bash,Shell,我正在尝试用这个shell脚本创建一个蓝图,以便在不同的系统上安装不同的RPM,并匹配系统类型。在本例中,我总共有两个系统,分别命名为centos1和suse1。每个系统都有一个存储在变量“id”中的相应资源id,并且应该运行与每个系统匹配的命令。所有系统名称都从名为vm.txt的文本文件中读取 ~]# cat vm.txt centos1 suse1 ~]# ./myscript.sh vm.txt #!/bin/bash for node in `cat
~]# cat vm.txt
centos1
suse1
~]# ./myscript.sh vm.txt
#!/bin/bash
for node in `cat $1`
do
# id is the resource id for each system listed in vm.txt
id=`./test.sh get resource id --platform=$node`
if [ $node == centos1 ]
then
echo `./install.sh resource $id centos1-httpd`
echo `./install.sh resource $id centos1-gcc`
elif [ $node == suse1 ]
then
echo `./install.sh resource $id suse1-httpd`
echo `./install.sh resource $id suse1-gcc`
else
echo "No packages found"
fi
done
接下来,我将不得不在脚本文件和安装标准中添加数百个系统。我不确定是否在这个脚本中使用if-else条件,这将导致编写更多的代码行,从而给以后的管理带来困难。。有没有更好的方法来处理这个问题?我应该使用不同的条件来实现这一点吗
PS:很抱歉这个基本问题。我仍在尝试动手编写脚本。关于:
#!/bin/bash
while read -d $'\n' -r node;
do
# id is the resource id for each system listed in vm.txt
id=`./test.sh get resource id --platform=$node`
echo `./install.sh resource $id ${node}-httpd`
echo `./install.sh resource $id ${node}-gcc`
done < "$1"
#/bin/bash
读取-d$'\n'-r节点时;
做
#id是vm.txt中列出的每个系统的资源id
id=`./test.sh获取资源id--platform=$node`
echo`./install.sh资源$id${node}-httpd`
echo`./install.sh资源$id${node}-gcc`
完成<“$1”
为了取悦乔洛巴
#!/bin/bash
known_nodes=" centos1 suse1 " # leading and final space are important
while read -d $'\n' -r node
do
if [[ " $node " =~ "$known_nodes" ]]; then
# id is the resource id for each system listed in vm.txt
id=`./test.sh get resource id --platform=$node`
echo `./install.sh resource $id ${node}-httpd`
echo `./install.sh resource $id ${node}-gcc`
else
echo "Don't know node ${node}. Skipping."
fi
done < "$1"
#/bin/bash
已知_nodes=“centos1 suse1”#前导空间和最终空间很重要
读取-d$'\n'-r节点时
做
如果[[“$node”=~“$known_nodes”];然后
#id是vm.txt中列出的每个系统的资源id
id=`./test.sh获取资源id--platform=$node`
echo`./install.sh资源$id${node}-httpd`
echo`./install.sh资源$id${node}-gcc`
其他的
echo“不知道节点${node}。正在跳过。”
fi
完成<“$1”
您可以使用案例
在进行过程中添加发行版将比elseif
更容易:
tiago@dell:/tmp$ cat myscript.sh
#! /bin/bash
for node in $(cat $1); do
id=$(./test.sh get resource id --platform=$node)
case $node in
centos1)
echo "./install.sh resource $id centos1-httpd"
echo "./install.sh resource $id centos1-gcc"
;;
suse1)
echo "./install.sh resource $id suse1-httpd"
echo "./install.sh resource $id suse1-gcc"
;;
*)
echo "No Packages found"
;;
esac
done
执行:
tiago@dell:/tmp$ bash myscript.sh <(echo -e 'centos1\nsuse1\nfedora')
./install.sh resource centos1-httpd
./install.sh resource centos1-gcc
./install.sh resource suse1-httpd
./install.sh resource suse1-gcc
No Packages found
tiago@dell:/tmp$bash myscript.sh我将为$node添加一个有效值的验证。用于'cat$1'中的节点。
。请不要这样做。看看我对蒂亚戈回答的评论。@gniourf_gniourf是的,你是对的,但它太诱人了。他的文件格式非常好:)谢谢蒂亚戈。我还针对每个节点映射了一个资源id,它存储在变量“id”中。需要识别此id并将其添加到install.sh命令中。在这个脚本中我应该在哪里指定它?id=/test.sh获取资源id--platform=$node
我把它放在了正确的位置。请注意,echo.“/install.sh resource$id suse1 httpd”
应该是echo$(/install.sh resource$id suse1 httpd)
。我把它放在引号之间,这样我就可以打印而不是执行。用于$(cat$1)中的节点;执行
。请不要用这个!作为练习,试着想象一下,在某些情况下,这种方法失败得很惨,为什么它会被严重破坏,为什么世界会变得更好,如果这种可怕的、错误的、破坏性的信息在网络上不那么普遍的话。我们希望成为一个高质量的网站,所以请,请!不要这样贴垃圾。使用另一个网站。嘿@gniourf\u gniourf我在这里学到的东西比其他任何东西都多<代码>而IFS=读取-r节点;do
仍然可能是无效节点。我当然可以为validade节点创建一个函数,但我想这超出了问题的范围,不是吗?您有什么建议?对于“cat$1”中的节点:
:不要这样做。另外,去投诉给你那些可怕信息的人或网站。去做吧。认真地