Turn command output into variable and trim variable in bash?
I have a command that I want to run from a bash script which is:
ec2-create-开发者_开发技巧volume -s 5 -z us-west-1c
I would like to turn the output of this command into a variable which is:
VOLUME vol-091b6065 5 us-west-1c creating 2011-06-02T23:13:02+0000
I would like to turn the output into a variable and trim everything out of the variable so all I am left with is a variable that will read "vol-091b6065"
Simple and straightforward:
ec2-create-volume -s 5 -z us-west-1c | cut -f2
or to directly assign it to a variable:
myvolid=$(ec2-create-volume -s 5 -z us-west-1c | cut -f2)
I use to use a aws_utils.sh script much like this, perhaps you can glean some tips from it:
#!/bin/bash
#set -o xtrace
#aws_numinstances=1
#aws_amiid=ami-1515f67c
#aws_delay=4
#aws_keyname=gsg-keypair
if [ -z "$aws_azone" ]
then
aws_azone=us-east-1b
echo selected default zone of $aws_azone
else
echo using configured zone of $aws_azone
fi
# handy in case we get interactively 'sourced' from a shell; make the temp(?) env var stick
export DEBUG
export VERBOSE
# unalias aws_debug
function aws_croak()
{
if [ ! -z "$VERBOSE" ]; then echo "$@"; fi
}
function aws_command()
{
if [ 1 != $# ]; then echo "WARNING: Quotes recommended (often required) on aws_command: '""$@""'" > /dev/stderr; fi
if [ ! -z $VERBOSE ]; then echo AWS: "$@" > /dev/stderr; fi
eval "$@"
}
function aws_debug()
{
if [ -z "$DEBUG" ]
then
aws_command "$@"
else
eval "echo AWS_DEBUG: '$@'" > /dev/stderr
fi
}
function aws_stubbing()
{
if [ -z "$DEBUG" ]
then
true
else
false
fi
}
function aws_launch()
{
aws_debug "ec2-run-instances $aws_amiid -k $aws_keyname -n $aws_numinstances -z $aws_azone|tee runlog|grep $aws_amiid|cut -f2"
aws_stubbing || echo "i-00000000"
}
function aws_all_inactive_volumes()
{
aws_command "ec2-describe-volumes|grep available|cut -f2"
}
# find any snapshots that don't have corresponding volumes (a) available or
# already attached to our instance (b) in the correct availability zone
function aws_unlinked_snaps()
{
# egrep -w "available|$aws_instanceid" |
sort <(aws_command "ec2-describe-volumes | grep -v delet | grep $aws_azone | cut -f4";
aws_command "ec2-describe-snapshots | cut -f2") |
uniq -u
}
function aws_instance_hostname()
{
aws_debug "ec2-describe-instances | grep $aws_azone | grep $aws_instanceid | cut -f4"
aws_stubbing || echo "dumnmy-ec2.sehe.nl"
}
function aws_instance_status()
{
aws_debug "ec2-describe-instances | grep $aws_azone | grep $aws_instanceid | cut -f6"
aws_stubbing || echo "running"
}
# select all volumes and attach them to our instance; these could fail if the
# volumes have already been attached to the instance
function aws_attach_all_available_volumes()
{
dev_index=0
for letter in g h i j k l m n o p q r s t u v w x y z; do devlist[${#devlist[@]}]=/dev/sd$letter; done
for volid in $(aws_command "ec2-describe-volumes | egrep -w available\|$aws_instanceid | grep $aws_azone | cut -f2")
do
devname=${devlist[$dev_index]}
dev_index=$(($dev_index+1))
aws_debug "ec2-attach-volume $volid -i $aws_instanceid -d $devname"&
done
}
# detach all volumes from our instance
function aws_detach_all_volumes()
{
for volid in $(aws_command "ec2-describe-volumes | grep -w attached | grep $aws_instanceid | cut -f2")
do
aws_debug "ec2-detach-volume $volid -i $aws_instanceid"&
done
}
# awaits all volume operations in our zone (attaching|detaching|creating)
# TODO:
# this might be improved by filtering the attaching/detaching operations to
# our aws_instanceid only
function aws_wait_volume_operations()
{
while aws_stubbing
do
# funky quoting construct ahead, sry;
# the tack-on 'true' is to avoid aborting if 'set -e' is in effect
response="$(aws_command "ec2-describe-volumes | grep $aws_azone | egrep -w 'attaching|busy|detaching|creating'; true")"
# aws_croak "response: '$response'"
if [ -z "$response" ]
then
break
else
aws_croak '(volumes not ready...)'
sleep $aws_delay
fi
done
}
aws_croak "(aws_utils.sh loaded)"
If the name always begins with vol-
, then you could use grep
:
ec2-create-volume -s 5 -z us-west-1c | grep -o "vol-[^ ]*"
You can do it with:
myvar=$(ec2-create-volume -s 5 -z us-west-1c | awk '{print $2}')
The following transcript shows that in action:
pax$ myvar=$(echo 'VOLUME vol-091b6065 blah blah' | awk '{print $2}')
pax$ echo $myvar
vol-091b6065
精彩评论