I'd like to create a "shell script" that ease and simplify the use of "Sort" command.


Well my shell script should cover the use of " -b -d -f -n -r "options.

also my script should do the following:

-allows both relative and absolute pathname arguments
-process either filename arguments or standard input
-allows a list of filename and filename wildcards
-allows output to be redirected

P.S I'm on a SunOs system.

thanks in advance!

hi,

I think it's quite late to reply u... anyhow here is the script u can make use of may be with some modification...

#!/bin/sh

#This function reads the SORT command option from the user and execute the comman
read_sort ()
{
    echo ""
    echo ""
    echo "****** Provide the SORT Option ******"
    echo ""
    echo "-b option (Type y/n)" 
    read btype
    echo ""
    echo "-d option (Type y/n)" 
    read dtype
    echo ""
    echo "-f option (Type y/n)" 
    read ftype
    echo ""
    echo "-n option (Type y/n)" 
    read ntype
    echo ""
    echo "-r option (Type y/n)" 
    read rtype
    echo ""

    option_str=""
    if [ "$btype" == "y" ] || [ "$btype" == "Y" ]
    then
        option_str="b"
    fi
    if [ "$dtype" == "y" ] || [ "$dtype" == "Y" ]
    then
        option_str=$option_str"d"
    fi
    if [ "$ftype" == "y" ] || [ "$ftype" == "Y" ]
    then
        option_str=$option_str"f"
    fi
    if [ "$ntype" == "y" ] || [ "$ntype" == "Y" ]
    then
        option_str=$option_str"n"
    fi
    if [ "$rtype" == "y" ] || [ "$rtype" == "Y" ]
    then
        option_str=$option_str"r"
    fi
    if [ -n "$option_str" ]
    then
        option_str=-"$option_str"
    fi
    echo ""
    echo "Executing Sort Command"
    if [ -z "$red_file" ]
    then
        cmd="sort $option_str $files"
        i=`$cmd`
    else
        touch $red_file
        cmd="sort $option_str $files"
        echo "cmd is sort $option_str $files"
        i=`$cmd >> $red_file`
    fi

    if [ $? -eq 0 ]
    then
        echo ""
        echo ""
        echo "Command Successfully Executed"
    else
        echo ""
        echo ""
        echo "Command Execution Failed"
        echo ""
        echo ""
    fi 
}


#Main
if [ -z $1 ]
then
    clear
    echo ""
    echo "*******Please give the FILE NAME(S) to be SORT*******"
    read files
else
    files=$*
fi
for file in $files
do
    if [ ! -f "$file" ]
    then
        echo ""
        echo "Invalid File(s) or Pathname $file"
        exit 1
    fi
done

echo ""
echo ""
echo "Enter the redirection filename(press enter if no redirection needed) "
read red_file
read_sort

I'm sure it was homework. S/He should show what effort s/he made so far.

There is nothing to show in this small script... don't know how u get this feeling...It was 1 hour job 4 me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.