Re: Re: rad

From NAT, 7 Years ago, written in Bash, viewed 903 times. This paste is a reply to Re: rad from NAT - go back
URL https://code.nat.moe/view/26991f43/diff Embed
Viewing differences between Re: rad and Re: Re: rad
#!/bin/bash
FILE=./db.pl
RDB=./rdb.pl
function h {
        echo "\
========================= Command Help =========================
Usage: a <rel> <tar1> <tar2> [non-dw]
       rel: Relationships(f/l/pl/k/h/ph)
       tar1: Target 1
       tar2: Target 2
       non-dw: Set any value to this if the relationship is not
               double way
Usage: c <tar1> <tar2> [tar3] [tar4] ... [tarN]
       Enter any names (at least 2) to create a compound relati
       -onship. ONLY give names as parameters, script will prom
       -t you to input relationship. 
Usage: f <file>
       Change file to write (Default ./db.pl)"

}
function f {
        if [[ -z $1 ]]
        then
                echo "Error: missing parameter"
                return 1
        else
                echo "Writng to $1"
                FILE=$1
        fi
}
function get_rel {
        case $1 in
                f|friend)
                        R="friend"
                ;;
                l|love)
                        R="love"
                ;;
                pl|possiblelove)
                        R="possiblelove"
                ;;
                k|know)
                        R="definedknow"
                ;;
                h|hate)
                        R="hate"
                ;;
                ph|possiblehate)
                        R="possiblehate"
                ;;
                ld|loved)
                        R="loved"
                ;;
                *)
                        return 1;
                ;;
        esac
        echo $R
}
function a {
        if [[ -z $1 || -z $2 || -z $3 ]]
        then
                echo "Error: missing operator"
                return 1;
        fi
        R=$(get_rel $1)
        echo "$R($2,$3)." >> $FILE.tmp
        if [[ -z $4 ]]
        then
                echo "$R($3,$2)." >> $FILE.tmp
        fi
        cat $FILE.tmp
        cfm_write
}
function c {
        if [[ -z $1 || -z $2 || -z $3 ]]
        then
                echo "Error: missing operator"
                return 1;
        fi
        echo -n "Relationship: "; read rel;
        R=$(get_rel $rel)
        for tar1 in $*
        do
                for tar2 in $*
                do
                        if [[ $tar1 != $tar2 ]]
                        then 
                                echo "$R($tar1,$tar2)." >> $FILE.tmp
                        fi
                done
        done
        cfm_write
}
function cfm_write {
        cat $FILE.tmp
        echo -n "Write to $FILE? [Y/n] "; read yn;
        if [[ -z $yn ]]
        then
                yn="Y"
        fi
        case $yn in 
                y|yes|Y|Yes)
                        echo -n "Writing file... "
                        cat $FILE.tmp >> $FILE
                        echo "OK" 
                ;;
                *)
                        echo "Give up writing."
                ;;
        esac
        rm $FILE.tmp
}
function s {
        cat $FILE
}
function rs {
        echo -n "Sorting and deleting dulipicated relationships... "
        sort $FILE | uniq > $FILE.tmp
        cat $FILE.tmp > $FILE
        rm $FILE.tmp
        echo "OK"
}
function chk chkrel {
        rs
        touch err-chk.log
        
[[ -z $1 ]] && echo -n "Checking for self-to-self error...relationship... "
        for tars in $(cat $FILE)
        do
                [[ ! -z $1 ]] && echo -n "Checking $tars for self-to-self... "
                tar1=$(echo $tars | awk -F"(" '{print $2}' | awk -F")" '{print $1}' | awk -F, '{print $1}')
                tar2=$(echo $tars | awk -F"(" '{print $2}' | awk -F")" '{print $1}' | awk -F, '{print $2}')
                if [[ $tar1 == $tar2 ]]
                then
                        [[ ! -z $1 ]] && echo -n "While processing $tars, found error: self-to-self... " || echo -n "FOUND ERROR... "
                        echo "Self-to-self relationship: $tars" >> err-chk.log
                        ERR1=true
                fi
                [[ ! -z $1 ]] && echo "END"
        done
        [[ -z $1 && -z $ERR1 ]] && echo -n "OK" 
        [[ -z $1 ]] && echo -ne "Checking for relationship... "
        for tars in $(cat $FILE)
        do
                [[ ! -z $1 ]] && echo -n "Checking for relationship: $tars... "
                rel=$(echo $tars | awk -F"(" '{print $1}')
                if [[ $rel != "friend" && $rel != "definedknow" && $rel != "hate" && $rel != "love" && $rel != "possiblelove" && $rel != "possiblehate" && $rel != "loved"  ]]
                then
                        [[ ! -z $1 ]] && echo -n "While processing $tars, found error: Unknow relationship $rel... " || echo -n "FOUND ERROR... "
                        echo "Unknow relationship: $tars" >> err-chk.log
                        ERR2=true
                fi
                [[ ! -z $1 ]] && echo "END"
        done
        [[ -z $1 && -z $ERR2 ]] && echo -n "OK" 
}
function chkrdef {
        [[ -z $1 ]] && echo -ne "\nChecking for redefine... "
        for defs in $(cat $RDB | awk -F"(" '{print $1}' | uniq)
        do
                [[ ! -z $1 ]] && echo -n "Checking redefine of $defs... "
                for tars in $(cat $FILE | awk -F"(" '{print $1}' | uniq)
                do
                        if [[ $defs == $tars ]]
                        then
                                [[ ! -z $1 ]] && echo -n "While processing $tars, found error: Redefined in $FILE... " || echo -n "FOUND ERROR... "
                                echo "Redefine relationships: $tars" >> err-chk.log
                                ERR3=true
                        fi
                done
                [[ ! -z $1 ]] && echo "END"
        done        
        [[ -z $1 && -z $ERR3 ]] && echo -n "OK" 
}
function chksyn {
        [[ -z $1 ]] && echo -ne "\nChecking syntax... "
        for syn in $(cat $FILE)
        do
                [[ ! -z $1 ]] && echo -n "Checking syntax of $syn... "
                if [[ ! $syn =~ .*(.*\,.*)\.$ ]]
                then
                        [[ ! -z $1 ]] && echo -n "While processing $syn, found error: Syntax error... " || echo -n "FOUND ERROR... "
                        echo "Syntax error: $syn" >> err-chk.log
                        ERR4=true
                fi
                [[ ! -z $1 ]] && echo "END"
        done
        [[ -z $1 && -z $ERR4 ]] && echo "OK"
        }
function chk {
        rs
        touch err-chk.log
        chkrel $1; chkrdef $1; chksyn $1;
        
if [[ -z $ERR1 && -z $ERR2 && -z $ERR3 && -z $ERR4 ]]
        then
                echo "Check complete. No error was found."
        else
                echo -e "\n=============================================\nThe following error(s) was found: "
                cat err-chk.log
        fi
        rm err-chk.log
}
function interactive {
echo "rad - Relationship Addition "
echo "\
Available Commands: a - add, c - add complex relationship,
s - show relationship, rs - resort relationship, h - help,
chk - Check possible error"
echo "Default file: ./db.pl"
while true
do
        echo -n "rad> "; read cmd;
        $cmd;
done
}
if [[ -z $* ]]
then
        interactive
else
        $*
fi
exit

Reply to "Re: Re: rad"

Here you can reply to the paste above

captcha