【问题标题】:bash script not seeing a file [duplicate]bash脚本没有看到文件[重复]
【发布时间】:2023-03-17 11:18:01
【问题描述】:

谁能向我解释为什么这不起作用?

ls ~\ 确实表明 cheatsheet 在主目录中

  1 #! /bin/bash
  2         let i=0
  3         cheatsheet="~/cheatsheet"
  4         until [ $i -eq $# ]
  5         do
  6                 grep -e $"$i" -e "^\s" $cheatsheet
  7                 i=$(expr $i + 1 )
  8         done    
  9         if [ -z $1  ]
 10         then 
 11                 grep -e "^\w" $cheatsheet
 12         fi 

【问题讨论】:

  • ~ 在扩展变量时不被处理。
  • 另外,您不能使用$"$i" 访问参数i。您需要使用变量间接:${!i}
  • @Barmar 实际上我从来不知道,谢谢
  • 我觉得还是用while [ $# -gt 0 ]; do ...; shift; done比较好

标签: linux bash


【解决方案1】:

试试

cheatsheet = "$HOME/cheatsheet"

注意:~ 不能用于变量。

【讨论】:

    【解决方案2】:

    波浪号不会在双引号变量上下文中展开。你可以使用$HOME 喜欢

    cheatsheet="$HOME/cheatsheet"
    

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 2014-12-12
      • 2018-09-24
      • 1970-01-01
      • 2013-07-24
      相关资源
      最近更新 更多