Skip to content

Instantly share code, notes, and snippets.

@manualbashing
Last active December 9, 2020 23:19
Show Gist options
  • Save manualbashing/8f77079987a276e3fce9b8770703c78d to your computer and use it in GitHub Desktop.
Save manualbashing/8f77079987a276e3fce9b8770703c78d to your computer and use it in GitHub Desktop.
Loop over resources and add to terraform state file

Padding a Number with zeros:

j=12
Academy${(l:3::0:)j}
# ==> Academy012

Use arithmetic evaluation:

i=2
let "j = $i +1"
# $j ==> 3

And the whole thing in a loop:

for ((i = 0; i < 150; i++))
	do 
		let "j = $i +1"
		userName=Academy${(l:3::0:)j}
		terraform import "azurerm_storage_table_entity.AcademyUser[$i]" "https://sajedoxlab8idgyu4918oj.table.core.windows.net/AcademyUser(PartitionKey='$userName',RowKey='$userName')"
	done

And in a foreach loop:

for userName in Academy053 Academy056 Academy057 
do 
	let "i = $(echo $userName | sed 's/Academy0*//') - 1"
	terraform import "azurerm_storage_table_entity.AcademyUser[$i]" "https://sajedoxlab8idgyu4918oj.table.core.windows.net/AcademyUser(PartitionKey='$userName',RowKey='$userName')"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment