linux scripting with a variable referring to id's in a text file OR mass citrix vm snapshot delete

StackOverflow https://stackoverflow.com/questions/4227222

  •  26-09-2019
  •  | 
  •  

Question

I have a command (citrix xenserver cli delete snapshot command--if it matters) that I have to repeat a bunch of times:

xe snapshot-uninstall snapshot-uuid=[snapshot-uuid]

I want to script this using a variable for [snapshot-uuid] so that I can repeat for all lines in a text file that looks like this (each line is a unique uuid wth no whitespace):

dd56e7d2-00b0-4ddd-b3bf-368e3de059f9
ba201e7e-c180-4e00-8134-2ea128c9a14e
32128f38-b1b5-40e7-848c-dbdf5ae255ed
b53145ec-3748-475d-8add-008665533f72
173cb6c4-04da-43bc-abce-f2c97f76d3d2
3829f365-e619-47a0-93cd-d5aca37c28dd
6eb7aff9-70f9-4a77-96b0-044dfb0ce96e
ca7478c6-eae1-4090-aed5-c348b172d12a
439e247d-cf6a-4f09-a4d7-8dc719c317ca
b595d2e8-e2c4-4d0e-9b07-cccf1108904b
1da57650-dc16-48e2-bf5f-e2b8b96dba03
f2718a31-baa8-4c74-b170-5933e71da8c4
b2263c61-175e-43fa-b941-52ec2379a835
d0d6ede9-0589-4908-884f-900fef1d1c92
65e54f20-8c33-4f67-a394-36676add54ac
6d0f388c-5a78-4962-b1b2-55117b18a4c9

An additional complication is that with each command an "are you sure... type 'yes' to continue" is given.

UPDATE: I originally posted this question because my question on Citrix's Xenserver CLI forum had been ignored. I did later get a response on the Citrix forum here, which included a command that is far more efficient than what I was trying to do. Please, if ever anyone has a need to delete all snapshots, use the following:

for i in `xe snapshot-list --minimal | sed -e 's/,/\ /g'` ; do xe snapshot-uninstall force=true uuid=$i ; done
Was it helpful?

Solution

You can create a shell script to loop over each line:

#!/bin/bash

while read UUID; do
    yes | xe snapshot-uninstall snapshot-uuid="$UUID"
done < uuids.txt

Or if you're a good typist you can enter all three lines of the while loop directly at your shell.

You can auto-answer any prompts by piping the output of yes. yes will repeatedly print the string of your choice (default "y"):

Name

yes - output a string repeatedly until killed

Synopsis

yes [STRING]...

Description

Repeatedly output a line with all specified STRING(s), or ‘y’.

OTHER TIPS

xargs -I % -a uuidfile xe snapshot-uninstall snapshot-uuid=%

To respond to "are you sure" prompts:

xargs -I % -a sh -c 'yes | uuidfile xe snapshot-uninstall snapshot-uuid=%'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top