Bash Script to Create Symbolic Links to Shared Libraries

Bash script to create symbolic links to shared libraries

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.

Linux bash script symlink with spaces

You can try to use find's -exec action:

find $MOVIE_DIRS -maxdepth 1 -type d -exec ln -sf -t $MOVIES_LIB {} +
find $MOVIE_DIRS -maxdepth 1 -type f -name "*.*" -exec ln -sf -t $MOVIES_LIB {} +

Create a custom symbolic link to a library at install time with CMake

One way to do it could be using CMake add_custom_command and add_custom_target. In your case it would be something like the following:

 SET( legacy_link   ${CMAKE_INSTALL_PREFIX}/libIex.so)
SET( legacy_target ${CMAKE_INSTALL_PREFIX}/libIex-2_0.so.10.0.1)
ADD_CUSTOM_COMMAND( OUTPUT ${legacy_link}
COMMAND ln -s ${legacy_target} ${legacy_link}
DEPENDS install ${legacy_target}
COMMENT "Generating legacy symbolic link")

ADD_CUSTOM_TARGET( install_legacy DEPENDS ${legacy_link} )

At this point you should have a target install_legacy in your generated Makefile with the correct dependency to generate libIex.so.

install symbolic links with coreutils install

I wondered about this too. After looking at the source code it would appear that install is pretty aggressive about resolving links at install time. Here are some of the defaults it passes to cp; the relevant ones don't get overridden later.

cp_option_init (struct cp_options *x)
{
cp_options_default (x);
x->copy_as_regular = true;
x->reflink_mode = REFLINK_NEVER;
x->dereference = DEREF_ALWAYS;
x->hard_link = false;
x->preserve_links = false;
x->preserve_mode = false;
x->symbolic_link = false;
(...)

The workaround would be to use cp + chmod.

Why is this symbolic link created two instances

Think about what happens when you run the ln -s command twice when its target is a directory rather than a file.

If $HOME/.shell doesn't exist, then

ln -s "$SCRIPTS_DIR/shell/" "$HOME/.shell"

...creates it. However, if it already exists, then...

ln -s "$SCRIPTS_DIR/shell/" "$HOME/.shell"

...treats .shell as a destination directory name, not a complete path to the destination to be created, and creates a new entry within that directory.


GNU ln has some extensions to fix this usage, including:

-h If the target_file or target_dir is a symbolic link, do not follow it. This is most useful with the -f option, to replace a symlink which may point to a directory.

Thus, if operating on a GNU system, you could use:

# quotes added for bash compatibility, since question is tagged for both shells
ln -sfh "$SCRIPTS_DIR/shell/" "$HOME/.shell"

Otherwise, just check first:

[[ -e $HOME/.shell ]] || ln -s "$SCRIPTS_DIR/shell/" "$HOME/.shell"

How can I symlink a file in Linux?

To create a new symlink (will fail if symlink exists already):

ln -s /path/to/file /path/to/symlink

To create or update a symlink:

ln -sf /path/to/file /path/to/symlink

Create Symlink from recursive search in a certain folder

ok, seems like I made it running with:

cd ./symlink-folder/
find ../ -type d -name "*search*" -exec ln -s {} . ';'

it does work with spaces



Related Topics



Leave a reply



Submit