Search code examples
linuxbashln

bash script that executes ln -s command is getting error code on exit but symlinks are being created successfully


I wrote a bash script that is supposed to iterate thru the files and directories in TARGET DIR and create a symlink to all directories in DESTINATION DIR. The script works but the ln command is giving an exit code of >0 from what I can tell and so the appropriate success message is not printed.

HERE IS THE DESTINATION DIRECTORY BEFORE EXECUTING THE SCRIPT:

obi@Hanzomon:/home/dog⟫ ls -la
total 8
drwxr-xr-x 2 obi obi 4096 Dec 24 11:13 .
drwxr-xr-x 8 obi obi 4096 Dec 24 10:32 ..

HERE IS THE TARGET DIRECTORY WITH QUITE A FEW SUB-DIRECTORIES

obi@Hanzomon:/home/dog⟫ ls /home/bud
bzfs.conf  Downloads         Music     Templates  VirtualBox VMs
Desktop    examples.desktop  Pictures  vboxen     vmz
Documents  lab               Public    Videos

HERE IS THE OUTPUT WHEN I RUN IT:

obi@Hanzomon:/home/dog⟫ /home/obi/scripts/dirlink.sh                            
/home/bud/Desktop failed to link to /home/dog/Desktop
/home/bud/Documents failed to link to /home/dog/Documents
/home/bud/Downloads failed to link to /home/dog/Downloads
/home/bud/lab failed to link to /home/dog/lab
/home/bud/Music failed to link to /home/dog/Music
/home/bud/Pictures failed to link to /home/dog/Pictures
/home/bud/Public failed to link to /home/dog/Public
/home/bud/Templates failed to link to /home/dog/Templates
/home/bud/vboxen failed to link to /home/dog/vboxen
/home/bud/Videos failed to link to /home/dog/Videos
/home/bud/VirtualBox VMs failed to link to /home/dog/VirtualBox VMs
/home/bud/vmz failed to link to /home/dog/vmz

HERE IS THE DESTINATION DIR AFTER SCRIPT RUNS WITH SYMLINKS ADDED.

obi@Hanzomon:/home/dog⟫ ls -la
total 8
drwxr-xr-x 2 obi obi 4096 Dec 24 11:13 .
drwxr-xr-x 8 obi obi 4096 Dec 24 10:32 ..
lrwxrwxrwx 1 obi obi   17 Dec 24 11:13 Desktop -> /home/bud/Desktop
lrwxrwxrwx 1 obi obi   19 Dec 24 11:13 Documents -> /home/bud/Documents
lrwxrwxrwx 1 obi obi   19 Dec 24 11:13 Downloads -> /home/bud/Downloads
lrwxrwxrwx 1 obi obi   13 Dec 24 11:13 lab -> /home/bud/lab
lrwxrwxrwx 1 obi obi   15 Dec 24 11:13 Music -> /home/bud/Music
lrwxrwxrwx 1 obi obi   18 Dec 24 11:13 Pictures -> /home/bud/Pictures
lrwxrwxrwx 1 obi obi   16 Dec 24 11:13 Public -> /home/bud/Public
lrwxrwxrwx 1 obi obi   19 Dec 24 11:13 Templates -> /home/bud/Templates
lrwxrwxrwx 1 obi obi   16 Dec 24 11:13 vboxen -> /home/bud/vboxen
lrwxrwxrwx 1 obi obi   16 Dec 24 11:13 Videos -> /home/bud/Videos
lrwxrwxrwx 1 obi obi   24 Dec 24 11:13 VirtualBox VMs -> /home/bud/VirtualBox VMs
lrwxrwxrwx 1 obi obi   13 Dec 24 11:13 vmz -> /home/bud/vmz

HERE IS THE SCRIPT ITSELF:

#!/bin/bash

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

TARDIR="/home/bud"
DESTDIR="/home/dog"

if  [ ! -d $TARDIR ]; then
        printf "$TARDIR does not exist!\n"
        exit
fi

if [ ! -d $DESTDIR ]; then 
        printf "$DESTDIR does not exist!\n"
        exit
fi


TARDIR="$TARDIR/*"
for DIR in $TARDIR; do
        if [ -d $DIR ]; then
                printf "$DIR"
                TARG="$DESTDIR/$(basename "$DIR")"
                if [  $(ln -s "$DIR" "$TARG") ]; then
                        printf " ==>  $TARG\n"
                else
                        printf " failed to link to $TARG\n"
                fi
        fi

done

IFS=$SAVEIFS

So, bottom line is the script works, but [ $(ln -s "$DIR" "$TARG") ] should return a zero and the printf " ==> $TARG\n" line should print a success message, but instead, I get a failed ot link to" message even when the destination directory is empty.

Any ideas/suggestions?

Thanks!


Solution

  • This line is wrong:

    if [  $(ln -s "$DIR" "$TARG") ]; then
    

    It is testing if ln generates any output, not whether or not it succeeds. Change it to:

    if ln -s "$DIR" "$TARG"; then
    

    Please remove your redundant printing, get in the habit of writing error messages to stderr, and exit with a non-zero value if your script fails. (eg, printf 'this is an error' >&2; exit 1;)