4

我编写了一个 bash 脚本,它应该遍历 TARGET DIR 中的文件和目录,并创建指向 DESTINATION DIR 中所有目录的符号链接。该脚本有效,但 ln 命令给出的退出代码为 >0,据我所知,因此不会打印相应的成功消息。

这是执行脚本之前的目标目录:

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 ..

这是包含很多子目录的目标目录

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

这是我运行它时的输出:

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

这是添加了符号链接的脚本运行后的目标目录。

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

这是脚本本身:

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

因此,底线是脚本有效,但是 [ $(ln -s "$DIR" "$TARG") ] 应该返回零并且 printf " ==> $TARG\n" 行应该打印成功消息,但是相反,即使目标目录为空,我也会收到一条失败的 ot 链接到“消息。

有什么想法/建议吗?

谢谢!

4

1 回答 1

4

这一行是错误的:

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

它正在测试是否ln生成任何输出,而不是它是否成功。将其更改为:

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

请删除多余的打印,养成将错误消息写入 stderr 的习惯,并在脚本失败时以非零值退出。(例如,printf 'this is an error' >&2; exit 1;

于 2014-12-24T16:35:52.437 回答