用变量替换命令?

时间:2019-05-14 12:25:05

标签: bash variables command debian

#!/bin/bash
```
download()
{
    local url=$1
    echo -n "    "
    wget -nc --progress=dot $url 2>&1 | grep --line-buffered "%" | \
    sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
    echo -ne "\b\b\b\b"
    echo " DONE"
}
```
file="adaway.org.txt"
echo -n "Downloading $file:"
download "https://raw.githubusercontent.com/EnergizedProtection/block/master/assets/active/filter/$file"

这仍然很粗糙,但是可行。我只想做一些变量来缩短底部,并从cat文件中读取我的bash。

1 个答案:

答案 0 :(得分:0)

您的问题尚不清楚,但听起来这可能是您要执行的操作:

$ cat a.txt
http://google.com/foo
http://yahoo.com/stuff/bar

$ cat tst.sh
#!/bin/env bash

input="$1"
while IFS= read -r line; do
    path="${line%/*}"
    file="${line##*/}"
    printf '\nline="%s"\n' "$line"
    printf 'path="%s"\n' "$path"
    printf 'file="%s"\n' "$file"
    echo download "${path}/${file}"
done < "$input"

$ ./tst.sh a.txt

line="http://google.com/foo"
path="http://google.com"
file="foo"
download http://google.com/foo

line="http://yahoo.com/stuff/bar"
path="http://yahoo.com/stuff"
file="bar"
download http://yahoo.com/stuff/bar

如果没有,请编辑您的问题以澄清。