0

I am having a problem with a unix shell script.

The script is for renaming the Files and directories which is having spaces with "-" recursively. Example "Stack Overflow" to "Stack-Overflow"

This is running perfectly and working , But I get this wierd error which I don't know where it is coming.

I am getting the below error though (last line), ./script.sh[164]: [: Test.dat: unexpected operator/operand

I know it is coming some where in below funtions,

fix_dirs fix_files my_rename

but not sure where.

Could some one please help me in finding the error (Sorry for the long script :) )?.

#!/usr/bin/ksh

USAGE="$0 -f directory
$0 -d  directory
$0 -d -f directory

-f rename files 
-d rename directories 
"

usage ()
{
print -u2 "$USAGE"
exit 1
}

pathname ()
{
print -- "${1%/*}"
}

basename ()
{
print -- "${1##*/}"
}

find_dirs ()
{
find "$1" -depth -type d -name '* *' -print
}

find_files ()
{
find "$1" -depth -type f -name '* *' -print
}

my_rename()
{

FROM_DIR="$1";
TO_DIR="$2";

if [ ! -w ${FROM_DIR##*/} ]
then
echo "Directory/file ${FROM_DIR##*/} Not Writable";
exit 1
fi

if [ -d "$TO_DIR" -o -f "TO_DIR" ]
then
echo "Target File or Directory already exists - $TO_DIR ";
exit 1
fi

mv "$FROM_DIR" "$TO_DIR"
STATUS=$?
if [ $STATUS -ne 0 ]
then
echo "Error in moving the file or directory";
echo "From directory : $FROM_DIR";
echo "To directory   : $TO_DIR";
fi

}

fix_dirs ()
{

find_dirs "$1"|while read old_directory ; do
is_root_directory=`echo "old_directory" | grep -c "/"`;
if [[ $is_root_directory -ne 0 ]]
then
new_directory=$(pathname "$old_directory")/$(basename "$old_directory" | tr " " "_")
else
new_directory=$(echo "$old_directory" | tr " " "-")
fi
my_rename "${old_directory}" "${new_directory}"
done

}

fix_files ()
{

find_files "$1"|while read old_file ; do
new_file=$(pathname "$old_file")/$(basename "$old_file" | tr " " "-")
my_rename "${old_file}" "${new_file}"
done

}

WFILE=
WDIR=
DIR=

if [ "$#" -eq 0 ]
then
usage
fi

while [ $# -gt 0 ] 
do
case $1 in
-d)
WDIR=1
;;
-f)
WFILE=1
;;
-*)
usage 
;;
*)
if [ -d "$1" ]
then
DIR="$1"
else
print -u2 "$1 does not exist ..."
exit 1
fi
;;
esac
shift
done

if [ -z "$DIR" ]
then
echo "Please enter the directory";
exit 1
fi

if [ ${PWD} == "$DIR" ] 
then
echo "Cannot perform rename on current directory";
exit 1
fi

if [ "$DIR" == "." -o "$DIR" == ".." ]
then
echo "cannot rename current or previous directory";
exit 1
fi


if [[ "$WDIR" == "" && "$WFILE" == "" ]] ; then
usage
exit 1
fi

if [ "$WDIR" -a "$WFILE" ]
then
fix_files "$DIR"
fix_dirs "$DIR"
elif [ "$WDIR" ]
then
fix_dirs "$DIR"
elif [ "$WFILE" ]
then
fix_files "$DIR"
fi

2 Answers 2

1

Running "ksh -n" on your example shows some possible problem on lines 70/71:

foo: warning: line 70: `...` obsolete, use $(...)
foo: warning: line 71: -ne within [[...]] obsolete, use ((...))

The relevant lines are

is_root_directory=`echo "old_directory" | grep -c "/"`;
if [[ $is_root_directory -ne 0 ]]

That was using a ksh93 on my Debian6, which likely is not the same version as the one you are using. However, you may find that making the suggested change to the second line will make your warning go away.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks a lot for you information.

I tried bash similar to the ksh.

bash ./scriptname.sh

I corrected the errors. and below are the modified codes,

my_rename()
    {

    FROM_DIR="$1";
    TO_DIR="$2";

    if [ ! -w "${FROM_DIR##*/}" ]
    then
       echo "Directory/file ${FROM_DIR##*/} Not Writable";
       exit 1
    fi

    if [ -d "$TO_DIR" -o -f "TO_DIR" ]
    then
        echo "Target File or Directory already exists - $TO_DIR ";
        exit 1
    fi

    mv "$FROM_DIR" "$TO_DIR"
    STATUS=$?
    if [ $STATUS -ne 0 ]
    then
       echo "Error in moving the file or directory";
       echo "From directory : $FROM_DIR";
       echo "To directory   : $TO_DIR";
    fi

    }

fix_dirs ()
    {

    find_dirs "$1"|while read old_directory ; do
        is_root_directory=$(echo "$old_directory" | grep -c "/");
        if [ $is_root_directory -ne 0 ]; then
            new_directory=$(pathname "$old_directory")/$(basename "$old_directory" | tr " " "_")
        else
            new_directory=$(echo "$old_directory" | tr " " "-")
        fi
        my_rename "${old_directory}" "${new_directory}"
    done

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.