0

I am pretty stuck with that script.

#!/bin/bash

STARTDIR=$1
MNTDIR=/tmp/test/mnt

find $STARTDIR -type l  |
    while read file;
    do
       echo Found symlink file: $file
       DIR=`sed 's|/\w*$||'`
       MKDIR=${MNTDIR}${DIR}
       mkdir -p $MKDIR
       cp -L $file $MKDIR
    done

I passing some directory to $1 parameter, this directory have three symbolic links. In while statement echoed only first match, after using sed I lost all other matches. Look for output below:

[artyom@LBOX tmp]$ ls -lh /tmp/imp/
total 16K
lrwxrwxrwx 1 artyom adm   19 Aug  8 10:33 ok1 -> /tmp/imp/sym3/file1
lrwxrwxrwx 1 artyom adm   19 Aug  8 09:19 ok2 -> /tmp/imp/sym2/file2
lrwxrwxrwx 1 artyom adm   19 Aug  8 10:32 ok3 -> /tmp/imp/sym3/file3

[artyom@LBOX tmp]$ ./copy.sh /tmp/imp/
Found symlink file: /tmp/imp/ok1
[artyom@LBOX tmp]$ 

Can somebody help with that issue? Thanks

1 Answer 1

1

You forgot to feed something to sed. Without explicit input, it reads nothing in this construction. I wouldn't use this approach anyway, but just use something like:

DIR=`dirname "$file"`
Sign up to request clarification or add additional context in comments.

3 Comments

Friek, thanks for robust solution and tip. From your tip I got this conclusion: DIR=echo $file | sed 's|/\w*$||'
That should work indeed. I would stick with the dirname nevertheless.
@Tom Lime: It's not exactly true that sed reads nothing in this case. What it reads is the stdin from where it was called, which in this case is the rest of the stream being piped to while (which is why you only get one line of output). Using a subshell with sed (or, worse, echo | sed) is hugely overkill. If you don't have dirname as Friek recommends then you can say DIR="${file##*/}".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.