bash脚本来创建符号链接共享库 [英] Bash script to create symbolic links to shared libraries

查看:175
本文介绍了bash脚本来创建符号链接共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题是比较容易让你shell脚本的怪物。

我要寻找最优雅,最简单的办法通过一个bash shell脚本的手段来创建符号链接为Unix共享库。

我需要的是开始了与共享库文件,如libmythings.so.1.1,libotherthings.so.5.11的名单是什么,获得创建符号链接,例如:

  libmythings.so  - > libmythings.so.1  - > libmythings.so.1.1
libotherthings.so - > libotherthings.so.5 - > libotherthings.so.5.11

的库文件,其中包含其他文件,如其他shell脚本的目录里面。

修改:那么,ldconfig的-nN。可以工作不错,但我还需要链接没有。所以,库中的至少一个,因为一个或多个库是JNI的入境点从Java调用,所以当一个库后追加库的主号码是的System.loadLibrary来实例化(库名称),预计一个名为libraryname.so,而不是libraryname.so.X库。

如果有用于Java部分解决方法ldconfig的-nN只有该解决方案可以工作。


解决方案

 在$ @baselib

     shortlib = $ baselib
     而分机= $(回声$ shortlib | sed的'S / \\ [0-9] [0-9] * $ //')
           [-n$分机]
     做
         shortlib = $($基名$ shortlib分机)
         LN -s $ baselib $ shortlib
     DONE
DONE

我已经被骗了 - 所有的链接到基库(libmythings.so.1.1);如果你真的想链,那么你需要:

 在$ @baselib

     shortlib = $ baselib
     而分机= $(回声$ shortlib | sed的'S / \\ [0-9] [0-9] * $ //')
           [-n$分机]
     做
         shorterlib = $($基名$ shortlib分机)
         LN -s $ shortlib $ shorterlib
         shortlib = $ shorterlib
     DONE
DONE

当心 - 未经考验code


傲慢precedes克星。

注释赶到了code以上不工作 - 和评论是正确的。在现场试验的固定版本是:

 设置 -  libname.so.5.1.1在$ @baselib

    shortlib = $ baselib
    而分机= $(回声$ shortlib |。SED -n'/ \\ [0-9] [0-9] * $ /秒/.* \\(\\ [0-9] [0-9] * \\) $ / \\ 1 / p')
          [-n$分机]
    做
        shortlib = $($基名$ shortlib分机)
        回声LN -s $ baselib $ shortlib
    DONE
DONE

的变化是在 SED 命令。此版本不打印默认情况下任何东西( -n ),只匹配以点后跟数字结尾的行,然后删除一切,除了该后缀,并打印剩下对于分配给分机。修订的脚本生成以下输出。删除回声得到它执行链接命令。

  LN -s libname.so.5.1.1 libname.so.5.1
LN -s libname.so.5.1.1 libname.so.5
LN -s libname.so.5.1.1 libname.so

该脚本说明有关shell脚本一个有趣的和经常被忽视的一点:在一段时间的条件块操作的顺序不一定是单一的命令。与编辑操作行的状态作为一个整体成功并不无论检测的影响;最后一个命令的退出状态,将 [-n$分机] ',控制循环是否继续。

I think this question is rather easy for you shell scripting monsters.

I am looking for the most elegant and shortest way to create symbolic links to shared libraries for Unix by means of a bash shell script.

What I need is starting out with a list of shared library files such as "libmythings.so.1.1, libotherthings.so.5.11", get the symbolic links created such as:

libmythings.so -> libmythings.so.1 -> libmythings.so.1.1
libotherthings.so -> libotherthings.so.5 -> libotherthings.so.5.11

The library files are inside a directory which contains other files such as other shell scripts.

EDIT: Well, "ldconfig -nN ." could work OK, but I also need the link without the major number of the library appended after ".so", at least one of the libraries, since one or more libraries are the entry points of JNI calls from Java, so when a library is instanced by means of System.loadlibrary("libraryname") it expects a library called "libraryname.so", not "libraryname.so.X".

The solution with just ldconfig -nN could work if there were a workaround for the Java part.

解决方案

for baselib in "$@"
do
     shortlib=$baselib
     while extn=$(echo $shortlib | sed 's/\.[0-9][0-9]*$//')
           [ -n "$extn" ]
     do
         shortlib=$(basename $shortlib $extn)
         ln -s $baselib $shortlib
     done
done

I've cheated - all the links go to the base library (libmythings.so.1.1); if you really want to chain, then you need:

for baselib in "$@"
do
     shortlib=$baselib
     while extn=$(echo $shortlib | sed 's/\.[0-9][0-9]*$//')
           [ -n "$extn" ]
     do
         shorterlib=$(basename $shortlib $extn)
         ln -s $shortlib $shorterlib
         shortlib=$shorterlib
     done
done

Beware - untested code.


Hubris precedes nemesis.

Comment arrived that the code above doesn't work - and the comment is correct. A fixed version with test in situ is:

set -- libname.so.5.1.1

for baselib in "$@"
do
    shortlib=$baselib
    while extn=$(echo $shortlib | sed -n '/\.[0-9][0-9]*$/s/.*\(\.[0-9][0-9]*\)$/\1/p')
          [ -n "$extn" ]
    do
        shortlib=$(basename $shortlib $extn)
        echo ln -s $baselib $shortlib
    done
done

The change is in the sed command. This version doesn't print anything by default (-n), matches only lines ending with a dot followed by digits, and then deletes everything except that suffix, and prints what remains for assignment to extn. As amended, the script generates the output below. Remove the echo to get it to execute the link commands.

ln -s libname.so.5.1.1 libname.so.5.1
ln -s libname.so.5.1.1 libname.so.5
ln -s libname.so.5.1.1 libname.so

The script illustrates an interesting and often overlooked point about shell scripts: the sequence of operations in the condition block of a while need not be a single command. The status of the line with the edit operation doesn't affect whether the test as a whole succeeds; the exit status of the last command, the '[ -n "$extn" ]', controls whether the loop continues.

这篇关于bash脚本来创建符号链接共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆