Redrick Fisher
Redrick Fisher

Reputation: 1

sh script: no output when run in mounted filesystem

Need some help to understand what's wrong. In short: I've written a bourne shell script, which creates links to contents of source directory in the target directory. It worked fine on the host system but when targeted on directories on mounted fs (both from chroot and native system) it doesn't work and provides no output at all.

Details: mounted fs: ext3, rw

host system: 3.2.0-48-generic #74-Ubuntu SMP GNU/Linux

To narrow the question, "/usr" was taken as an example.

permissions for "/usr" in the host system: drwxr-xr-x

permissions for "/usr" on mounted partition: drwxr-xr-x

Tried to use both bash and dash from host system. Same result - works for native file systems, does not work for the mounted.

script (cord.sh; run from root in my cases):

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

The most obvious thing to suggest, was some issue with permissions. Yet everything looks sane. Maybe I've missed something?

Upvotes: 0

Views: 86

Answers (1)

user2404501
user2404501

Reputation:

I don't know what your main problem might be (permissions or something else - you should include an example of how you run the script and how you prepare for it with the mounts and everything). But this script can be cleaned up.

First, if you want to test whether something is a directory, use

if [ -d "$something ]

That'll get rid of the clumsy file usage.

Second, don't go through the redundant steps of converting your $TARGETS array to a series of lines and then reading the lines with a loop. Just loop over the array directly.

for line in $TARGETS

Also, instead of using ls to populate an array of filenames, I'd use a glob. But instead of either of those, I'd use find so it can take care of recursion and eliminate the tree of processes you're creating by recursing with a call to the same script. And instead of writing a symlink-tree-maker script I'd use something like lndir which already exists for that purpose...

Upvotes: 1

Related Questions