SHARE
TWEET

Untitled

a guest Jun 16th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. Create () {
  4.  
  5.     #Authors
  6.  
  7.     declare -i n=1
  8.     array=()
  9.  
  10.     while :
  11.     do
  12.         if [ "$n" -ge "2" ]; then
  13.             echo -n "Do you have more authors?[Yes/No]:"
  14.             read result
  15.             if [ "$result" != "Yes" ]; then
  16.                 break;
  17.             fi
  18.         fi
  19.    
  20.         echo -n "Please enter a name of author$n:"
  21.         read author
  22.         echo $author
  23.         if [ "$author" == "" ]; then
  24.              echo "Author input is incorrect"
  25.         else
  26.             array[$n]=$author
  27.         n=$(( n + 1 ))
  28.         fi
  29.     done
  30.  
  31.     printf "\nAuthors:\n"
  32.     for item in ${array[*]}
  33.     do
  34.         printf "   %s\n" $item
  35.     done
  36.  
  37.     #Titles
  38.     while :
  39.     do
  40.         echo -n "Please enter a title of a book: "
  41.         read title
  42.         if [ "$title" == "" ]; then
  43.             echo "Title is incorrect. Please try again"
  44.         else
  45.             break;
  46.         fi
  47.     done
  48.  
  49.     printf "\nTitle:\n"
  50.     printf "   %s\n" $title
  51.  
  52.     #Pages
  53.     while :
  54.     do
  55.         echo -n "Please enter a number of pages: "
  56.         read pages
  57.         if [ "$pages" != "" ] && [ $pages -eq $pages 2>/dev/null ] && [ $pages -gt "0" ]; then
  58.             break
  59.         else
  60.             echo "Please write a correct number of pages"
  61.         fi
  62.     done
  63.  
  64.     printf "\nNumber of pages:\n"
  65.     printf "   %s\n" $pages
  66.  
  67.     #Year
  68.     while :
  69.     do
  70.         echo -n "Please enter a year of book's release: "
  71.         read year
  72.         currentYear=$(date +'%Y')
  73.         if [ "$year" != "" ] && [ $year -eq $year 2>/dev/null ] && [ $year -gt "1000" ] && [ $year -le $currentYear ]; then
  74.             break
  75.         else
  76.             echo "Please write a correct year"
  77.         fi
  78.     done
  79.  
  80.     printf "\nYear:\n"
  81.     printf "   %s\n" $year
  82.  
  83.     #Other values
  84.  
  85.     keys=()
  86.     values=()
  87.     declare -i in=0
  88.  
  89.     while :
  90.     do
  91.         echo -n "Do you want something else(key: value)?[Yes/No]:"
  92.         read result
  93.             if [ "$result" != "Yes" ]; then
  94.             break
  95.         fi
  96.    
  97.         while :
  98.         do
  99.             echo -n "Please enter a key: "
  100.             read key
  101.             if [ "$key" == "" ]; then
  102.                 echo "Key is incorrect. Please try again"
  103.             else
  104.                 keys[in]=$key
  105.                 break
  106.             fi
  107.         done
  108.  
  109.         while :
  110.         do
  111.             echo -n "Please enter a value: "
  112.             read value
  113.             if [ "$value" == "" ]; then
  114.                 echo "Value is incorrect. Please try again"
  115.             else
  116.                 values[$in]=$value
  117.                 in=$(( in + 1 ))
  118.                 break;
  119.             fi
  120.         done
  121.     done
  122.  
  123.     printf "\nKeys and values:\n"
  124.  
  125.     if [[ ${!values[*]} != "0" ]]; then
  126.         for index in ${!values[*]}
  127.         do
  128.             printf "%s: %s\n" ${keys[$index]} ${values[$index]}
  129.         done
  130.     fi 
  131.  
  132.     #Creating a record
  133.    
  134.     number=1
  135.  
  136.     while :
  137.     do
  138.         recordName="record$number.txt"
  139.         if [ -f "$recordName" ]; then
  140.             number=$(( number + 1))
  141.         else
  142.             break;
  143.         fi
  144.     done
  145.  
  146.     #Authors
  147.     for item in ${array[*]}
  148.     do
  149.         echo "author: $item" >> $recordName
  150.     done
  151.  
  152.     #Titles
  153.     echo "title: $title" >> $recordName
  154.  
  155.     #Pages
  156.     echo "pages: $pages" >> $recordName
  157.    
  158.     #Year
  159.     echo "year: $year" >> $recordName
  160.    
  161.     #Other
  162.     for index in ${!values[*]}
  163.     do
  164.         echo "${keys[$index]}: ${values[$index]}" >> $recordName
  165.     done
  166.  
  167.     echo "Record is successfully created. Name: $recordName"
  168. }
  169.  
  170. Search () {
  171.  
  172.     author=""
  173.     title=""
  174.  
  175.     echo "Do you want to search by author?[Yes/No]"
  176.     read result
  177.     if [ "$result" == "Yes" ]; then
  178.         while :
  179.         do
  180.             echo -n "Please enter an author of a book: "
  181.             read author
  182.             if [ "$author" == "" ]; then
  183.                 echo "Author name is incorrect. Please try again"
  184.             else
  185.                 break;
  186.             fi
  187.         done
  188.     fi
  189.  
  190.     echo "Do you want to search by title?[Yes/No]"
  191.     read result
  192.     if [ "$result" == "Yes" ]; then
  193.         while :
  194.         do
  195.             echo -n "Please enter an title of a book: "
  196.             read title
  197.             if [ "$title" == "" ]; then
  198.                 echo "Title name is incorrect. Please try again"
  199.             else
  200.                 break;
  201.             fi
  202.         done
  203.     fi
  204.    
  205.     found="false"
  206.     printf "\n"
  207.  
  208.     for i in record*.txt;
  209.     do
  210.         if [ "$author" != "" ]; then
  211.             result1=`awk '$1 == "author" && $2 == "'"$author"'" {n++}END{print n}' FS=": "  $i`
  212.            
  213.             if [ "$title" == "" ] && [ "$result1" != "" ]; then
  214.                 echo "Record with author $author found. Name: $i"
  215.                 found="true"
  216.             fi
  217.         fi
  218.  
  219.         if [ "$title" != "" ]; then
  220.             result2=`awk '$1 == "title" && $2 == "'"$title"'" {n++}END{print n}' FS=": "  $i`
  221.             echo $result2;
  222.             if [ "$result2" != "" ]; then
  223.                 if [ "$author" != "" ] && [ "$result1" != "" ]; then
  224.                     echo "Record with author $author and title $title found. Name: $i"
  225.                     found="true"
  226.                 else
  227.                     echo "Record with title $title found. Name: $i"
  228.                     found="true"           
  229.                 fi
  230.             fi
  231.         fi     
  232.     done
  233.    
  234.    
  235.     if [ "$author" == "" ] && [ "$title" == "" ]; then
  236.         printf "No input. Impossible to search.\n"
  237.     elif [ "$found" == "false" ]; then
  238.         printf "\nNo result. Nothing found\n"
  239.     fi
  240. }
  241.  
  242. Delete () {
  243.     while :
  244.     do
  245.         echo -n "Please enter a name of record you want to delete: "
  246.         read record
  247.         if [ "$record" == "" ]; then
  248.             echo "Record name is incorrect. Please try again"
  249.         else
  250.             break;
  251.         fi
  252.     done
  253.  
  254.     if [ -f "$record" ]; then
  255.         rm "$record"
  256.         echo "Record $record was successfully deleted"
  257.     else
  258.         echo "No record with name $record was found"
  259.     fi
  260. }
  261.  
  262. #Main
  263. if [ "$1" == "" ]; then
  264.     echo "Parameter is empty. Allowed parameters: create, delete, search"
  265. elif [ "$1" == "create" ]; then
  266.     Create
  267. elif [ "$1" == "delete" ]; then
  268.         Delete
  269. elif [ "$1" == "search" ]; then
  270.     Search
  271. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top