linux - 使用 cat 输出运行 bash 脚本的问题(额外引号)
问题描述
我的 apache 目录中有一个bash
脚本,可以下载一些图片并对其进行优化。
我的脚本路径在:/var/www/site/storage/optimazer/photo_optimazer.sh
该脚本从txt
文件中获取一些命令并将其传递给wget
#!/usr/bin/env bash
..
THREAD="$(cat ${THREAD_FILE})";
$(command -v wget) $THREAD
...
内容${THREAD_FILE}
:
$ cat "${THREAD_FILE}"
--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0" -np -r -l 1 -A "jpg" --ignore-case -P /var/www/optimazer/public/optimazed -x http://example.com
我尝试用另一个创建的 bash 来执行这个 bash/usr/local/bin/optimaze.sh
我不得不这样做,因为它将与系统服务一起运行。
这是/usr/local/bin/optimaze.sh
内容
#!/usr/bin/env bash
cd /var/www/site/storage/optimazer/
$(command -v bash) photo_optimazer.sh
现在,当我执行optimaze.sh
它时,在我的内容中添加一些额外的引号${THREAD}
并破坏了脚本,我得到了一些像这样的错误:
--2021-07-30 12:56:59-- http://(windows/
Resolving (windows ((windows)... failed: Name or service not known.
wget: unable to resolve host address ‘(windows’
--2021-07-30 12:56:59-- http://nt/
Resolving nt (nt)... failed: Name or service not known.
wget: unable to resolve host address ‘nt’
--2021-07-30 12:56:59-- http://10.0;/
Resolving 10.0; (10.0;)... failed: Name or service not known.
wget: unable to resolve host address ‘10.0;’
--2021-07-30 12:56:59-- http://win64;/
Resolving win64; (win64;)... failed: Name or service not known.
wget: unable to resolve host address ‘win64;’
--2021-07-30 12:56:59-- http://x64;/
Resolving x64; (x64;)... failed: Name or service not known.
wget: unable to resolve host address ‘x64;’
--2021-07-30 12:56:59-- ftp://rv/90.0)
=> ‘/var/www/scraper/public/***/3/rv/.listing’
Resolving rv (rv)... failed: Name or service not known.
wget: unable to resolve host address ‘rv’
--2021-07-30 12:56:59-- http://gecko/20100101
Resolving gecko (gecko)... failed: Name or service not known.
wget: unable to resolve host address ‘gecko’
--2021-07-30 12:56:59-- http://firefox/90.0%22
Resolving firefox (firefox)... failed: Name or service not known.
wget: unable to resolve host address ‘firefox’
我尝试set -ex
在 photo_optimizer.sh 中查看发生了什么
wget '--user-agent="Mozilla/5.0' '(Windows' NT '10.0;' 'Win64;' 'x64;' 'rv:90.0)' Gecko/20100101 'Firefox/90.0"' -np -A '"jpg,png"' --ignore-case --ignore-length -P /example/path -x http://example.com
它在我的输出中添加了单引号,我${THREAD}
不知道为什么!
我用GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
解决方案
对于这种特殊情况,一个想法是将每一行提供给xargs
.
对于示例数据,我将 OP 加倍$THREAD_FILE
:
$ cat tfile
--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0" -np -r -l 1 -A "jpg" --ignore-case -P /var/www/optimazer/public/optimazed -x http://example.com
--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0" -np -r -l 1 -A "jpg" --ignore-case -P /var/www/optimazer/public/optimazed -x http://example.com
第一次通过xargs
:
cat tfile | xargs -r wget
cat
或者我们可以通过将文件直接提供给以下方式来消除不必要的xargs
:
xargs -r -a tfile wget
A few variations on KamilCuk's comment/suggestion:
xargs -r < tfile wget
xargs -r wget < tfile
< tfile xargs -r wget
If we're dealing wih a variable (as with OPs example):
thread=$(head -1 tfile)
xargs -r wget <<< "${thread}"
And expanding on the <<< "${thread}"
example ... using this in a loop (eg, need to perform additional processing for each line from a multi-line input file):
while read -r thread
do
xargs -r wget <<< "${thread}"
done < tfile
All of these generate the following for each line processed:
--2021-07-31 13:50:41-- http://example.com/
Resolving example.com (example.com)... 93.184.216.34, 2606:2800:220:1:248:1893:25c8:1946
Connecting to example.com (example.com)|93.184.216.34|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1256 (1.2K) [text/html]
Saving to: ‘/var/www/optimazer/public/optimazed/example.com/index.html.tmp’
example.com/index.html.tmp 100%[================================================================================>] 1.23K --.-KB/s in 0.001s
2021-07-31 13:50:41 (1.25 MB/s) - ‘/var/www/optimazer/public/optimazed/example.com/index.html.tmp’ saved [1256/1256]
Removing /var/www/optimazer/public/optimazed/example.com/index.html.tmp since it should be rejected.
推荐阅读
- php - 从具有替代值的数据库表中检索所有组合
- c++ - 为什么 std:string 在比较相同文本的子字符串时不返回 0?
- symfony - 试图调用类的名为“remove”的未定义方法
- r - 在 R 中将推文标记为 ngram
- sql - DBeaver 中的 WHILE 循环给出语法错误
- javascript - 如何修复 chrome 扩展:拒绝运行 JavaScript URL,因为它违反了以下内容安全策略指令:
- node.js - 为什么第一次连接/请求到 MongoDB Atlas 数据库需要这么长时间?
- javascript - 如何使用 useEffect 挂钩从产品中获取类别?
- javascript - Laravel:删除 ID 错误的数据
- html - 轮播指示器处于活动状态