Unix的"发现"命令用法 [英] Unix "find" command usage
问题描述
这是一个bash的安装脚本。该脚本foo.sh以目录的参数。我说,有一个目录< $ HOME> /测试/ TEST_1A / TEST_2A / TEST_3
,另外DIR < $ HOME> /测试/ TEST_1B / TEST_2B / TEST_3
。
This is for a bash installation script. The script foo.sh takes "DIRECTORY" as an argument. Say, there is a dir <$HOME>/TEST/TEST_1A/TEST_2A/TEST_3
and another dir <$HOME>/TEST/TEST_1B/TEST_2B/TEST_3
.
脚本:foo.sh简要
Script: foo.sh in brief.
DIR='find $HOME -type d -name $1 | head 1'
if [ DIR is set to a directory ]
then
rm -rf $DIR
fi
exit 0
用法: foo.sh TEST_3
现在从剧本,只有&LT; $ HOME&GT; /测试/ TEST_1A / TEST_2A / TEST_3
即可消除。要删除&LT; $ HOME&GT; /测试/ TEST_1B / TEST_2B / TEST_3
,我需要在我find命令使用一个reg EXP,微调捞出来解决目录冲突。
Now from the script, only the <$HOME>/TEST/TEST_1A/TEST_2A/TEST_3
can be removed. To remove <$HOME>/TEST/TEST_1B/TEST_2B/TEST_3
, I need to use a reg exp in my find command, to fine tune the remove to resolve the directory conflict.
修改上面的脚本的一部分,找到如下
Modified the find part of the above script as below
DIR='find $HOME -type d -regexp $1 | head 1'
新用法: foo.sh TEST_2B / TEST_3
但查找命令无法获得 DIR
设置为&LT; $ HOME&GT; /测试/ TEST_1B / TEST_2B / TEST_3
,而是返回空&安培;结果 DIR
是空的,我可从来没有删除&LT; $ HOME&GT; /测试/ TEST_1B / TEST_2B / TEST_3
But "find" command FAILS to get the DIR
set to <$HOME>/TEST/TEST_1B/TEST_2B/TEST_3
and instead returns empty & as a result DIR
is empty and I can never ever remove <$HOME>/TEST/TEST_1B/TEST_2B/TEST_3
我如何改变脚本,这样可以找到对刚刚目录名行事,以及道路上的目录也没有问题。逸岸,一些用户可能会部分的目录路径作为参数传递给foo.sh。我希望foo.sh的工作,即使在这种情况下,
How do I change the script, so that find can act on JUST the directory name, as well as on the path to the directory too with NO issues. Infact, some users may give a partial directory path as argument to "foo.sh". I expect "foo.sh" to work, even in such cases
推荐答案
您可以尝试:
if [[ "$1" == */* ]]; then
EXPR="-path *$1"
else
EXPR="-name $1"
fi
DIR=$(find $HOME -type d $EXPR | head -1)
像TEST_3一个简单的名称将转化为找到-name TEST_3
但像ST_2 / TEST_3斜杠的名称将转化为找到-path * ST_2 / TEST3
。这将需要的(部分)目录名护理。
A simple name like "TEST_3" will translate into find -name TEST_3
but a name with a slash like "ST_2/TEST_3" will translate into find -path *ST_2/TEST3
. This will take care of (partial) directory names.
这篇关于Unix的&QUOT;发现&QUOT;命令用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!