【问题标题】:I have a problem copying files in bash from subdirectories to parent directories我在将 bash 中的文件从子目录复制到父目录时遇到问题
【发布时间】:2020-08-19 00:14:53
【问题描述】:

大家好

我有一个 bash 脚本来安装基于 bash 的应用程序(它们只是 bash 脚本,我用Platypus 包装成一个应用程序。

我有我的应用程序需要复制到“安装应用程序”的资源文件夹中的父目录。

它是这样的:

AFolderWhereItGotDownloaded/
   Install.app/
      Contents/
         MacOS/
         Info.plist
         Resources/
            Application1.app/ <-- This is the application that I want to move to "AFolderItGotDownloaded"
            Application2.app/ <-- This is the application that I want to move to "AFolderItGotDownloaded"
            AppSettings.plist
            AppIcon.icns
            MainMenu.nib
            script <-- This is the script that runs when I double click the app

我唯一想做的就是cp -rrsync -r这两个申请AFolderWhereItGotDownloaded/

到目前为止我做了什么:

#!/bin/bash

#The directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null 2>&1 && pwd )"

#The "parent" directory
PARENTDIR="../../.."

cp -r "${DIR}/Application1.app" "${PARENTDIR}/Application1.app"
cp -r "${DIR}/Application2.app" "${PARENTDIR}/Application2.app"

我得到了什么:

cp: ../../../Application1.app: Permission denied
cp: ~/Downloads/AFolderWhereItGotDownloaded/Install.app/Contents/Resources/Application1.app: unable to copy extended attributes to ../../../Application1.app: Permission denied

使用 rsync

#!/bin/bash

#The directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null 2>&1 && pwd )"

#The "parent" directory
PARENTDIR="../../.."

rsync -r "${DIR}/Application1.app" "${PARENTDIR}/Application1.app"
rsync -r "${DIR}/Application2.app" "${PARENTDIR}/Application2.app"

我得到了什么:

rsync: mkdir "~/../../../Application1.app" failed: Permission denied (13)
rsync error: error in file IO (code 11) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52.200.1/rsync/main.c(545) [receiver=2.6.9]
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52.200.1/rsync/io.c(453) [sender=2.6.9]

如果有人知道发生了什么,那就太好了......

OS: macOS Mojave (10.14.6)

文件信息窗口显示我有权读取和写入文件夹。

ls -l的结果

在 AFolderWhereItGotDownloaded 内

total 16
drwxr-xr-x@ 3 <myusername>  staff    96  4 mai 00:13 Install.app
-rwxr-xr-x@ 1 <myusername>  staff  1069 28 avr 13:20 LICENSE.txt
-rwxr-xr-x@ 1 <myusername>  staff  2586  3 mai 15:05 README.md

在 Install.app 中

total 0
drwxr-xr-x@ 5 <myusername>  staff  160  4 mai 00:13 Contents

内幕

total 8
-rwxr-xr-x@  1 <myusername>  staff  632  3 mai 14:56 Info.plist
drwxr-xr-x@  3 <myusername>  staff   96  4 mai 00:13 MacOS
drwxrwxrwx@ 17 <myusername>  staff  544  4 mai 13:43 Resources

内部资源

total 728 (I have other apps too but to keep it clean I only stated two "Applications" here)
-rwxrwxrwx@ 1 <myusername>  staff  313202  3 mai 14:54 AppIcon.icns
-rwxrwxrwx@ 1 <myusername>  staff     499  3 mai 14:56 AppSettings.plist
drwxrwxrwx@ 3 <myusername>  staff      96  4 mai 00:13 Application1.app
drwxrwxrwx@ 3 <myusername>  staff      96  4 mai 00:13 Application2.app
-rwxrwxrwx@ 1 <myusername>  staff   42236  3 mai 14:56 MainMenu.nib
-rwxrwxrwx@ 1 <myusername>  staff     940  4 mai 14:29 script

【问题讨论】:

    标签: bash macos shell terminal permissions


    【解决方案1】:

    PARENTDIR="../../.." 是错误的。如果您执行cd ~; pwd,它将显示类似/home/user 的内容。由于这是两个深度,您应该改用PARENTDIR="../.."

    此外,您必须具有类似 root 的权限(root、sudo)才能在根目录中创建文件。最好将/opt 中的软件添加到用户PATH 变量中,例如export PATH=$PATH:/opt/myapp/bin/。对于/opt,您还需要类似root 的权限。

    【讨论】:

    • 谢谢!这就是我想出来的,我实际上正在研究一种使用 Platypus 获得类似 root 权限(使用 sudo)的方法
    【解决方案2】:

    实际上,问题在于 macOS 对从 Web 下载的应用程序进行沙盒处理:使其正常工作的唯一方法是将应用程序放入 Applications 文件夹中。

    【讨论】:

      猜你喜欢
      • 2019-04-04
      • 1970-01-01
      • 2012-12-03
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2019-12-08
      相关资源
      最近更新 更多