shell - 如何在 Unix 上的 Makefile 中获取文件大小?

标签 shell unix makefile

我想将其作为 Makefile 任务来实现:

# step 1:
curl -u username:password -X POST \
  -d '{"name": "new_file.jpg","size": 114034,"description": "Latest release","content_type": "text/plain"}' \
  https://api.github.com/repos/:user/:repo/downloads

# step 2:
curl -u username:password \
-F "key=downloads/octocat/Hello-World/new_file.jpg" \
-F "acl=public-read" \
-F "success_action_status=201" \
-F "Filename=new_file.jpg" \
-F "AWSAccessKeyId=1ABCDEF..." \
-F "Policy=ewogIC..." \
-F "Signature=mwnF..." \
-F "Content-Type=image/jpeg" \
-F "file=@new_file.jpg" \
https://github.s3.amazonaws.com/

但是,在第一部分中,我需要获取文件大小(以及内容类型,如果很简单,但不是必需的),因此存在一些变量:

{"name": "new_file.jpg","size": $(FILE_SIZE),"description": "Latest release","content_type": "text/plain"}

我尝试了这个,但它不起作用(Mac 10.6.7):

$(shell du path/to/file.js | awk '{print $1}')

有什么想法可以实现这一点吗?

最佳答案

如果您有 GNU coreutils:

FILE_SIZE=$(stat -L -c %s $filename)

-L 告诉它遵循符号链接(symbolic link);如果没有它,如果 $filename 是符号链接(symbolic link),它将为您提供符号链接(symbolic link)的大小,而不是目标文件的大小。

MacOS stat等效项似乎是:

FILE_SIZE=$(stat -L -f %z)

但是我没能尝试过。 (我将其编写为 shell 命令,而不是 make 命令。)您可能还会发现 -s 选项很有用:

Display information in "shell output", suitable for initializing variables.

关于shell - 如何在 Unix 上的 Makefile 中获取文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12065362/

相关文章:

linux - 在 Linux 启动时运行时我的应用程序速度很慢

python - 尝试除了 IndentationError

unix - 单引号在 shell 脚本中不起作用

perl - 检查一个数字是否位于两个值之间

build - 如何从 cmake 中的列表中删除标记?

c++ - cmake 使用 *.cpp 规则创建 Makefile

Eclipse Juno-突然 “cannot run program ' make' : unknow reason”

linux - 向我发送电子邮件并重启 tomcat 攻击 DDOS 的脚本

linux - 如何在 UNIX 中获得与管理员完全相同的权限

macos - 在 Mac OS shell 脚本中使用日期