0

Can anyone explain why this script is trying to create the wrong symbolic link?

The main issue is with the line:

ln -s ${src}/${lib} ${dest64}/${lib}

as it's throwing the following error:

ln: creating symbolic link `/usr/lib64/libpgtypes.so/libpgtypes.so': File exists

but based on the echo messages the actual variable/link should be

/usr/lib64/libpgtypes.so

This is the output from executing the script:

[root@server dir]# sh myscript.sh
Symbolic link already created from /opt/rh/rh-postgresql94/root/usr/lib64/libpgtypes.so to /usr/lib/libpgtypes.so

ln -s /opt/rh/rh-postgresql94/root/usr/lib64/libpgtypes.so /usr/lib64/libpgtypes.so
ln: creating symbolic link `/usr/lib64/libpgtypes.so/libpgtypes.so`: File exists
Created symbolic link from /opt/rh/rh-postgresql94/root/usr/lib64/libpgtypes.so to /usr/lib/libpgtypes.so

And this is the code in the script: #!/bin/bash

libraries=('libpgtypes.so')

src="/opt/rh/rh-postgresql94/root/usr/lib64"
dest="/usr/lib"
dest64="/usr/lib64"

for lib in ${libraries[*]}; do
    if [ -f ${dest}/${lib} ]; then
        echo "Symbolic link already created from ${src}/${lib} to ${dest}/${lib}"
    else
        ln -s ${src}/${lib} ${dest}/${lib}
        echo "Created symbolic link from ${src}/${lib} to ${dest}/${lib}"
    fi

    if [ -f ${dest64}/${lib} ]; then
        echo "Symbolic link already create from ${src}/${lib} to ${dest64}/${lib}"
    else
        echo "ln -s ${src}/${lib} ${dest64}/${lib}"
        ln -s ${src}/${lib} ${dest64}/${lib}
        echo "Created symbolic link from ${src}/${lib} to ${dest}/${lib}"
    fi
done
2

It appears that /usr/lib64/libpgtypes.so is a directory on your system. (I can't tell you if that's right or wrong.)


Incidentally, as a matter of good coding practice I would strong recommend that you double-quote all your variables.

Instead of ln -s ${src}/${lib} ${dest64}/${lib} you should write ln -s "${src}/${lib}" "${dest64}/${lib}". This will also allow you to handle your for loop correctly: for lib in "${libraries[@]}"; do ... done (notice @ instead of *)

  • Ah! It seems there is a directory there already..... – AK47 May 10 '17 at 14:41
1

Use -h to check if dest is a symbolic link, for example you could remove all other types with:

if ! [ -h "${dest}/${lib}" ]; then
    rm -rf "${dest}/${lib}"
fi
0

There should be /usr/lib64/libpgtypes.so directory in that path. Please remove or rename it and execute the script again to avoid the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.