Home > Back-end >  Bash script that only extract specific file name and then use it to a variable
Bash script that only extract specific file name and then use it to a variable

Time:12-09

I am trying to create a bash script that extracts a "specific" filename and then is used in a variable?

In other words, I am running multiple virtual machine instances and each of them has its own configuration files within their own directories, each virtual machine instance is named (POD##)

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

I have a bash script that runs each virtual machine depending on their config files

#!/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

#...

As I have more servers with other Pod numbers, I would like to automate the search of these files directories so that the bash script knows which directory to look for without actually manually typing on the script the path location, for instance: if I have a directory named POD8 in my server, I would like the bash script to extract that path automatically for each pod

Instead of having ./vmx.sh -lv --start --cfg config/pod8/vmx1.conf in the script I would like to have something like ./vmx.sh -lv --start --cfg config/{VARIABLE that extracts foldername}/vmx1.conf and if I have Pod 9, then the path would change to 9 automatically.

So far I was able to extract the path files by using basename, but I have had no success in the rest :(, any ideas?

CodePudding user response:

So after such great help from "Fravadona" I was able to solve my issue, here is my running script which runs flawlessly!

#!/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
}
  • Related