STDOUT到多个变量

时间:2013-09-18 13:18:53

标签: bash shell variables stdout sh

我有一个自定义程序,它从shell运行并下载文件,并输出有关它下载的文件的一些信息吗?

    sh-3.1$ superdl -l username -p password filename
    Logging in...
    OK
    File: "filename"
    Size: "1100 bytes"
    Type: "text"
    Encryption: "RSA"
    Encoding: "utf-8"
    Done!

1 个答案:

答案 0 :(得分:1)

以下是在bash中执行此操作之一:

#!/bin/bash
while read -r var val
do
    [[ $var == Type: ]] && type="$val"
    [[ $var == Encryption: ]] && encryption="$val"
    [[ $var == Encoding: ]] && encoding="$val"
done < <(./superdl -l username -p password filename)

echo "$type $encryption $encoding"

输出:

text RSA utf-8

或者使用案例陈述:

#!/bin/bash
while read -r var val
do
    case "$var" in
    Type: )
        type="$val" ;;
    Encryption: )
        encryption="$val" ;;
    Encoding: )
         encoding="$val" ;;
    esac
done < <(./superdl -l username -p password filename)

echo "$type $encryption $encoding"

关于问题中的编辑,没有什么真正改变。你可以这样做:

#!/bin/bash
for filename in "$@"
do

    while read -r var val
    do
        [[ $var == Type: ]] && type="$val"
        [[ $var == Encryption: ]] && encryption="$val"
        [[ $var == Encoding: ]] && encoding="$val"
    done < <(./superdl -l username -p password "$filename")

    echo "$type $encryption $encoding"
done