【问题标题】:How to use a $variable in `ls` command?如何在`ls`命令中使用$变量?
【发布时间】:2020-07-12 01:02:40
【问题描述】:

我在将名为 $discogs 的变量传递给脚本中的 ls 命令时遇到问题。

当我不运行脚本时,我可以回显变量并在 shell 中正确运行命令,但是在脚本中会产生错误。

这是我的代码:

echo "I will search for the Artist ID Number and return the Artist Name"
read id
cat file--2020-04-26.xml | grep -A 1  '<artist id="'$id'">' | tail -n 1 |  cut -c 9- | rev | cut -c 8- | rev
artist=$(cat file--2020-04-26.xml | grep -A 1  '<artist id="'$id'">' | tail -n 1 |  cut -c 9- | rev | cut -c 8- | rev)
discogs=$(curl --silent  https://athena.example.com/discogs/get_discogs_id.php?Artist="'$artist'")
echo $artist
echo Discogs ID:
echo $discogs
ls -l /root/athena/scrape/icons/png/"'*'$discogs'*'"
ls -l /root/athena/scrape/icons/png/"*${discogs}*"

错误:

ls: cannot access '/root/athena/scrape/icons/png/'\''*'\'''$'\r\n\r\n\r\n''1103159'\''*'\''': No such file or directory
ls: cannot access '/root/athena/scrape/icons/png/*'$'\r\n\r\n\r\n''1103159*': No such file or directory

【问题讨论】:

  • \r 表示 Windows 行尾,也就是回车。
  • 谢谢,虽然我怎样才能在命令中正确使用变量?
  • 您应该删除 * 符号周围的单引号,并从 curl 命令的结果中检查和删除换行符 (cr/lf)。
  • 我已经尝试过了,但我仍然遇到问题。我可以回显 $discogs 并且我可以看到变量 - 它只是一个数字。
  • od -An -tx1 &lt;&lt;&lt; "$discogs" 的输出是什么?

标签: bash shell ubuntu-16.04 ls


【解决方案1】:

尝试通过| tr -d '[:space:]' 传递您的curl 结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 2022-01-08
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多