0

I have the following script. I want the inputs to be: -a, -l, -s <number>, -c <keyword>, -d <keyword> -b/-r, -n. However, there's a problem with the number and keyword parameter as well as the extracom parameter which stands for either -b or -r. Whenever I type in e.g. -s 2 or -c dog or -d cat -b the command does nothing. Also catalog is a txt file that has a list of names, surnames, cities and phone numbers (e.g. nick smith london 456523)

#!/bin/bash

        read -rp 'choose: ' opt word extracom
            case $opt in 
            -a) echo Type name\,surname\,city\,phone number\.
                read name surname city tel
                echo $name $surname $city $tel >> list ;;
            -l) echo Shows the contents of the file\:
                cat -n catalog (|sed -i '/^$/d' list);;
            -s\ [1-4]) echo About to show the contents of a file sorted\:
                       sort -k ${opt#"-s "} catalog ;;
            -c) echo Show lines that contain the word $word
                if egrep -q "$word" catalog; then
                   cat catalog | egrep $word
                else
                   echo The string you are looking for does not exist
                fi ;;
            -d) case $extracom in
                -b) if egrep -q "$word" catalog; then
                       sed -i "s/.*$word.*//" catalog 
                   else
                       echo The string you are looking for does not exist
                   fi;;
                -r)if egrep -q "$word" catalog; then  
                      sed -i "/$word/d" catalog
                   else
                      echo The string you are looking for does not exist
                   fi;;
                esac
                ;;
            -n)egrep "^$" catalog | wc -l
                 read -rp 'Do you want to delete the file's empty lines? Yes or no?' answer
            case $answer in 
                yes) sed -i '/^$/d' catalog ;;
                no) cat catalog ;;
            esac;;
            *) echo -e 'Usage Manual:\n-a: New entry to catalog\n-l Displays the contents of the catalog without empty lines\n-s <number>: Displays
            the contents of the file sorted according to the number\n-c <keyword> shows file line that contain that specific keyword\n-d <keyword> -b/-r:
            Deletes empty file lines\n-n: Number of empty file lines and question about deleting them'
            esac

Thank you in advance!

9
  • This is a question in a series of questions from you where you are interactively reading what looks like command line arguments from the user. Why don't you simply write the script to take these options from the actual command line rather than from standard input? This would make it 1) easier to keep track of individual argument (especially if they contain spaces), and 2) possible to use getopts to do the command line parsing. Commented Dec 18, 2019 at 8:03
  • @Kusalananda doesn't it already take the options from the command line? Commented Dec 18, 2019 at 8:05
  • @MargaritaK No, it reads from standard input. What I meant was that your could write your script to be run as ./script.sh -a or ./script -s 20. Currently, it is supposed to be run as ./script.sh, and it then asks choose: at which point the user is supposed to type -a or -s 20. This is not how ordinary scripts and other commands work. Commented Dec 18, 2019 at 8:13
  • @MargaritaK no, it doesn't. Also, what happens if you type the other options on the command-line? My shell definitely complained about a syntax error in your -l option part. Commented Dec 18, 2019 at 8:14
  • @MargaritaK See e.g. how to properly parse shell script flags and arguments using getopts or other questions tagged with getopts. Commented Dec 18, 2019 at 8:15

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.