Script problem with deleting data

The 10.Delete data doesnt work at all, plesase anyone could help on that. When I choose options 10 to delete a record it only copy the selected data on the other file dbs1 but doesnt delete it from the database where other records are.

#! /bin/bash
i="y"
echo "Enter name of database "
read db
while [ $i = "y" ]
do
clear
echo "1.Enter id "
echo "2.Display current dir"
echo "3.Listing the dir"
echo "4.Make a dir"
echo "5.Copy a file"
echo "6.Rename file"
echo "7.Delete file"
echo "8.Edit file"
echo "9.Add e record"
echo "10.Delete data"
echo "11.Exit"
echo "Enter your choice"
read ch
case $ch in
1)echo "Enter id "
read id
grep -i "$id" $db ;;
2)echo "Current Dir is : "
  pwd;;
  3)echo "Directories are"

    ls;;
    4)echo "Enter dir name to create"
     read d
             mkdir $d
                 echo $d" Dir is created";;
             5)echo "Enter filename from copy"
              read f1
               echo "Enter filenm2 to be copied"
                read f2
                    #cat $f1 > $f2
                        cp $f1 $f2
                            echo $f2" is copied from "$f1;;
                        6)echo "Enter file name to rename"
                         read f1
                          echo "Enter new name of file"
                           read f2
                                   mv $f1 $f2
                                       echo $f1" is renamed as "$f2;;
                                   7)echo "Enter any filenm to be delete"
                                    read f1
                                            rm $f1
                                                echo $f1" is deleted";;
                                            8)echo "Enter any file to be editing "
                                             read f1
                                                     vi $f1;;
9)echo "Enter the name"
read kdi
echo �$kdi� > dbkodi
cat dbkodi
echo "Enter the profesion:"
read tpi
echo �$tpi� > dbtitulli
cat dbtitulli
echo "Enter the school "
read ept
echo �$ept� > dbemri_piktorit
cat dbemri_piktorit
echo "Enter the class"
read cmm
echo �$cmm� >dbcmimi
cat dbcmimi
echo "$kdi $tpi $ept $cmm">>$db;;
10)echo "Enter Id"
read id
# set -a
sed '/$id/d' $db > dbs1
grep -v "$id" $db > dbs1
echo "Record is deleted"
cat dbs1;; 

                                                 11)echo "Have a nice time"
                                                 exit;;
                                                 *)echo "Invalid choice entered";;
                                                 esac
                                                 echo "Do u want to continue ? "
                                                 read i
                                                 if [ $i != "y" ]
                                                 then
                                                 exit
                                                 fi
                                                 done

Please show an example of how it is not working.
Not working at all, only partially, etc...

1 Like

When I put the ID of the data I want to remove it only copy the data to dbs1 and doesnt delete it from the database in which are the other datas.

sed '/$id/d' $db > dbs1
grep -v "$id" $db > dbs1

In theory, the 2nd command is how you could remove data from a file.

it just doesnt work even if the mesagge printed is "Data deleted", anyway thanx for the replay :slight_smile:

What happens if you manually execute that command?
Or, execute the command without the -v option? Without the -v option, the result would demonstrate what meets the criteria.

grep -v "stuff" <myfile

should copy all except for lines with "stuff"
while,

grep "stuff" <myfile

would show you what contains "stuff"

As I read your code, I do not think the grep is analyzing the input file. It is early on a Sunday morning for me, but I think you would want something like:

grep -v "stuff" <myfile >mynewfile
1 Like

its pretty the same when I exeute the comand without -v, I mean its a way better in terminal but still doesnt delete the data from database, maybe its something wrong with the whole code, idk...Thank you for your kind replay, apreciate it :smiley: