我有以下脚本。我希望输入为:-a
, -l
, -s <number>
, -c <keyword>
, -d <keyword> -b/-r
, -n
。但是,数字和关键字参数以及代表 或 的 extracom 参数存在-b
问题-r
。每当我输入例如 -s 2 或 -c dog 或 -d cat -b 时,该命令不执行任何操作。目录也是一个 txt 文件,其中包含姓名、城市和电话号码的列表(例如 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
先感谢您!