I am working on DBMS project using bash script. I have a problem No such file or directory
but the file already exists and it's working too but when I source this file from Main_Menu
file it's not working as expected. Sometimes it's working and sometimes not.
Drop_DB.sh:
#!/bin/bash
read -rp "Enter the name of db you want to remove: " db_name
# we use this variable as flag to check everything is ok.
error=""
# function for validations
function validation {
if [ -d "./DataBases" ]
then
error="0"
else
error="1"
echo "Error! DataBases file not exist ❌"
fi
if [ -d "./DataBases/$db_name" -a $error = 0 ]
then
error="0"
else
error="1"
echo "Error! No Database with this name: $db_name ❌"
fi
}
# Call function validation first
validation
if [ $error -eq 0 ]
then
rm -r "./DataBases/$db_name"
echo "Database: $db_name deleted successfully ✓ ✓"
else
echo "Can't complete this action ❌"
fi
List_DB.sh:
#!/bin/bash
error=""
function validation {
if [ -d "./DataBases" ]
then
error="0"
else
error="1"
echo "Error! DataBases file not exist ❌"
fi
if [ ! -d "./DataBases/" -a $error = 0 ]
then
error="1"
echo "Error! There's no DBs yet ❌"
else
error="0"
fi
}
# call validation function
validation
if [ $error -eq 0 ]
then
# 3 - display only directories.
cd ./DataBases
ls -d */
else
echo "Can't complete this action ❌❌"
fi
Main_Menu.sh
#!/bin/bash
options=("Create Database" "List Databases" "Connect To Databases" "Drop Database" "Exit")
PS3="Enter choices from 1 to 5: "
while [[ "$choice" != "Exit" ]]
do
echo " "
select choice in "${options[@]}"
do
case $choice in
"Create Database" ) source ./Create_DB.sh
break
;;
"List Databases" ) source ./List_DB.sh
break
;;
"Connect To Databases" ) source ./Connect_DB.sh
break
;;
"Drop Database" ) source ./Drop_DB.sh
break
;;
"Exit" ) exit
break
;;
*) echo "Invalid input please try again later"
;;
esac
done
done
I thought the file needed execute permission but I was wrong:
$ ls -l
-rwxrwxr-x. 1 aem aem 804 Apr 8 14:45 Drop_DB.sh
-rwxrwxr-x. 1 aem aem 696 Apr 8 14:41 Mainmenu.sh
Here's the error:
./Mainmenu.sh: line25: ./Drop_DB.sh: No such file or directory
Also, I have tried this: "No such file or directory" but it exists but still have the same error.
Edit:
My problem here is that I did cd
in the List_DB.sh
file to list all directories there. So, after executing List_DB.sh
, my pwd
was DataBases/$DB_Name
directory and scripts are located in another path which means No such file or directory
in DataBases/$DB_Name
. I solved this problem by adding cd ..
in List_DB.sh
file to go back to the main folder.