Re: rad

From NAT, 6 Years ago, written in Bash, viewed 1'043 times. This paste is a reply to rad from NAT - view diff
URL https://code.nat.moe/view/5f787dce Embed
Download Paste or View Raw
  1. #!/bin/bash
  2. FILE=./db.pl
  3. RDB=./rdb.pl
  4. function h {
  5.         echo "\
  6. ========================= Command Help =========================
  7. Usage: a <rel> <tar1> <tar2> [non-dw]
  8.       rel: Relationships(f/l/pl/k/h/ph)
  9.       tar1: Target 1
  10.       tar2: Target 2
  11.       non-dw: Set any value to this if the relationship is not
  12.               double way
  13. Usage: c <tar1> <tar2> [tar3] [tar4] ... [tarN]
  14.       Enter any names (at least 2) to create a compound relati
  15.       -onship. ONLY give names as parameters, script will prom
  16.       -t you to input relationship.
  17. Usage: f <file>
  18.       Change file to write (Default ./db.pl)"
  19.  
  20. }
  21. function f {
  22.         if [[ -z $1 ]]
  23.         then
  24.                 echo "Error: missing parameter"
  25.                 return 1
  26.         else
  27.                 echo "Writng to $1"
  28.                 FILE=$1
  29.         fi
  30. }
  31. function get_rel {
  32.         case $1 in
  33.                 f|friend)
  34.                         R="friend"
  35.                 ;;
  36.                 l|love)
  37.                         R="love"
  38.                 ;;
  39.                 pl|possiblelove)
  40.                         R="possiblelove"
  41.                 ;;
  42.                 k|know)
  43.                         R="definedknow"
  44.                 ;;
  45.                 h|hate)
  46.                         R="hate"
  47.                 ;;
  48.                 ph|possiblehate)
  49.                         R="possiblehate"
  50.                 ;;
  51.                 ld|loved)
  52.                         R="loved"
  53.                 ;;
  54.                 *)
  55.                         return 1;
  56.                 ;;
  57.         esac
  58.         echo $R
  59. }
  60. function a {
  61.         if [[ -z $1 || -z $2 || -z $3 ]]
  62.         then
  63.                 echo "Error: missing operator"
  64.                 return 1;
  65.         fi
  66.         R=$(get_rel $1)
  67.         echo "$R($2,$3)." >> $FILE.tmp
  68.         if [[ -z $4 ]]
  69.         then
  70.                 echo "$R($3,$2)." >> $FILE.tmp
  71.         fi
  72.         cat $FILE.tmp
  73.         cfm_write
  74. }
  75. function c {
  76.         if [[ -z $1 || -z $2 || -z $3 ]]
  77.         then
  78.                 echo "Error: missing operator"
  79.                 return 1;
  80.         fi
  81.         echo -n "Relationship: "; read rel;
  82.         R=$(get_rel $rel)
  83.         for tar1 in $*
  84.         do
  85.                 for tar2 in $*
  86.                 do
  87.                         if [[ $tar1 != $tar2 ]]
  88.                         then
  89.                                 echo "$R($tar1,$tar2)." >> $FILE.tmp
  90.                         fi
  91.                 done
  92.         done
  93.         cfm_write
  94. }
  95. function cfm_write {
  96.         cat $FILE.tmp
  97.         echo -n "Write to $FILE? [Y/n] "; read yn;
  98.         if [[ -z $yn ]]
  99.         then
  100.                 yn="Y"
  101.         fi
  102.         case $yn in
  103.                 y|yes|Y|Yes)
  104.                         echo -n "Writing file... "
  105.                         cat $FILE.tmp >> $FILE
  106.                         echo "OK"
  107.                 ;;
  108.                 *)
  109.                         echo "Give up writing."
  110.                 ;;
  111.         esac
  112.         rm $FILE.tmp
  113. }
  114. function s {
  115.         cat $FILE
  116. }
  117. function rs {
  118.         echo -n "Sorting and deleting dulipicated relationships... "
  119.         sort $FILE | uniq > $FILE.tmp
  120.         cat $FILE.tmp > $FILE
  121.         rm $FILE.tmp
  122.         echo "OK"
  123. }
  124. function chk {
  125.         rs
  126.         touch err-chk.log
  127.         [[ -z $1 ]] && echo -n "Checking for self-to-self error... "
  128.         for tars in $(cat $FILE)
  129.         do
  130.                 [[ ! -z $1 ]] && echo -n "Checking $tars for self-to-self... "
  131.                 tar1=$(echo $tars | awk -F"(" '{print $2}' | awk -F")" '{print $1}' | awk -F, '{print $1}')
  132.                 tar2=$(echo $tars | awk -F"(" '{print $2}' | awk -F")" '{print $1}' | awk -F, '{print $2}')
  133.                 if [[ $tar1 == $tar2 ]]
  134.                 then
  135.                         [[ ! -z $1 ]] && echo -n "While processing $tars, found error: self-to-self... " || echo -n "FOUND ERROR... "
  136.                         echo "Self-to-self relationship: $tars" >> err-chk.log
  137.                         ERR1=true
  138.                 fi
  139.                 [[ ! -z $1 ]] && echo "END"
  140.         done
  141.         [[ -z $1 && -z $ERR1 ]] && echo -n "OK"
  142.         [[ -z $1 ]] && echo -ne "Checking for relationship... "
  143.         for tars in $(cat $FILE)
  144.         do
  145.                 [[ ! -z $1 ]] && echo -n "Checking for relationship: $tars... "
  146.                 rel=$(echo $tars | awk -F"(" '{print $1}')
  147.                 if [[ $rel != "friend" && $rel != "definedknow" && $rel != "hate" && $rel != "love" && $rel != "possiblelove" && $rel != "possiblehate" && $rel != "loved"  ]]
  148.                 then
  149.                         [[ ! -z $1 ]] && echo -n "While processing $tars, found error: Unknow relationship $rel... " || echo -n "FOUND ERROR... "
  150.                         echo "Unknow relationship: $tars" >> err-chk.log
  151.                         ERR2=true
  152.                 fi
  153.                 [[ ! -z $1 ]] && echo "END"
  154.         done
  155.         [[ -z $1 && -z $ERR2 ]] && echo -n "OK"
  156.         [[ -z $1 ]] && echo -ne "\nChecking for redefine... "
  157.         for defs in $(cat $RDB | awk -F"(" '{print $1}' | uniq)
  158.         do
  159.                 [[ ! -z $1 ]] && echo -n "Checking redefine of $defs... "
  160.                 for tars in $(cat $FILE | awk -F"(" '{print $1}' | uniq)
  161.                 do
  162.                         if [[ $defs == $tars ]]
  163.                         then
  164.                                 [[ ! -z $1 ]] && echo -n "While processing $tars, found error: Redefined in $FILE... " || echo -n "FOUND ERROR... "
  165.                                 echo "Redefine relationships: $tars" >> err-chk.log
  166.                                 ERR3=true
  167.                         fi
  168.                 done
  169.                 [[ ! -z $1 ]] && echo "END"
  170.         done   
  171.         [[ -z $1 && -z $ERR3 ]] && echo -n "OK"
  172.         [[ -z $1 ]] && echo -ne "\nChecking syntax... "
  173.         for syn in $(cat $FILE)
  174.         do
  175.                 [[ ! -z $1 ]] && echo -n "Checking syntax of $syn... "
  176.                 if [[ ! $syn =~ .*(.*\,.*)\.$ ]]
  177.                 then
  178.                         [[ ! -z $1 ]] && echo -n "While processing $syn, found error: Syntax error... " || echo -n "FOUND ERROR... "
  179.                         echo "Syntax error: $syn" >> err-chk.log
  180.                         ERR4=true
  181.                 fi
  182.                 [[ ! -z $1 ]] && echo "END"
  183.         done
  184.         [[ -z $1 && -z $ERR4 ]] && echo "OK"
  185.         if [[ -z $ERR1 && -z $ERR2 && -z $ERR3 && -z $ERR4 ]]
  186.         then
  187.                 echo "Check complete. No error was found."
  188.         else
  189.                 echo -e "\n=============================================\nThe following error(s) was found: "
  190.                 cat err-chk.log
  191.         fi
  192.         rm err-chk.log
  193. }
  194. function interactive {
  195. echo "rad - Relationship Addition "
  196. echo "\
  197. Available Commands: a - add, c - add complex relationship,
  198. s - show relationship, rs - resort relationship, h - help,
  199. chk - Check possible error"
  200. echo "Default file: ./db.pl"
  201. while true
  202. do
  203.         echo -n "rad> "; read cmd;
  204.         $cmd;
  205. done
  206. }
  207. if [[ -z $* ]]
  208. then
  209.         interactive
  210. else
  211.         $*
  212. fi
  213. exit

Replies to Re: rad rss

Title Name Language When
Re: Re: rad NAT bash 6 Years ago.

Reply to "Re: rad"

Here you can reply to the paste above

captcha