【问题标题】:Why is my script not going up one level when using /../?为什么我的脚本在使用 /../ 时没有上一级?
【发布时间】:2011-06-02 14:30:34
【问题描述】:

我的理解是,在 bash 中的文件位置使用“/../”会提升一级。这就是我在这里尝试做的事情:

  for box in {0..4}
  do
   for lvl in {0..24}
   do
    key="UNLOCKED_${box}_$lvl"
    plutil -key "$key" -value '1' "$appdir/../Library/Preferences/com.chillingo.cuttherope.plist" 2>&1> /dev/null
    #successCheck=$(plutil -key "$key" "/$appdir/../Library/Preferences/com.chillingo.cuttherope.plist")
    #if [ "$successCheck" -lt 1 ]; then
    # echo "Level ${box}-$lvl failed! "
    #fi
   done
  done

但我不断收到此错误 (x125):

Error: File not found at path /var/mobile/Applications/1E17CC78-AA6E-4FFA-B241-74A73FE3AB0E/CutTheRope.app/../Library/Preferences/com.chillingo.cuttherope.plist

任何帮助将不胜感激/ 谢谢。

【问题讨论】:

    标签: bash scripting path


    【解决方案1】:

    / 表示从文件系统或上下文的根目录开始,../ 表示上一层。 /../ 有效地抵消了两者。

    要上一级,请尝试 ../

    编辑:换句话说,尝试从路径中的变量末尾删除 /。

    编辑 #2:最好的解决方案可能是删除 /../

    编辑#3:

    这可行:

      /var/mobile/Applications/1E17CC78-AA6E-4FFA-B241-74A73FE3AB0E/CutTheRope.app/Library/../Library/Preferences/
    

    如果你删除了第二个库和 .. 它也应该可以工作:

     /var/mobile/Applications/1E17CC78-AA6E-4FFA-B241-74A73FE3AB0E/CutTheRope.app/Library/Preferences/
    

    【讨论】:

    • 那么,我会做“${appdir}../Library/Preferences/plistname.plist”吗?
    • 是的 :) 你是对的。我认为...尽管删除 .. 可能会更容易。
    • 当我执行 ${appdir}../Library/Preferences/plistname.plist 时,我得到:错误:在路径 /var/mobile/Applications/1E17CC78-AA6E-4FFA-B241 找不到文件-74A73FE3AB0E/CutTheRope.app../Library/Preferences/com.chillingo.cuttherope.plist
    • cd /home/james/../james - 当我从命令提示符尝试该操作时,我最终会在同一个地方,就像我去 cd /home/james。因此,除非 appdir 指向 Library 目录,否则您可能希望完全删除 .. 。换句话说,我可能会改变我原来的答案;)
    • 您的 appdir 看起来不像以 /Library 结尾:/var/mobile/Applications/1E17CC78-AA6E-4FFA-B241-74A73FE3AB0E/CutTheRope.app
    【解决方案2】:

    查找过程如下:

    inode lookup(string path) {
      inode cur = path[0] == '/' ? process->root_directory : process->current_directory;
      foreach component in split(path, '/') {
        inode next = cur.get_entry(component);
        if (next is a file && the component is the last one)
          return;
        if (next is a directory)
          cur = next;
        else
          return null;
      }
      return cur;
    }
    

    .. 没有特殊处理。关键是文件只能作为路径的最后一个组成部分出现。如果之前出现过,则会采用return null 路径。

    实际的实现(例如NetBSD)有点复杂。

    更新:我刚刚看到你的问题$appdir 可能会指向一个目录,所以上面的讨论不适用。尽管如此,仍可能涉及一些符号链接。当您跟随一个符号链接然后继续 .. 时,lookup 函数将从符号链接指向的位置继续。一个小例子:

    $ ln -s somewhere/else/deep/in/the/path symlink
    $ ls -l symlink/..
    

    这将解析为somewhere/else/deep/in/the(前提是它存在)。 lookup 函数不会将路径 symlink/.. 简化为 .,或者作为另一个示例,first/../second 不会简化为 second

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2019-05-17
      • 1970-01-01
      • 2010-09-05
      • 2013-02-12
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多