问题描述:

I have a custom program that runs from a shell and downloads a file(s) and also outputs some info about the file(s) it downloads?

 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!

网友答案:

Here is one of doing it in 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"

Output:

text RSA utf-8

Or with a case statement:

#!/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"

Regarding the Edit in the question, nothing really changes. You can do:

#!/bin/bash
for filename in "[email protected]"
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
相关阅读:
Top