Home > Software design >  Terminal can't understand Bash script; permission denied error?
Terminal can't understand Bash script; permission denied error?

Time:10-06

My script was working fine until now. When I call ./mkproj.sh from the terminal, it displays that it has two arguments (I assume because I call it with $1 and $2) and then proceeds to give the following error messages:

num arguments 2

mkdir: cannot create directory ‘/archive’: Permission denied

mkdir: cannot create directory ‘/backups’: Permission denied

mkdir: cannot create directory ‘/docs’: Permission denied

mkdir: cannot create directory ‘/docs’: Permission denied

mkdir: cannot create directory ‘/assets’: Permission denied

mkdir: cannot create directory ‘/database’: Permission denied

mkdir: cannot create directory ‘/src’: Permission denied

mkdir: cannot create directory ‘/src’: Permission denied

I have checked my files, including hidden, and have no file named myproject. I also have execute, read, and write permissions on mkproj.sh. Here is my script:

#!/bin/bash
check_for_file()
{
echo "first argument $0"
echo "second argument $1"
echo "third argument $2"
echo "num arguments $#"
if [ $# -eq 0 ]; then
        local testing_file=myproject
        if [ -d "$testing_file" ]; then
                echo "Directory name already exists"
                exit

        else
                mkdir -p "$testing_file"/{archive,backups,docs/{html,txt},assets,database,src/{sh,c}}
        fi
else
        local testing_file=$1

        if [ -d "$testing_file" ]; then
                echo "Directory name already exists"
                exit
        else
                mkdir -p "$testing_file"/{archive,backups,docs/{html,txt},assets,database,src/{sh,c}}
        fi
fi
}

check_for_file "$1" "$2"

Thank you!

CodePudding user response:

It's because you are trying to create a folder in "/" (the root folder) and you need to be sudo to do that. Try to add something like this before your if

cd $pwd

CodePudding user response:

It seems that you want to find $testing_file in your certain directory, and if you can’t find anything, seems you want to make a directory named $testing_file.

Then, at least you need to fix your code like:

if [ -d ./$testing_file ]; then

You need to change your testing_file path to the relative path or absolute path(full path).

In your original code, your if conditional expressions are checking if there is $testing_file at root directory(/) or not.

  • Related