Virtualization Plugins

 View Only
  • 1.  dynamically passing parameters to bash scripts

    Posted 07-30-2020 02:11:00 PM
    I want to know how to pass parameters to a shell script file.
    for ex:
    My shell script(echoscript.sh) contains
    echo $1

    and i want to pass $1 dynamically  to the following code how do i do it.

    script_content = pkgutil.get_data('resources', 'echoscript.sh')
    
        # Execute script on remote host
        response = libs.run_bash(direct_source.connection, script_content)


    ------------------------------
    Ravi Nistala
    Community Member
    TDGlobal
    ------------------------------


  • 2.  RE: dynamically passing parameters to bash scripts

    Posted 07-30-2020 03:08:00 PM
    Hey Ravi,

    Unfortunately you cannot pass in variables via stdin. The only way to pass in variables to scripts is by using environment variables. Here is an example:
    https://developer.delphix.com/References/Platform_Libraries/#examples

    Would it be possible for you to modify the shell script to use environment variables instead?

    The Virtualization SDK is also open source on GitHub: https://github.com/delphix/virtualization-sdk/. If you find any bugs, or have feature requests, please feel free to open a new issue and the engineering team will get back to you.

    -Ankur

    ------------------------------
    Ankur Sarin
    Senior Engineering Manager
    Delphix
    ------------------------------



  • 3.  RE: dynamically passing parameters to bash scripts

    Posted 07-30-2020 11:15:00 PM

    Thank you Ankur for your response. I am not able to understand the example provided at https://developer.delphix.com/References/Platform_Libraries/#examples.

    They are misleading. If you see the example below.

    from dlpx.virtualization import libs

     

    command = "echo 'Hi' >> /tmp/debug.log"

    variables = {"var": "val"}

     

    response = libs.run_bash(connection, command, variables)

     

    print response.exit_code

    print response.stdout

    print response.stderr

     

    The command contains no "var" as a parameter in the command. To me there is no point in passing 'variables" in this example.

     

     

    Can you tell me how to use environment variables to pass values to a shell script in a directory? I can call the shell script without any variables which is inside a directory.

     

     

     

    Thanks & Regards

    Ravi Nistala

    +919949999282 (m)

     






  • 4.  RE: dynamically passing parameters to bash scripts
    Best Answer

    Posted 07-30-2020 11:51:00 PM
    Edited by Michael Torok 07-31-2020 12:03:46 AM

    The example above represents a way to pass in variables, we will fix the command to show how to access the variables.
    Here is another example, hope this helps:

    Assume the "echoscript.sh" script is:

    echo "$MY_VARIABLE" > /var/tmp/my_file​


    This can be called to create a file called "/var/tmp/my_file" with the content as "my_value" using the plugin code as following:

    script_content = pkgutil.get_data('resources', 'echoscript.sh')
    variables = {"MY_VARIABLE": "my_value"}
    
    # Execute script on remote host
    response = libs.run_bash(direct_source.connection, script_content, variables)


    ------------------------------
    Ankur Sarin
    Senior Engineering Manager
    Delphix
    ------------------------------



  • 5.  RE: dynamically passing parameters to bash scripts

    Posted 07-31-2020 12:15:00 AM

    Thank you Ankur. The example you provided below makes sense. Hope you will update the online example to reflect the same.

     

    So let us say my script (source_profile.sh) looks like as below and is stored in a folder called 'resources':

     

    source $PATH_TO_PROFILE

     

    can I use the following code, shown below, to pass the  value to 'PATH_TO_PROFILE' dynamically

     

    script_content = pkgutil.get_data('resources', 'source_profile.sh')

    variables = {" PATH_TO_PROFILE ": "/home/username/.username.bash"}

    # Execute script on remote host

    response = libs.run_bash(direct_source.connection, script_content, variables)

     

    I have additional question :

    Can I change "/home/username/.username.bash"  value based on a python variable dynamically?

     

     

     

     

    Thanks & Regards

    Ravi Nistala

    +919949999282 (m)

     






  • 6.  RE: dynamically passing parameters to bash scripts

    Posted 07-31-2020 12:27:00 AM
    yes, whenever you call

    response = libs.run_bash(direct_source.connection, script_content, variables)

    the script will get whatever the value of the "variables" array is at that point in time, the value will be dynamically resolved at invocation time, it will not be statically allocated.

    for example:

    # This will run the script with the value as "my_value1"
    variables = ["MY_VARIABLE": "my_value1"]
    response1 = libs.run_bash(direct_source.connection, script_content, variables)
    
    
    # This will run the script with the value as "my_value2"
    variables = ["MY_VARIABLE": "my_value2"]
    response2 = libs.run_bash(direct_source.connection, script_content, variables)


    ------------------------------
    Ankur Sarin
    Senior Engineering Manager
    Delphix
    ------------------------------



  • 7.  RE: dynamically passing parameters to bash scripts

    Posted 07-31-2020 12:50:00 AM

    Thank you Ankur for your valuable help. This helps me in my development. I will test it and will let you know if I encounter any issues.

     

    Thanks & Regards

    Ravi Nistala

    +919949999282 (m)