carry0987
carry0987

Reputation: 127

How can I slide multiple variables by pipe in while (bash)?

I'm using bash to download file and auto change file name via wget, but when I use | (it's space pipe space) to distinguish the variables, it always read the first part of filename with space

Here is my script, I use while read -r fileName ' | ' url to read my download.list file

#!/usr/bin/env bash

set -e

IFS=' | '    

while read -r fileName url
do
    wget $url -O $fileName
done < download.list

exit 0

here is example text in my download.list MR HH GD | https://example/xxxx.zip I expect I will get MR HH GD.zip, but it actually give me this: ME.zip

Upvotes: 0

Views: 61

Answers (1)

James R.
James R.

Reputation: 143

I don't know how you got past the read command. In section "SHELL BUILTIN COMMANDS" of bash(1), the read command is defined as accepting variable names as arguments, not plain text.

You can use IFS like so, but you'll have to remove any unwanted whitespace yourself.

while IFS='|' read -r fileName url
do
    echo ">$fileName<"
    echo ">$url<"
done < download.list

MR HH GD | https://example/xxxx.zip in your download.list will output

>MR HH GD <
> https://example/xxxx.zip<

Upvotes: 1

Related Questions