[英]bash + how to set cli PATH with version in sub folder
Under /usr/hdp
folder we can have only one the following sub-folders在
/usr/hdp
文件夹下,我们只能有以下子文件夹之一
2.6.5.0-292
2.6.4.0-91
2.6.0.3-8
example例子
ls /usr/hdp/
2.6.5.0-292 current stack file.txt
I want to use the following cli, and $VERSION
could be one of the above versions我想使用下面的 cli,
$VERSION
可以是上述版本之一
/usr/hdp/$VERSION/kafka/bin/kafka-reassign-partitions.sh
then I did the following in my bash script然后我在我的 bash 脚本中执行了以下操作
[[ -d /usr/hdp/2.6.5.0-292 ]] && VERSION=2.6.5.0-292
[[ -d /usr/hdp/2.6.4.0-91 ]] && VERSION=2.6.4.0-91
[[ -d /usr/hdp/2.6.0.3-8 ]] && VERSION=2.6.0.3-8
/usr/hdp/$VERSION/kafka/bin/kafka-reassign-partitions.sh
Can we do the above setting in a more efficienct way without the test of [[ -d...... ]]
?如果没有
[[ -d...... ]]
的测试,我们可以以更有效的方式进行上述设置吗?
I don't think there is any way to make this more efficient and if your requirement is that the directory should exist, there is no way to avoid checking for that.我认为没有任何方法可以提高效率,如果您的要求是该目录应该存在,则无法避免检查。 But you can at least avoid repeating yourself by using a loop.
但是您至少可以通过使用循环来避免重复自己。
for ver in 2.6.5.0-292 2.6.4.0-91 2.6.0.3-8; do
if [[ -d "/usr/hdp/$ver" ]]; then
VERSION=$ver
break
fi
done
The break
causes the script to skip older versions as soon as it finds the newest in the list; break
会导致脚本在列表中找到最新版本后立即跳过旧版本; your original code would weirdly do the opposite.你的原始代码会奇怪地做相反的事情。 If you genuinely want the oldest available version, reverse the ordering of the arguments to
for
.如果您真的想要最旧的可用版本,请将 arguments 的顺序颠倒为
for
。
Not checking for any others once you find a match is an actual optimization here, albeit a very minor one.找到匹配项后不检查任何其他内容是这里的实际优化,尽管是非常小的优化。
If you can have only one of the folders 2.6.5.0-292
, 2.6.4.0-91
or 2.6.0.3-8
, then the assignment VERSION=2.6.*
will expand to the unique and correct folder.如果您只能拥有文件夹
2.6.5.0-292
、 2.6.4.0-91
或2.6.0.3-8
,则分配VERSION=2.6.*
将扩展为唯一且正确的文件夹。
So your script could look like:所以你的脚本可能看起来像:
VERSION=2.6.*
/usr/hdp/$VERSION/kafka/bin/kafka-reassign-partitions.sh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.