-2

I am new to scripting. I would like to execute a script like below. However I am getting error. Kindly help me to fix this.

Script File Name : TestScript.sh Execution : ./TestScript.sh "A"

#!/bin/bash


COStesting("Test")
{
    if [ $1 = "A" ]
    then
        Name = "Tiger"
        Gender = "Male"

    elif [ "$1" = "B" ]
     then
        Name = "Lion"
        Gender = "Male"
    fi
}
    pass=`python3 - <<END
    import subprocess
      print(Name, Gender)
    END
    ```
  • 2
    What is the error? And what is the purpose of this script? I mean what do you expect it to do? – Code-Apprentice Nov 07 '19 at 23:27
  • 2
    You cannot access shell variables created in a shell script inside Python as Python variables. One way is to to `export` shell variables into the environment, so they become environment variables, and then use the [environment access mechanism in Python](https://stackoverflow.com/questions/4906977/how-to-access-environment-variable-values) to retrieve the values. – Kaz Nov 07 '19 at 23:31
  • 1
    `COStesting("Test")` isn't valid shell function syntax; you can't have anything between the `(` `)` other than blanks. – Kaz Nov 07 '19 at 23:33
  • 1
    beware that syntax for assignment in bash is not `var = val` but `var=val`. This alone must be causing `command not found` for ya – Mika Feiler Nov 07 '19 at 23:41
  • I recommend [shellcheck.net](https://www.shellcheck.net) for pointing out common shell syntax mistakes. – Gordon Davisson Nov 07 '19 at 23:48
  • for indendation of here-strings you need to use `<<-END` and tabs not spaces – Mika Feiler Nov 08 '19 at 00:03

2 Answers2

0

This does what you want; incorporating some stuff that i and others mentioned in comments.

#!/bin/bash

COStesting()
{
    local Name
    local Gender
    if [ "$1" == "A" ]
    then
        Name="Tiger"
        Gender="Male"
    elif [ "$1" == "B" ]
    then
        Name="Lion"
        Gender="Male"
    fi
    pass=$(Name="$Name" Gender="$Gender" python3 - <<-END
        from os import environ
        print(environ['Name'], environ['Gender'])
    END
    )
    echo $pass
}
  • Please ensure that in what you copy from here the indentation is done with tab characters and not with spaces

What was done here:

  • comment: Shell script with Python

    You cannot access shell variables created in a shell script inside Python as Python variables. One way is to to export shell variables into the environment, so they become environment variables, and then use the environment access mechanism in Python to retrieve the values.

    What i did was exporting them for just the python command, instead of globally.

  • comment: Shell script with Python

    COStesting("Test") isn't valid shell function syntax; you can't have anything between the ( ) other than blanks.

  • comment: Shell script with Python

    beware that syntax for assignment in bash is not var = val but var=val. This alone must be causing command not found for ya

  • comment: Shell script with Python

    for indendation of here-strings you need to use <<-END and tabs not spaces

  • you used single = instead of double == for comparison operator in the [ command
  • pass was a local variable in function. to have it out, you need to echo it from the function onto standard output.

to use this function, you first source <filename.sh> (unless you just append commands to the file with this function) and then you can use it like $(COStesting "$AorB") where $AorB is some variable containing either 'A' or 'B'.

Mika Feiler
  • 474
  • 3
  • 17
  • @https://stackoverflow.com/users/68587/john-kugelman-supports-monica, your edit was wrong, here-string indentation only works with tabs and not with spaces. Also do we want to export? What scope does such export have? If sourced, won't it affect the sourcing shell globally? – Mika Feiler Nov 08 '19 at 00:06
-1
COStesting()
{
    if [ $1 == "A" ]
    then
      Name="Tiger"
    Gender="Male"

elif [ "$1" == "B" ]
 then
    Name="Lion"
    Gender="Male"
fi
}

COStesting  $1
pass=$(python3 <<END
import subprocess
print('$Name', '$Gender')
END
)