AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-168003

el-teedee's questions

Martin Hope
el-teedee
Asked: 2019-03-07 13:32:07 +0800 CST

如何防止围绕我要解析的变量进行参数扩展?

  • 4

编辑:

进一步阅读之前的重要说明:自从Kusalananda 的 anwser以来,我的问题现在已经毫无用处了,因为他指出了我使用的另一个脚本中的错误。现在这个bug被修复了,我描述的下面的错误不再发生了!

您的代码中有两个问题导致 $extension 中的值被扩展
第二个问题是函数本身


TL;博士

当我开始写这个问题时,我被卡住了,但找到了一个可行的解决方案(使用shell noglob),但我仍然有一个“是否可以写”的问题。

我确信这已经得到解答,但是我迷失在数百个关于 bash 扩展的主题中。

这是我的情况:

  • 我想运行一个脚本(可以说带有接受正则表达式ffmpeg的选项模式)-pattern_type glob -i*.JPG
  • 争论(假设*.JPG)
  • 来自变量的参数(比如说$extension)
  • 来自脚本参数的变量(比如说$1)
  • 但我不希望 shell$extension默认扩展为像这样的模式*.JPG

请注意,所有这些操作都发生在脚本script.sh中。

因此,用户可以运行:

script.sh 'JPG' ; # where $1='JPG'

注意:在 中script.sh,我连接*.并$1获得更最终的正则表达式,例如*.JPG

我读到(从这里https://stackoverflow.com/a/11456496/912046)外壳扩展发生在ffmpeg接收参数之前(ffmpeg甚至不知道外壳扩展发生)

我尝试以不同的方式使用引号、双引号或反斜杠${extension},但没有成功。
以下是一些尝试:

ffmpeg ... "${extension}"

导致:(ffmpeg 1.jpg 2.jpg [...]发生扩展)

ffmpeg ... ${extension}

相同的

ffmpeg ... '${extension}'

由于搜索模式导致没有文件匹配${extension}(按字面意思使用变量名)

extension="\'${extension}\'" ; # wrapping the single quotes (preventing expansion) directly in variable
ffmpeg ... ${extension}

'*.jpg'由于搜索模式(包括单引号)导致没有文件匹配

ffmpeg ... \"${extension}\"

相同但搜索"*.jpg"(包括双引号)

我终于成功使用了 Shell noglob 选项。
这是好奇的命令:

set -f ; # disable glob (prevent expansion on '*.jpg')
ffmpeg -pattern_type glob -i ${extension} movie.mp4 ;
set +f ; # restore, but what if glob was already disabled? I should not restore it then?

但是出于我的好奇心,是否有另一种方法可以防止来自我们想要解决的变量的脚本参数扩展?(因此,我知道防止扩展(使用单引号)的方式使变量不再解析/替换)。

类似的东西:(但我读到应该避免字符串连接以确保安全,注入)

ffmpeg '${extension}'
       ||           | 3
       || 2
       | 1

在哪里 :

  • 1:将为最终结果打开单引号ffmpeg '*.jpg'
  • 2:将我们的变量注入并解析extension为.jpg
  • 3:会关闭单引号ffmpeg来接收参数,就像我手动写的一样'*.jpg'

编辑:在评论如何分配扩展变量后:

local extension="${2}" ;
echo $extension ;
extension="${extension:=jpg}" ; # Default on "jpg" extension
# wrapp extension type in single quotes + prefix with "*."
extension="*.${extension}" ;

然后,将其用于:

runprintcommand ffmpeg -loglevel verbose -pattern_type glob -i ${extension} "$movieName" ;

runprintcommand函数/脚本在哪里:

function runprintcommand() {
    echo "Command to run: (Note: '\\' escape character may not be printed)" ;
    echo "$*" ;
    $* ;    
}
bash shell-script
  • 3 个回答
  • 5493 Views
Martin Hope
el-teedee
Asked: 2018-02-12 10:54:48 +0800 CST

将脚本输出重定向到文件时防止 tput 转义序列

  • 3

我有一个 shell 函数,它使用tput.

color=$( tput setaf 1 )
normal=$( tput sgr0 )
colorize() {
    echo "${color}$1$(normal)"
}

当我调用该函数时,终端会按预期显示彩色文件名。

$ myScript.sh .
/path/to/dir # (in color red)

然后,如果我将输出重定向到文件:

$ myScript.sh > /tmp/file.log

file.log仍然包含转义序列,例如:

^[[36m~/path/to/my/file/^[(B^[[[m

这可能与TERM和infocomp以及终端如何解释转义序列有关吗?

这个想法是模拟一个“非显示”终端,不是吗?

我的术语是(Ubuntu 16.04):

$ echo $TERM
xterm

tput当我的脚本被重定向到文件时,我应该怎么做才能防止这种转义序列?

解决方法:在我的脚本中添加一个选项以手动禁用着色ls,grep如--color=none.

terminal io-redirection
  • 1 个回答
  • 1244 Views
Martin Hope
el-teedee
Asked: 2018-02-12 07:20:07 +0800 CST

错误替换:heredoc / EOF 中没有关闭“`”

  • 8

我想打印一个男人风格的用法消息来描述这样的输出man find:

NAME
       find - search for files in a directory hierarchy

SYNOPSIS
       find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]

DESCRIPTION
       This  manual  page  documents the GNU version of find.  GNU find searches the directory tree rooted at each
       given starting-point by evaluating the given expression from left to  right,  according  to  the  rules  of
       precedence  (see section OPERATORS), until the outcome is known (the left hand side is false for and opera‐
       tions, true for or), at which point find moves on to the next file name.  If no  starting-point  is  speci‐
       fied, `.' is assumed.

OPTIONS

我在 ` 字符上遇到错误消息。
以下简单脚本显示错误:

~$ cat <<EOF
`.'
EOF

bash: bad substitution: no closing "`" in `.'

我虽然heredoc是一种很酷的方式来通过粘贴来回显字符串,而不必转义其内容,例如引号等...... 我认为我错了:/

有人可以解释一下这种行为吗?可以heredoc接受`字符吗?

编辑 2:我接受了引用的 here-document <<'END_HELP'的答案,但我最终不会将它用于这种完整的手动输出,因为kusalananda确实建议

编辑 1:(供将来阅读)使用引用的 here-document的限制是防止tput在here-document.
为此,我执行了以下操作:

  1. 未加引号here-document,用于tput执行命令
  2. 通过转义反引号来防止“错误替换”错误
  3. tput内 使用here-document

例子:

normal=$( tput sgr0 ) ;
bold=$(tput bold) ;

cat <<END_HELP # here-document not quoted
${bold}NAME${normal}
       find - search for files in a directory hierarchy

${bold}SYNOPSIS${normal}
       find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]

${bold}DESCRIPTION${normal}
       This  manual  page  documents the GNU version of find.  GNU find searches the directory tree rooted at each
       given starting-point by evaluating the given expression from left to  right,  according  to  the  rules  of
       precedence  (see section OPERATORS), until the outcome is known (the left hand side is false for and opera‐
       tions, true for or), at which point find moves on to the next file name.  If no  starting-point  is  speci‐
       fied, \`.' is assumed.
END_HELP

unset normal ;
unset bold ;

在这里,请注意作为错误来源的转义反引号:

\`.'
escape-characters command-substitution
  • 2 个回答
  • 5858 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve