Jessica
Jessica

Reputation: 23

STDOUT to multiple variables

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!

Upvotes: 0

Views: 536

Answers (1)

user000001
user000001

Reputation: 33387

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

Upvotes: 1

Related Questions