简体   繁体   English

bash + 如何使用子文件夹中的版本设置 cli PATH

[英]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-2922.6.4.0-912.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.

相关问题 仅当Linux bash中存在子文件夹时,才使用文件名获取当前目录(不是完整路径) - Get current directory (not full path) with filename only when sub folder is present in Linux bash 如何在 ssh_config 的 Azure CLI bash window 中将 StrictHostKeyChecking 设置为 no - How to set StrictHostKeyChecking to no in Azure CLI bash window for ssh_config 在 bash 脚本上检查 AWS cli 版本 - Check AWS cli version on bash script 试图了解如何在 linux 上设置 PATH(Ubuntu 16.04 上的 bash) - Trying to understand how the PATH is set on linux (bash on Ubuntu 16.04) 如何在ubuntu中的.profile或.bash_profile中设置PATH - How to set the PATH in .profile or .bash_profile in ubuntu BASH Shellscript 从文件夹路径获取用户名 - BASH Shellscript get username from folder path 重击:剪切当前路径,直到某个文件夹 - Bash: Cut current path until a certain folder 如何搜索指定文件但您不知道路径文件夹是(在 bash 中)? - How search specify file but you don't know in with path folder is (in bash)? 如何在bash中没有内部文件夹路径的情况下将特定文件tar到特定文件夹? - How to tar specific files to specific folder without inside folders path in bash? 如何在不更改代码的情况下为Symfony2设置php CLI版本? - How can I set the php CLI version for Symfony2 without changing the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM