【问题标题】:Bash script that only extract specific file name and then use it to a variable仅提取特定文件名然后将其用于变量的 Bash 脚本
【发布时间】:2022-01-11 07:46:29
【问题描述】:

我正在尝试创建一个提取“特定”文件名然后在变量中使用的 bash 脚本?

换句话说,我正在运行多个虚拟机实例,每个实例在自己的目录中都有自己的配置文件,每个虚拟机实例都被命名为 (POD##)

# cd /home/vMX-ENV/vMX-21.1R1/config/
# ls
pod10  pod8  pod9  samples  vmx.conf  vmx-junosdev.conf

我有一个 bash 脚本,它根据每个虚拟机的配置文件运行它们

#!/bin/bash

## Activate Python Virtual Environment
source /home/vMX-ENV/bin/activate

### Commands used to /start/ vmx VM [Repeat per each POD]
#[Pod 8]
./vmx.sh -lv --start --cfg config/pod8/vmx1.conf

#[Pod 9]
./vmx.sh -lv --start --cfg config/pod9/vmx1.conf

#[Pod 9]
./vmx.sh -lv --start --cfg config/pod10/vmx1.conf

#...

由于我有更多具有其他 Pod 编号的服务器,我想自动搜索这些文件目录,以便 bash 脚本知道要查找哪个目录,而无需在脚本上实际手动键入路径位置,例如:如果我的服务器中有一个名为 POD8 的目录,我希望 bash 脚本为每个 pod 自动提取该路径

脚本中没有./vmx.sh -lv --start --cfg config/pod8/vmx1.conf 我想要./vmx.sh -lv --start --cfg config/{VARIABLE that extracts foldername}/vmx1.conf 之类的东西,如果我有 Pod 9,那么路径会自动更改为 9。

到目前为止,我能够使用 basename 提取路径文件,但在其余部分我没有成功 :(,有什么想法吗?

【问题讨论】:

  • 您需要每个 vmx1.conf 文件的 POD 编号或路径吗?
  • 理论上,我需要 pod 编号,因为 vmx1.conf 文件在所有 pod 目录上始终相同,每个 pod 目录内都有 vmx1.conf vmx2.conf vr-device和 vmx-junos-dev.conf
  • for vmx_conf in config/pod*/vmx1.conf; do ./vmx.sh -lv --start --cfg "$vmx_conf"; done
  • 我必须手动在 (pod*) 中添加 pod 编号吗?有没有办法自动提取现有目录名称,所以如果它是 Pod 8,它将是:(对于 config/{pod8 directory}/vmx1.conf 中的 vmx_conf)或者如果它是 Pod 9(对于 config/{ 中的 vmx_conf pod9 目录}/vmx1.conf)?
  • 不用了,这段代码足以启动所有的pod

标签: bash automation filenames script


【解决方案1】:

所以在"Fravadona" 的大力帮助下,我能够解决我的问题,这是我运行完美的运行脚本!

#!/bin/bash

## Activate Python Virtual Environment
source /home/vMX-ENV/bin/activate


vmx1.conf () {
echo -e "\e[42mStarting Juniper vMX1 Routers\e[0m"
for vmx1_conf in config/pod*/vmx1.conf
do ./vmx.sh -lv --start --cfg "$vmx1_conf"
done
vmx2.conf
}

vmx2.conf () {
echo -e "\e[42mStarting Juniper vMX2 Routers\e[0m"
for vmx2_conf in config/pod*/vmx2.conf
do ./vmx.sh -lv --start --cfg "$vmx2_conf"
done
vr-devices.conf
}

#vr-devices.conf () {
echo -e "\e[42mStarting Juniper vr-vMX Routers\e[0m"
for vr_conf in config/pod*/vr-device.conf
do ./vmx.sh -lv --start --cfg "$vr_conf"
done
echo -e "\e[42mAll vMX Instances Have Started Sucessfully\e[0m"
bindings.conf
}

bindings.conf () {
echo -e "\e[42mStarting Juniper vMX Bindings\e[0m"
for bind_conf in config/pod*/vmx-junos-dev.conf
do ./vmx.sh -lv --start --cfg "$bind_conf"
done
echo -e "\e[42mJuniper Bindings deployed Sucessfully\e[0m"
./link-br.sh
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2014-10-11
    • 2012-12-03
    相关资源
    最近更新 更多