简体   繁体   English

使用 cat 输出运行 bash 脚本的问题(额外引号)

[英]Problem with run bash script using cat output ( extra quotes )

I have a bash script in my apache directories that download some pictures and optimize them.我的 apache 目录中有一个 bash 脚本,可以下载一些图片并对其进行优化。

my script path is in : /var/www/site/storage/optimazer/photo_optimazer.sh我的脚本路径在: /var/www/site/storage/optimazer/photo_optimazer.sh

this script get some command from an txt file and pass it to wget此脚本从txt文件中获取一些命令并将其传递给wget


#!/usr/bin/env bash
..
THREAD="$(cat ${THREAD_FILE})";
$(command -v wget) $THREAD
...

and THREAD_FILE had this :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

I try to execute this bash with another one that was created at /usr/local/bin/optimaze.sh我尝试使用在/usr/local/bin/optimaze.sh创建的另一个 bash 执行此 bash

I had to do it cuz its would be run with system services.我不得不这样做,因为它会与系统服务一起运行。

here is the /usr/local/bin/optimaze.sh content这是/usr/local/bin/optimaze.sh内容

#!/usr/bin/env bash

cd /var/www/site/storage/optimazer/
$(command -v bash) photo_optimazer.sh

now, when I execute the optimaze.sh its add some extra quotes to my THREAD_FILE content and broke the script and I got some errors like this :现在,当我执行optimaze.sh它添加一些额外的报价我THREAD_FILE内容,并打破了剧本,我有一些像这样的错误:

--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’

I try set -ex in photo_optimazer.sh and see what happend我在 photo_optimazer.sh 中尝试set -ex看看发生了什么


 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

It add single quotes to my THREAD output and I don't know why!它在我的 THREAD 输出中添加了单引号,我不知道为什么!

I use GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)我使用 GNU bash,版本 4.2.46(2)-release (x86_64-redhat-linux-gnu)

If your arguments can't contain newlines, consider changing THREAD_FILE (better named all-lowercase, as thread_file , to stay out of the reserved all-caps namespace ) to be contain one argument per line, with no shell syntax whatsoever :如果您的参数不能包含换行符,请考虑更改THREAD_FILE (最好将其命名为全小写,作为thread_file ,以避开保留的全大写命名空间),使其每行包含一个参数,而没有任何 shell 语法

--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

Once you've done that, you can use (in bash 4.0 or later) readarray or mapfile to read each line of that file into a new array entry:完成后,您可以使用(在 bash 4.0 或更高版本中) readarraymapfile将该文件的每一行读入一个新的数组条目:

readarray -t wget_args <"$thread_file"

...and then expand that array onto your wget command line: ...然后将该数组扩展到您的 wget 命令行:

wget "${wget_args[@]}"

A note, about that "reserved all-caps namespace" claim made above: The POSIX standard only strictly requires POSIX-specified tools to use only all-caps names for environment variables that modify those tools' behavior.关于上面提出的“保留的全大写命名空间”声明的说明:POSIX 标准仅严格要求 POSIX 指定的工具仅对修改这些工具行为的环境变量使用全大写名称。 However:然而:

  • When an environment variable and a shell variable have the same name, any changes to the shell variable will also implicitly modify the environment variable .当环境变量和 shell 变量同名时,对 shell 变量的任何更改也将隐式修改环境变量
  • The purpose of POSIX tools being defined to use only all-caps variables is to make variable names with at least one lower-case variable safe for application use . POSIX 工具被定义为仅使用全大写变量的目的是使变量名至少包含一个小写变量,以便应用程序使用安全

When all-caps variables are used for application-defined purposes, doing so discards the benefits of the restrictions POSIX places on built-in tools.当全大写变量用于应用程序定义的目的时,这样做会放弃 POSIX 对内置工具施加的限制的好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM