-1

I have this shell script but I don't know why it doesn't work after passing parameters to it...

Here it is


#!/bin/bash

# Check the argument type and change the directory depending on it.
# Open with code


create() {
    directory="E:/husseinabbas/programming/Code"
    use_directory=""
    project_name="$1"
    project_type="$2"
    project_mode="$3" || "new"
    repository_name="$4" || "$project_name"
    repository_type="$5" || false
    if [[ "$project_type" == "html" ]]
      then
        use_directory="${directory}/html_css/$project_name"
    elif [[ "$project_type" == "node" ]] 
      then 
        use_directory="{$directory}/node-projects/$project_name"
    elif [[ "$project_type" == "php" ]] 
      then 
        use_directory="F:/XAMPP/htdocs/$project_name"
    elif [[ "$project_type" == "python" ]] 
      then 
        use_directory="{$directory}/python-projects/$project_name"
    elif [[ "$project_type" == "flutter" ]] 
      then 
        use_directory="{$directory}/android/$project_name"
    elif [[ "$project_type" == "test" ]] 
      then 
        use_directory="C:/Users/H.Abbas/Desktop/$project_name"
    else 
        return
    fi
    # Activate the venv
    cd "E:/husseinabbas/programming/Code/python-projects/day_automator"
    source venv/Scripts/activate
    python day_automator.py "$project_name" "$project_type" "$project_mode" "$repository_name" "$repository_type"
    cd "$use_directory" || return
    echo "# $project_name" >> README.md
    git init
    git add README.md
    git commit -m "init"
    git branch -M main
    git remote add origin https://github.com/husseinYY/$repository_name.git
    git push -u origin main
    code .
    
}

"$@"

And I'm using this command to run it;

sh .\create.sh my_day python all my_day false

And it gives me this:-

.\create.sh: line 52: my_day: command not found

I have read other answers to this question but they weren't helpful

This paragraph is just here because Stackoverflow robot wants more details hehe

  • You normally run scripts in the same directory with ./ and not .\ Also, it is good practice to enclose your parameters in quotes. – Raman Sailopal Mar 12 '21 at 15:00
  • `project_mode="$3" || "new"` is not how you supply default values in the shell; use `project_mode="${3:-new}"` instead (see [this question](https://stackoverflow.com/questions/9332802/how-to-write-a-bash-script-that-takes-optional-input-arguments)). – Gordon Davisson Mar 12 '21 at 19:17

1 Answers1

1

In your script you are not calling your function create, so nothing is done.

Also you are expanding "$@" at the end: this prints the shell arguments you passed trying to execute them. The error message comes from "Line 52" that is exactly where $@ is.

In your command:

sh ./create.sh my_day python all my_day false
  • $0 is the path of the script ./create.sh
  • $1 is my_day
  • $2 is python
  • ...

$@ expands from $1 to the final argument, this is why you receive error on my_day that is in fact $1.

Prepend create to "$@" and should be ok.

Check also What does $@ mean in a shell script?

piertoni
  • 1,933
  • 1
  • 18
  • 30
  • 2
    Maybe "Replace `"$@"` with `create "$@"` and should be ok"? – omajid Mar 12 '21 at 15:27
  • Yes, also. If you write only `create` it will take arguments from the main script so It will work. But I like your solution more – piertoni Mar 12 '21 at 16:00
  • 1
    @piertoni Just using `create` will *not* pass on the arguments to the main script, it will run the `create` function with no arguments. – Gordon Davisson Mar 12 '21 at 19:16