在bash中将命令输出转换为变量和修剪变量?

时间:2011-06-02 23:40:05

标签: bash variables amazon-ec2

我有一个命令,我想从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”的变量

3 个答案:

答案 0 :(得分:3)

简单明了:

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 "$@"; 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)"

答案 1 :(得分:0)

如果名称始终以vol-开头,则您可以使用grep

ec2-create-volume -s 5 -z us-west-1c | grep -o "vol-[^ ]*"

答案 2 :(得分:0)

你可以用:

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