Skip to content

How to execute script only if a single parameter is given #246

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Nottt opened this issue Dec 23, 2019 · 9 comments
Closed

How to execute script only if a single parameter is given #246

Nottt opened this issue Dec 23, 2019 · 9 comments
Labels

Comments

@Nottt
Copy link

Nottt commented Dec 23, 2019

      {
         "name":"ID",
         "type":"text",
         "required":false
      },
      {
         "name":"NAME",
         "type":"text",
         "required":false
      }

Given those parameters, and a single script that will execute different stuff based if a ID or NAME is given how you would do this?

I can't use if [ ! -z "$1" ] for example. Because if someone only gives a name, it will be $1 and vice versa.

I tried to use "param":"2", in my json because I thought that way I could control that $1 will always be a ID and $2 a NAME

@bugy
Copy link
Owner

bugy commented Dec 23, 2019

Hi @Nottt unfortunately, that's not possible. If parameter is not specified, it's not passed at all

@Nottt
Copy link
Author

Nottt commented Dec 23, 2019

So what I should do to accomplish what I want? I don't want to clutter the UI with lots of scripts

@Nottt
Copy link
Author

Nottt commented Dec 23, 2019

So what I should do to accomplish what I want? I don't want to clutter the UI with lots of scripts.

I'm not sure you understood me...what I saying is that I want to control inside the script what variable is what...

Either by controlling the order (e.g first parameter is always $1, second is $2) or by other means... like I define that first parameter will be variable X so I can use that with if statements

@bugy
Copy link
Owner

bugy commented Dec 23, 2019 via email

@bugy
Copy link
Owner

bugy commented Dec 23, 2019 via email

@Nottt
Copy link
Author

Nottt commented Dec 23, 2019

+1 for #181 😄

I think I'm using "param" wrong then.

      {
         "name":"ID",
         "type":"text",
         "param":"id",
         "required":false
      },
      {
         "name":"Nome",
         "type":"text",
         "param":"name",
         "required":false
      }

With this script:

echo $id
echo $name

Doesn't work.

@bugy
Copy link
Owner

bugy commented Dec 23, 2019 via email

@Nottt
Copy link
Author

Nottt commented Dec 23, 2019

Thanks for the support! This is working:

      {
         "name":"ID",
         "type":"text",
         "param":"-id",
         "required":false
      },
      {
         "name":"Nome",
         "type":"text",
         "param":"-name",
         "required":false
      }

In the script:

POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -id)
    ID="$2"
    shift
    shift
    ;;
    -name)
    NAME="$2"
    shift
    shift
    ;;
esac
done
set -- "${POSITIONAL[@]}"

@bugy bugy added the feature label Dec 26, 2019
@bugy bugy changed the title How to execute script only if a single parameter is given Add possibility to send parameters to a script even if they are empty Dec 26, 2019
@bugy bugy removed the feature label Dec 26, 2019
@bugy bugy changed the title Add possibility to send parameters to a script even if they are empty How to execute script only if a single parameter is given Dec 26, 2019
@bugy bugy added the question label Dec 26, 2019
@bugy
Copy link
Owner

bugy commented Dec 26, 2019

I added a ticket (#247) to address this problem in a different manner: by always passing parameters, even if they are empty
In this case $1 parameter would exist and if [ ! -z "$1" ] check will work properly (because the value will be empty)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants