0
param(
[string]$pod,
[string]$username,
[string]$pass,
[string]$environment,
[string]$delay,
[string]$utype,
[string]$etype,
[string]$url
)


$cmd = "protactor  " + $f.name + " ";
    if($pod -ne ""){
    $cmd+=" --params.pod=$pod";
    }
    if($username -ne ""){
    $cmd+=" --params.user=$username";
    }
    if($pass -ne ""){ 
    $cmd+=" --params.pass=$pass"
    }
    if($utype -ne ""){ 
        $cmd+=" --params.type=$utype"
    }
    if($url -ne ""){ 
        $cmd+=" --params.url=$url"
    }
 
    if($etype -ne ""){ 
        $cmd+=" --params.type=$etype"
    }
    
    $cmd
    Invoke-Expression -Command:$cmd

is there a way to convert above powershell script to bash script? thank you so much, much appreciated

I have done something like this but I am unable to build command dynamically and invoke it at the end like in powershell, my question is can I build a command dynamically if an argument is not passed, I don't want that to be included in the command, for example if I don't pass '-p xxx' then I don't want '--params.pass=$password' show up in the command in bash.

 #!/bin/bash
    
    # how to use 
    #  ./protactor-batch.sh -e xxx -u xxx -p xxx
    while getopts u:p:e:s: flag
    do
        case "${flag}" in
            u) username=${OPTARG};;
            p) password=${OPTARG};;
            e) pod=${OPTARG};;
            s) sec=${OPTARG};;
        esac
    done
protractor $f --params.pod=$pod --params.user=$username --params.pass=$password
shoes_on
  • 1
  • 1
  • You're aware that PowerShell Core supports Linux and Mac, right? You can run your ps1 scripts without conversion? – Todd Apr 20 '22 at 01:22
  • 1
    Transform your last line with `echo "protactor $f ..."` to ensure your command is ok. Otherwise, what is the question? What happens with your script? Have you tried with `#!/bin/bash -x` to debug it? – Nic3500 Apr 20 '22 at 01:29
  • 1
    A shell parameter expansion trick that solves this is `protractor .... ${pswd:+--password=${pswd}} ....`. How ever, this requires that the `pswd` variable has NO value (and has not been set to null). So some other place in the code where someone puts in `pswd=xyz` could trip you up later. A great short cut, but not great for maintainance except by experienced shell progrrammers. This feature goes back to the borne shell (or at least the original ksh) and all of their descendants. Not in `[t]csh`. Good luck. – shellter Apr 20 '22 at 04:00
  • 1
    @shellter I'd recommend double-quoting the variable in case it contains weird characters (especially common with passwords): `${pswd:+--password="${pswd}"}`. BTW, another option is to build the arg list in an array; see my answer to [this question](https://stackoverflow.com/questions/64956669/pass-conditional-arguments-to-curl-in-bash/64959731). – Gordon Davisson Apr 20 '22 at 04:31
  • @GordonDavisson : Yes! Excellent advice. Especially for modern passwords, almost certain to have some char that will trip up the shell parsing. Thanks! – shellter Apr 24 '22 at 15:01

1 Answers1

0

this seems to work for me

`

#!/bin/bash
    
    # how to use 
    #  ./protactor-batch.sh -e xxx -u xxx -p xxx
    while getopts u:p:e:s: flag
    do
        case "${flag}" in
            u) username=${OPTARG};;
            p) password=${OPTARG};;
            e) pod=${OPTARG};;
            s) sec=${OPTARG};;
        esac
    done

    cmd1="protractor " ;

  if [ ! -z "$username" ]
  then
    cmd1+=" --params.username=$username";
  fi
    
    $cmd1

`

shoes_on
  • 1
  • 1
  • This will fail if any of the parameters contain whitespace, or possibly some other funny characters (e.g. the password). To build commands like this *safely*, you need to use an array rather than a plain string. – Gordon Davisson Apr 24 '22 at 17:15