skip to Main Content

I am trying to copy binary dependencies to other folder, so I use ldd to see what should be copied.

However the script fails when copying. It "appears" '$'n' characters when binary dependencies are copied.

Something is wrong, but I dont know what. Tried running command per command and cant see the fault.

What is the trouble here ?

Thanks

Script code

#!/usr/bin/env bash

set -e

chr=/home/myjail
cmds=( bash echo ls rm )

mkdir -p "$chr"/{bin,lib,lib64}

# copy commands
for app in "${cmds[@]}"; do
        echo 'Added command:'"$app"
        cp -v /bin/"$app" "$chr/bin"
done

# copy deps    
for app in "${cmds[@]}";do

        echo 'deps for:'"$app"
        deps="$(ldd /bin/"$app" | egrep -o '/lib.*.[0-9]')"

        for curdep in "${deps[@]}";do
                echo "$curdep"
                cp -v --parents "$curdep" "$chr"
        done
done

Script output

furby@debian-haptic20:~# ./depcopy.sh
Added command:bash
'/bin/bash' -> '/var/lib/haproxy/bin/bash'
Added command:echo
'/bin/echo' -> '/var/lib/haproxy/bin/echo'
Added command:ls
'/bin/ls' -> '/var/lib/haproxy/bin/ls'
Added command:mysql
'/bin/mysql' -> '/var/lib/haproxy/bin/mysql'
deps for:bash
/lib/x86_64-linux-gnu/libtinfo.so.6
/lib/x86_64-linux-gnu/libdl.so.2
/lib/x86_64-linux-gnu/libc.so.6
/lib64/ld-linux-x86-64.so.2
cp: failed to get attributes of '/lib/x86_64-linux-gnu/libtinfo.so.6'$'n': No such file or directory

2

Answers


  1. I’m using this script for this purpose:

    copydep.sh

    #!/bin/bash
    
    if [ ${#} != 2 ]
    then
        echo "usage $0 PATH_TO_BINARY target_folder"
        exit 1
    fi
    
    path_to_binary="$1"
    target_folder="$2"
    
    # if we cannot find the the binary we have to abort
    if [ ! -f "${path_to_binary}" ]
    then
        echo "The file '${path_to_binary}' was not found. Aborting!"
        exit 1
    fi
    
    # copy the binary itself
    echo "---> copy binary itself"
    cp --parents -v "${path_to_binary}" "${target_folder}"
    
    # copy the library dependencies
    echo "---> copy libraries"
    ldd "${path_to_binary}" | awk -F'[> ]' '{print $(NF-1)}' | while read -r lib
    do
        [ -f "$lib" ] && cp -v --parents "$lib" "${target_folder}"
    done
    

    Run it like this:

    $ mkdir /tmp/test
    $ bash copydep.sh /bin/ls /tmp/test
    ---> copy binary itself
    /bin -> /tmp/test/bin
    '/bin/ls' -> '/tmp/test/bin/ls'
    ---> copy libraries
    /lib -> /tmp/test/lib
    /lib/x86_64-linux-gnu -> /tmp/test/lib/x86_64-linux-gnu
    '/lib/x86_64-linux-gnu/libselinux.so.1' -> '/tmp/test/lib/x86_64-linux-gnu/libselinux.so.1'
    '/lib/x86_64-linux-gnu/libc.so.6' -> '/tmp/test/lib/x86_64-linux-gnu/libc.so.6'
    '/lib/x86_64-linux-gnu/libpcre.so.3' -> '/tmp/test/lib/x86_64-linux-gnu/libpcre.so.3'
    '/lib/x86_64-linux-gnu/libdl.so.2' -> '/tmp/test/lib/x86_64-linux-gnu/libdl.so.2'
    /lib64 -> /tmp/test/lib64
    '/lib64/ld-linux-x86-64.so.2' -> '/tmp/test/lib64/ld-linux-x86-64.so.2'
    '/lib/x86_64-linux-gnu/libpthread.so.0' -> '/tmp/test/lib/x86_64-linux-gnu/libpthread.so.0'
    
    # Result:
    $ tree /tmp/test/
    /tmp/test/
    ├── bin
    │   └── ls
    ├── lib
    │   └── x86_64-linux-gnu
    │       ├── libc.so.6
    │       ├── libdl.so.2
    │       ├── libpcre.so.3
    │       ├── libpthread.so.0
    │       └── libselinux.so.1
    └── lib64
        └── ld-linux-x86-64.so.2
    
    4 directories, 7 files
    
    Login or Signup to reply.
  2. Your deps variable should be an array, created with mapfile and some redirection of process substitution:

    mapfile -t deps < <(ldd /bin/"$app" | grep -o '/lib.*.[0-9]')
    

    As you have it, it’s treated as a single string, which as you’ve seen, doesn’t work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search