The bash script that runs the ln -s command gets an error code on exit, but symbolic links are created successfully

I wrote a bash script that should iterate over files and directories in TARGET DIR and create a symlink for all directories in DESTINATION DIR. The script works, but the ln command gives an exit code> 0 from what I can say, and therefore the corresponding success message is not printed.

HERE DESTINATION DYNAMICS BEFORE SCRIPT PERFORMANCE:

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 TARGET REFERENCE INFORMATION WITH QUICK DIVISIONS

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 EXIT WHEN I WORK:

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 A PURPOSE AFTER a script WORKS WITH SINMINKS 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 script YOURSELF:

#!/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

, script works, [$ (ln -s "$ DIR" "$ TARG" )] , printf "== > $TARG\n" , "", .

/?

!

+4
1

:

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

, ln - , , . :

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

, stderr , script . (, printf 'this is an error' >&2; exit 1;)

+4

Source: https://habr.com/ru/post/1568906/


All Articles