0

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!

gogo
  • 53
  • 1
  • 12
  • 1
    What are you calling your script as? Without any arguments your call to `check_for_file "$1" "$2"` calls `check_for_file` with empty arguments. But `$#` will still see the two empty strings so `$#` will still be two. Do your `else` clause runs with an empty $1, so the files are written to `/` – SamBob Oct 05 '21 at 13:37
  • It seems that `testing_file` path is missing. Did you check your `testing_file` path on `if [ -d "$testing_file" ];`? You need to change it to full path or relative path. – Aiden Yeomin Nam Oct 05 '21 at 13:52
  • How would I check for the testing_file path? im not sure what that means. – gogo Oct 05 '21 at 13:56
  • The variable "myproject" is not defined, so when you do "testing_file=myproject" "testing_file" will be empty and later when you try to created folders using the variable "testing_file" you are actually creating files under "/" and the user running the script doesn't have permissions for that. –  Oct 05 '21 at 15:10
  • @gogo: You did not write how you invoke your script, so it's hard to tell what **exactly** is going wrong. Why don't you run your script with `-x` and just look why and where it bails out? – user1934428 Oct 05 '21 at 15:40

2 Answers2

0

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
0

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.

Aiden Yeomin Nam
  • 315
  • 5
  • 16
  • Thank you, how do I change a file path? Do I need to make a path variable? What if $1 comes with a path attached to it? (sorry for all the questions) – gogo Oct 05 '21 at 14:07
  • hmm.. you mean like `../tmp/myproject` or `/home/user/Documents/tmp/myproject`? Try this https://stackoverflow.com/a/4045350/6345487 – Aiden Yeomin Nam Oct 05 '21 at 14:17