【问题标题】:Regex with path?带路径的正则表达式?
【发布时间】:2013-02-12 11:59:13
【问题描述】:

在进行 iOS 开发时,我发现打开以下文件很有用:

/Users/disappearedng/Library/Application Support/iPhone Simulator/6.1/Applications/B957F50E-CF57-4797-AA14-C580F5596E56/Documents/MyApp.sqlite

目前我的 bash_profile 中有以下别名:

alias cdi='cd /Users/disappearedng/Library/Application\ Support/iPhone\ Simulator/6.1/Applications'

我不得不在这里停下来的原因是因为在我的 iOS 模拟器上每次新安装应用程序时,哈希 B957F50E-CF57-4797-AA14-C580F5596E56 都会发生变化。

有没有人知道一个别名的好方法,以便我可以将以下内容别名为我的 bash_profile 中的命令?

'/Users/disappearedng/Library/Application Support/iPhone Simulator/6.1/Applications/<any-hash>/Documents/*.sqlite'

我尝试对 has 使用通配符,但文件夹中存在 .DS_Cache 文件导致此操作失败。

【问题讨论】:

  • 哈希值总是一样的长度吗?

标签: ios bash


【解决方案1】:

怎么样:

find /Users/disappearedng/Library/Application Support/iPhone Simulator/6.1/Applications/.{38}/Documents/MyApp.sqlite | xargs <your_text_editor>

【讨论】:

    【解决方案2】:

    如果有多个匹配目录,并且您想输入最新的,请使用此脚本:

    cd "$(find "/Users/disappearedng/Library/Application Support/iPhone Simulator/6.1/Applications/" -name '*.sqlite' -type d -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")"
    

    【讨论】:

    • @EeroHelenius 对于ls,他需要过滤目录。它也必须是递归的。 parse the output of ls 通常被认为是不好的做法,但如果 printf 不可用,我想这是唯一的选择......
    • 对解析ls的输出很感兴趣,不知道;感谢您的链接。是的,我在第一次回复时有点仓促,对此感到抱歉; cd "$DIRNAME" &amp;&amp; cd "$(ls -t1 -d */ | head -1)" 之类的东西是我要发布的。 (是的,-printfisn't available on OS X)。
    • 是的,这可能会工作......不知道 OSX - 太糟糕了!
    • 您可以使用 Homebrew 安装 findutils 软件包,然后使用 gfind 而不是 find 如果您愿意,当然 - gfind 是 GNU 查找的,因此支持 @ 987654333@.
    【解决方案3】:

    这显然只是您在模拟器中运行时在开发过程中想要的东西。为什么不让您的应用程序在启动时获取文件的路径,然后将路径写入主目录中的文件。更新您的 .bash_profile 以获取此文件。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    #if TARGET_IPHONE_SIMULATOR
        NSString *sqlitePath = ... // path to the Documents folder
        NSString *command = [NSString stringWithFormat:@"alias cdi='cd %@'", sqlitePath];
        [sqlitePath writeToFile:@"/Users/disappearedng/.sqlitePath" atomically:YES encoding:NSUTF8StringEncoding error:nil];
    #endif
    }
    

    然后在你的.bash_profile,做:

    . ~/.sqlitePath
    

    【讨论】:

    • 有趣的解决方案。考虑到我们作为一个团队开发这个应用程序并且从应用程序本身检索我们的 bash 用户名可能很困难,这可能很难。但绝对有趣!
    【解决方案4】:

    您可以尝试将.DS_Cache 添加到FIGNORE shell 参数。这样,通配符应该只匹配一个目录,cd 仍然可以工作。在您的.bashrc 文件中:

    # To avoid adding .DS_Cache multiple times, just to be safe
    [[ $FIGNORE =~ .DS_Cache ]] || FIGNORE="$FIGNORE:.DS_Cache"
    

    【讨论】:

      猜你喜欢
      • 2020-09-30
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      相关资源
      最近更新 更多