【问题标题】:Bash - command works in console but not in scriptBash - 命令在控制台中有效,但在脚本中无效
【发布时间】:2015-08-29 11:53:23
【问题描述】:

我有一个命令:printf "\x$(printf %x $char)"。 当我在控制台中写:char=65; printf "\x$(printf %x $char)"; 结果是写信A。 但是当我运行我的脚本时:

#!/bin/bash
char=65;
printf "\x$(printf %x $char)"

结果写成:\x41 我尝试在脚本中的命令printf 之前写sudo,然后命令按预期工作。我需要找到一个不需要使用sudo的解决方案。

【问题讨论】:

  • 为我工作。正如预期的那样,复制上面的脚本并执行我会导致“A”。只有当您删除最后一行的双引号时,情况才会发生变化。
  • 您确定使用 bash 执行脚本,并且没有意外使用其他 shell(sh script.shsh 不指向 bash)?
  • 你可能用sh script_path运行它,用bash script_path运行它...
  • 仅仅因为某些命令不能按预期工作而使用 sudo 是一个可怕的想法。只有在需要以 root 身份运行某些东西时才应该使用 sudo。
  • 我使用的是 sh script_path 而不是 bash scirpt_path - 适用于 bash script_path。谢谢!

标签: linux bash shell command


【解决方案1】:

通过运行 sh script.shaccidentally run your script using dash instead of bash,其内置的 printf 不支持 \xHH 文字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多