简体   繁体   English

在 unix 中执行测试命令时导致问题的变量:: find $PWD -type d -exec sh -c 'test "{}" ">" "$PWD/$VersionFolders"'; -打印|wc -l`

[英]Variable causing issue while doing the test command in unix:: find $PWD -type d -exec sh -c 'test "{}" ">" "$PWD/$VersionFolders"' ; -print|wc -l`

Variable causing issue while doing the test command in unix Command is::在 unix 命令中执行测试命令时导致问题的变量是:

find $PWD -type d -exec sh -c 'test "{}" ">" "$PWD/$VersionFolders"' \; -print|wc -l`

Input Values-输入值-

Here $PWD- Current Directory这里 $PWD- 当前目录

b1_v.1.0 b1_v.1.2 b1_v.1.3 b1_v.1.4 b1_v.1.0 b1_v.1.2 b1_v.1.3 b1_v.1.4

Given Version folder as $VersionFolders b1_v.1.2给定版本文件夹为 $VersionFolders b1_v.1.2

The Command should check if any folders exist in current directory which is greater than the give version folder and it should count or display.该命令应检查当前目录中是否存在任何大于给定版本文件夹的文件夹,并且它应该计数或显示。 This approach has to be consider with out date or time created of folders.这种方法必须考虑到创建文件夹的日期或时间。

Expected Output- b1_v.1.3 b1_v.1.4预期输出 - b1_v.1.3 b1_v.1.4

If I give hard code Directories its working fine.如果我给硬代码目录它工作正常。 But when I pass it like as variable.it give all folders.但是当我像变量一样传递它时,它会给出所有文件夹。

working fine this commend-工作正常这个表扬-

find $PWD -type d -exec sh -c 'test "{}" ">" "$PWD/b1_v.1.2"' \; -print|wc -l`

Not working this command with variable- find $PWD -type d -exec sh -c 'test "{}" ">" "$PWD/$VersionFolders"';不使用此命令与变量 - find $PWD -type d -exec sh -c 'test "{}" ">" "$PWD/$VersionFolders"'; -print|wc -l` -打印|wc -l`

The variable $VersionFolders won't get expanded inside the single quotes, and apparently you did not export it to make it visible to subprocesses.变量$VersionFolders不会在单引号内扩展,显然您没有export它以使其对子进程可见。

An obscure but common hack is to put it in $0 (which nominally should contain the name of the shell itself, and is the first argument after sh -c '...' ) because that keeps the code simple.一个晦涩但常见的 hack 是将它放在$0中(名义上应该包含 shell 本身的名称,并且是sh -c '...'之后的第一个参数)因为这样可以使代码简单。

find . -type d \
  -exec sh -c 'test "$1" ">" "$0"' \
    "$VersionFolders" {} \; \
  -print | wc -l

But as @chepner remarks, you can run -exec test {} ">" "$VersionFolders" \;但正如@chepner 所说,您可以运行-exec test {} ">" "$VersionFolders" \; directly.直接地。

The shell already does everything in the current directory, so you don't need to spell out $PWD . shell 已经完成了当前目录中的所有操作,因此您无需拼写$PWD Perhaps see also What exactly is current working directory?也许另请参阅当前工作目录到底是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM