我有我想從一個bash腳本是運行一個命令:將命令輸出轉換爲bash中的變量和修剪變量?
ec2-create-volume -s 5 -z us-west-1c
我願把這個命令的輸出到一個變量是:
VOLUME vol-091b6065 5 us-west-1c creating 2011-06-02T23:13:02+0000
我會喜歡把輸出變成一個變量,並把變量全部從變量中刪除,所以我只剩下一個變量,它會讀取「vol-091b6065」
我有我想從一個bash腳本是運行一個命令:將命令輸出轉換爲bash中的變量和修剪變量?
ec2-create-volume -s 5 -z us-west-1c
我願把這個命令的輸出到一個變量是:
VOLUME vol-091b6065 5 us-west-1c creating 2011-06-02T23:13:02+0000
我會喜歡把輸出變成一個變量,並把變量全部從變量中刪除,所以我只剩下一個變量,它會讀取「vol-091b6065」
簡單而直接:
ec2-create-volume -s 5 -z us-west-1c | cut -f2
,或者直接將其分配給一個變量:
myvolid=$(ec2-create-volume -s 5 -z us-west-1c | cut -f2)
我用使用aws_utils.sh腳本喜歡這個,也許你可以從中收集一些提示:
#!/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 "[email protected]"; fi
}
function aws_command()
{
if [ 1 != $# ]; then echo "WARNING: Quotes recommended (often required) on aws_command: '""[email protected]""'" > /dev/stderr; fi
if [ ! -z $VERBOSE ]; then echo AWS: "[email protected]" > /dev/stderr; fi
eval "[email protected]"
}
function aws_debug()
{
if [ -z "$DEBUG" ]
then
aws_command "[email protected]"
else
eval "echo AWS_DEBUG: '[email protected]'" > /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)"
如果名字總是以vol-
開頭,那麼你可以使用grep
:
ec2-create-volume -s 5 -z us-west-1c | grep -o "vol-[^ ]*"
你可以做到這一點:
myvar=$(ec2-create-volume -s 5 -z us-west-1c | awk '{print $2}')
下面的記錄表明,在行動:
pax$ myvar=$(echo 'VOLUME vol-091b6065 blah blah' | awk '{print $2}')
pax$ echo $myvar
vol-091b6065
甜!這有助於創建所需的輸出。我將如何在bash腳本中運行該命令來使該輸出成爲變量?它只是variable_name「ec2-create-volume -s 5 -z us-west-1c | cut -f2」? – 2011-06-02 23:56:59
編輯答案;該腳本包含了如何使用這種方式使用EC2 API的結果的例子。 – sehe 2011-06-03 00:27:19