LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-18-2018, 04:42 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
Unhappy case statement not working in for loop


Hello Guys,

Can you kindly assist me and advise why my script is not working??

Code:
#!/bin/bash

set -x


# declare an array variable
declare -a host_list=("APREPAOS-1" "BPREPAOS-2" "APREPWEB-1" "AAPREPWEB-2" "PREPSQL-1" "PREPSQL-2" "PREPRDS-1" "PREPBI01" "SUPPORT-1")

## now loop through the above array
for i in "${host_list[@]}"
do
case ${i} in
[*AOS*])
  export resource_group="rg_app"
  ;;
[*SQL*])
  export resource_group="rg_sql"
  ;;
[*WEB*])
  export resource_group="rg_app"
  ;;
[*RDS*])
  export resource_group="rg_remote"
  ;;
[*BI*])
  export resource_group="rg_sql"
  ;;
[*SUPPORT*])
  export resource_group="rg_support"
  ;;
*)
  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 I execute it always goes to the last one and says Invalid hostname, please check.
What I want to do is declare a host array and each host comes in a different resource group, hence the case statement.

Once the export is done, it will come and execute azure cli for that host.

No idea why it is not working, I followed the syntax from here: http://tldp.org/LDP/Bash-Beginners-G...ect_07_03.html

Many thanks
 
Old 11-18-2018, 04:46 PM   #2
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Easy win, removed [ ] and its working but is there any way to consolidate *APP* and *WEB* into one? as for both its only one resource_group anyway.
 
Old 11-19-2018, 12:36 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
See above for solution and way to consolidate is to add a pipe (|) between option for individual case
 
1 members found this post helpful.
Old 11-19-2018, 04:44 AM   #4
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
@grail

Thank you, let me try
 
Old 11-19-2018, 06:03 PM   #5
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
@grail

Thank you, works like a charm!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using while loop & select statement - Loop issues Kustom42 Programming 4 05-17-2013 08:43 AM
Case statement in function not working cmosentine Programming 8 02-14-2012 12:24 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
Piping into a Case Statement embedded in a while loop telecom_is_me Programming 5 07-02-2008 04:45 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration