1

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.

AEM
  • 1,354
  • 8
  • 20
  • 30
  • BTW, consider running your code through http://shellcheck.net/ and fixing what it covers. The `-a` and `-o` predicates in `test` are marked obsolescent in the POSIX standard document; `[ foo ] && [ bar ]` avoids ambiguities in how `[` parses its arguments and is the current replacement for `[ foo -a bar ]`. – Charles Duffy Apr 08 '23 at 16:25
  • 1
    @AEM you previously mentioned in a (now-deleted) comment that you found and fixed the issue; to help future readers of this Q&A you may want to update the question to include a brief description of the exact issue in your code and how you addressed it; regards – markp-fuso Apr 08 '23 at 16:28
  • (well, the "and how you addressed it" part would be better edited into the answer, but with that answer being community-wiki, that's a thing anyone is welcome to do). – Charles Duffy Apr 08 '23 at 17:50
  • @markp-fuso I don't get it sorry. Should I edit the question and add what helped me to solve the issue or what do you mean? – AEM Apr 08 '23 at 23:45
  • @CharlesDuffy Thanks a lot for the feedback. I will try to clarify the problem in more details. – AEM Apr 08 '23 at 23:47
  • @CharlesDuffy I edited the question. Can you please check and tell me what needs to be improved? Thanks a lot for the feedback. – AEM Apr 09 '23 at 00:03
  • @AEM the (accepted) answer was more of a 'guess' so I was just suggesting you provide a confirmation on the final resolution and it looks like you've done exactly that with the latest edit of the question; thanks – markp-fuso Apr 09 '23 at 00:10
  • Possible duplicate of [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory) – tripleee Apr 11 '23 at 03:43

1 Answers1

2

A couple questions/observations:

  • the main script calls List_DB.sh but this file is not in your ls -l output, are your scripts located in different directories?
  • since you're sourcing the script files the 'execute' permission is not needed

Keep in mind:

  • when a script is exec'd a subshell will be spawned and most operations in the subshell (eg, variable assignments, cd operations) are 'lost' when the script exits (in the case of cd it's as if the cd is 'undone' and you're left back in the directory where you started)
  • in the case of a sourced script the commands (in the script) are performed in the current shell so when the script exits any changes made in the shell (variable assignments, cd operations) remain in the main shell (in the case of a cd you are not returned to where you started)

My guess: the last successfully sourced script (List_DB.sh ?) performs an operation (eg, cd) that leaves the parent script sitting in a different directory hence the error message.

markp-fuso
  • 28,790
  • 4
  • 16
  • 36