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!
getoptsto do the command line parsing../script.sh -aor./script -s 20. Currently, it is supposed to be run as./script.sh, and it then askschoose:at which point the user is supposed to type-aor-s 20. This is not how ordinary scripts and other commands work.-loption part.