0

I want to read a project_name and project_path from the user. If the user enters empty name I want to prompt them again to enter some name.

# Get the project name from the user
echo "Enter the project name:"
read project_name

# check if the project name is empty, if empty prompt the user to enter the project name
if [ -z "$project_name" ]; then
   echo "Project name is empty, please enter the project name"
   exit 1
fi

# get the project creation path from the user
echo "Enter the project path:"
read project_path

# Check if the project path is empty, if empty prompt the user to enter the project path
if [ -z "$project_path" ]; then
   echo "Project path is empty, please enter the project path"
   exit 1
fi

# convert project path to absolute path
project_path=$(cd "$project_path" && pwd)


# Create a maven project in the given path with the given project name
cd $project_path
mvn archetype:generate -DgroupId=com.$project_name -DartifactId=$project_name
# extra extension: -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

# navigate to the project folder
cd $project_name
# Delete the src folder
rm -rf src

What I want to do: Get the project_name and project_path from the user and create a maven project(with project_name) in the said project_path. Finally navigate to the newly created project.

Problem: When I'm running the project with permission: chmod +x create.sh the entire directory is being created in the script containing directory.

Example: My script is inside /Desktop/bash. After granting permission when I'm running the script the maven project is getting created in the /Desktop/bash and not the provided path.

What's the issue? Permissions or reading the path?

EDIT: Resulting command line:

Enter the project name:
test
Enter the project path:
~/Desktop/java
./install.sh: line 32: cd: ~/Desktop/java: No such file or directory

After creating maven project I'm getting this in the prompt

Project created from Archetype in dir: /root/test
6
  • Could you provide the beginning of the part which actually creates the Maven files. Perhaps something like echo "…" > "$project_path/pom.xml". Without this part, it is difficult to know why the project_path is ignored. Nov 12, 2021 at 21:53
  • hi, I've added my entire code. Can you check please?
    – user458847
    Nov 12, 2021 at 22:02
  • Your issue is with the tilde (~) not expanding since it's never interpreted by the shell. Your users would be better served by providing the needed data as arguments on the scripts command line. That would allow them to use globbing patterns, tab completion, tilde and whatever other (possibly scripted) way of calling your script.
    – they
    Nov 12, 2021 at 22:08
  • @they You mean this line: ~/Desktop/java? So if someone wants to create the project in Desktop he/she would have to enter /home/user/Desktop? Would you be kind enough to provide an example? About data as arguments on the scripts command line.? Thank you so much
    – user458847
    Nov 12, 2021 at 22:18
  • 1
    @FahimHoque unix.stackexchange.com/questions/31414/…
    – they
    Nov 13, 2021 at 6:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy