[SOLVED] How to break array and extract individual components in a shell script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to break array and extract individual components in a shell script
Hey Guys,
I have a shell script below, basically, the intent of the script is to enable firewall rule on remote Windows servers for WinRM so that ANsible can perform configuration management.
Code:
#!/bin/bash
set -x
PROCESS_ID=$$
LOGFILE=/tmp/"${PROCESS_ID}"
az vm list --output=table |tee -a >> "${LOGFILE}"
az login --service-principal -u $APP_ID --password $PASSWORD --tenant $TENANT_ID
# declare an array variable
declare -a host_list=$(cat "${LOGFILE}"|grep NXT|awk '{print $1}')
echo $host_list
## now loop through the above array
for i in "${host_list[@]}"
do
case ${i} in
*AD* )
export resource_group="rg-ad"
;;
*AOS* | *WEB* )
export resource_group="rg-app"
;;
*SQL* | *FS* )
export resource_group="rg-sql"
;;
*APP* | *STG* )
export resource_group="rg-remote"
;;
*BI* | *SSRS* )
export resource_group="rg-bi"
;;
*)
Message="Invalid hostname , please check"
;;
esac
echo "Installing Windows Remote Management $i with resource group $resource_group "
az vm run-command invoke --command-id RunPowerShellScript --resource-group "${resource_group}" --name "${i}" --scripts '(New-Object -TypeName System.Net.WebClient).DownloadFile("https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1", "$env:temp\ConfigureRemotingForAnsible.ps1")'
az vm run-command invoke --command-id RunPowerShellScript --resource-group "${resource_group}" --name "${i}" --scripts 'powershell.exe -ExecutionPolicy ByPass -File "$env:temp\ConfigureRemotingForAnsible.ps1"'
az vm run-command invoke --command-id RunPowerShellScript --resource-group "${resource_group}" --name "${i}" --scripts 'netsh advfirewall firewall add rule name="Allow WinRM (Http)" dir=in localport=5985 protocol=tcp action=allow enable=yes'
az vm run-command invoke --command-id RunPowerShellScript --resource-group "${resource_group}" --name "${i}" --scripts 'netsh advfirewall firewall add rule name="Allow WinRM (Https)" dir=in localport=5986 protocol=tcp action=allow enable=yes'
done
exit 0
When it is executed
Code:
./temp.sh
+ PROCESS_ID=13879
+ LOGFILE=/tmp/13879
+ az vm list --output=table
+ tee -a
+ az login "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
[
{
"cloudName": "AzureCloud",
"type": "servicePrincipal"
}
}
]
++ cat /tmp/13879
++ awk '{print $1}'
++ grep NXT
+ declare -a 'host_list=NXTPREPAOS-1
NXTPREPAOS-2
NXTPREPAOS-3
NXTPREPAOS-4
NXTPREPAOS-5
NXTPREPSQL-1
NXTPREPSQL-2'
+ echo NXTPREPAOS-1 NXTPREPAOS-2 NXTPREPAOS-3 NXTPREPAOS-4 NXTPREPAOS-5 NXTPREPSQL-1 NXTPREPSQL-2
NXTPREPAOS-1 NXTPREPAOS-2 NXTPREPAOS-3 NXTPREPAOS-4 NXTPREPAOS-5 NXTPREPSQL-1 NXTPREPSQL-2
+ for i in '"${host_list[@]}"'
+ case ${i} in
+ export resource_group=rg-app
+ resource_group=rg-app
+ echo 'Installing Windows Remote Management NXTPREPAOS-1
NXTPREPAOS-2
NXTPREPAOS-3
NXTPREPAOS-4
NXTPREPAOS-5
NXTPREPSQL-1
NXTPREPSQL-2 with resource group rg-app '
Installing Windows Remote Management NXTPREPAOS-1
NXTPREPAOS-2
NXTPREPAOS-3
NXTPREPAOS-4
NXTPREPAOS-5
NXTPREPSQL-1
NXTPREPSQL-2 with resource group rg-app
+ az vm run-command invoke --command-id RunPowerShellScript --resource-group rg-app --name 'NXTPREPAOS-1
NXTPREPAOS-2
NXTPREPAOS-3
NXTPREPAOS-4
NXTPREPAOS-5
NXTPREPSQL-1
NXTPREPSQL-2' --scripts '(New-Object -TypeName System.Net.WebClient).DownloadFile("https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1", "$env:temp\ConfigureRemotingForAnsible.ps1")'
Operation failed with status: 'Bad Request'. Details: 400 Client Error: Bad Request for url: https://management.azure.com/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/rg-app/providers/Microsoft.Compute/virtualMachines/NXTPREPAOS-1%0ANXTPREPAOS-2%0ANXTPREPAOS-3%0ANXTPREPAOS-4%0ANXTPREPAOS-5%0ANXTPREPSQL-1%0ANXTPREPSQL-2/runCommand?api-version=2018-10-01
I want to dynamically get the no of VM's as it could change as per requirement. I know the problem is a whitespace character in the array, how to solve?
Thanks, heaps for that, I cannot believe that double quotes around the array were causing the grief! I am now confused, normally it a good practice to reference a variable like "${env}", so this does not hold true for an array?
You should in fact double quote if it was actually an array.
As it is a simple string the ${[@]} expander is not being used correctly hence the output issues.
Here is a quick demo using orbea's example:
Code:
foo='this is a test'
for i in ${foo[@]}; do echo $i; done
for i in "${foo[@]}"; do echo $i; done
fooa=(this 'is a' test)
for i in ${fooa[@]}; do echo $i; done
for i in "${fooa[@]}"; do echo $i; done
The additional quotes inside the array are to prove why you need quotes around an array
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.