首页 文章

sh脚本:在挂载的文件系统中运行时没有输出

提问于
浏览
0

需要一些帮助来理解什么是错的 . 简而言之:我编写了一个bourne shell脚本,它创建了目标目录中源目录内容的链接 . 它在主机系统上运行良好但是当针对已安装的fs上的目录(来自chroot和本机系统)时,它不起作用并且根本不提供输出 .

细节:挂载fs:ext3,rw

主机系统:3.2.0-48-通用#74-Ubuntu SMP GNU / Linux

为了缩小这个问题,以“/ usr”为例 .

主机系统中“/ usr”的权限:drwxr-xr-x

安装分区上“/ usr”的权限:drwxr-xr-x

试图从主机系统使用bash和dash . 相同的结果 - 适用于本机文件系统,不适用于已安装 .

脚本(cord.sh;在我的情况下从root运行):

# !/bin/sh

SRCFOLDER=$2                    # folder with package installation
DESTFOLDER=$3                   # destination folder to install symlinks to ('/' - for base sys; '/usr' - userland)
TARGETS=$(ls $SRCFOLDER)        # targets to handle

SRCFOLDER=${SRCFOLDER%/}        # stripping slashes from the end, if they are present
DESTFOLDER=${DESTFOLDER%/}      #

##
## LINKING
##

if [ "$1" = "-c" ];
    then printf %s "$TARGETS" | while IFS= read -r line
        do
            current_target=$(file $SRCFOLDER/$line)     # had an issue with different output in different systems
            if [ "${current_target% }" = "$SRCFOLDER/$line: directory" ];   # stripping space helped
                then
                    mkdir -v $DESTFOLDER/$line    # if other package created it - it'll fail
                    /usr/local/bin/cord.sh -c $SRCFOLDER/$line $DESTFOLDER/$line                   # RECURSION
            else
                ln -sv $SRCFOLDER/$line $DESTFOLDER/$line   # will fail, if exists
            fi;
        done

##
## REMOVING LINKS
##

elif [ "$1" = "-d" ];
    then printf %s "$TARGETS" | while IFS= read -r line
        do
            current_target=$(file $SRCFOLDER/$line)
            if [ "${current_target% }" = "$SRCFOLDER/$line: directory" ];
                then
                    /usr/local/bin/cord.sh -d $SRCFOLDER/$line $DESTFOLDER/$line                   # RECURSION
            else
                rm -v $DESTFOLDER/$line
            fi;
        done


elif [ "$1" = "-h" ];
    then
        echo "Usage:"
        echo "cord -c /path/to/pkgdir /path/to/linkdir  -   create simlinks for package contents"
        echo "cord -d /path/to/pkgdir /path/to/linkdir  -   delete links for package"
        echo "cord -h                                   -   displays this help note"


else
    echo "Usage:"
    echo "cord -c /path/to/pkgdir /path/to/linkdir  -   create simlinks for package contents"
    echo "cord -d /path/to/pkgdir /path/to/linkdir  -   delete links for package"
    echo "cord -h                                   -   displays this help note"

fi;

最明显的建议是权限问题 . 但一切看起来都很清醒 . 也许我错过了什么?

1 回答

  • 1

    我不知道你的主要问题是什么(权限或其他东西 - 你应该包括一个如何运行脚本以及如何使用安装和一切准备它的示例) . 但是这个脚本可以清理干净 .

    首先,如果要测试某些内容是否是目录,请使用

    if [ -d "$something ]
    

    这将摆脱笨拙的 file 用法 .

    其次,不要经历将 $TARGETS 数组转换为一系列行,然后用循环读取行的冗余步骤 . 只需直接遍历数组 .

    for line in $TARGETS
    

    此外,我没有使用 ls 来填充文件名数组,而是使用 find ,因此它可以处理递归并消除您使用已经存在的 lndir 之类的进程树...

相关问题