API shell scripts programming language examples
Why use shell scripts?
Shell scripts are great tools for rapid development and validation for simple (smaller) requirements. Most development is done iteratively, and shell scripts provide immediate feedback on logic and code.
Company-supported programming languages are the preferred tools for enterprise applications.
Most likely, your company employs more Java and/or PHP programmers than Linux Shell script programmers.
The "use cases" will be programmed using either Unix/Linux Shell and/or Windows PowerShell scripts. You can easily port the logic from these scripts into your favorite programming language. Basic examples of connection with API will be provided for a number of major programming languages in a later section of this document.
Linux/Unix/(and Mac too) shell scripts
There are numerous shell environments, including sh, bash, csh, ksh, and tsh. Identify the current shell environment using any of the commands below:
|
The examples provided have all been run from the bash shell environment and may or may not run the same as the other shells. We recommend that you start a bash shell by typing bash at the operating system prompt, like this:
|
The scripts included within this document have all been run on Linux and Mac environments within a bash shell and are NOT certified by any means. As always, test and verify in development for your environment.
For non-Linux platforms and non-bash shell environments, please re-validate for your configuration and search the web for any alternative methods/tools/utilities that may perform the same actions.
Windows PowerShell
Powershell Open Source is now available for Linux and Mac OS. for more information refer to Microsoft Open Source
Requirements
Windows has a number of versions of Powershell. The minimum version for Delphix is 2.0 for SQL Server 2008 environments. There are numerous enhancements and features with subsequent Powershell versions. Additionally, you must be aware of the architecture of 32bit or 64bit Powershell versions you are running from within.
|
32bit or 64 bit
If executing Powershell scripts from within Delphix Pre/Post Scripts commands or Delphix hooks, the default Powershell used is 32 bit, whereas the typical default Windows Powershell is 64 bit. However, Powershell allows you to execute 64 bit Powershell command from within the 32 bit environment. Shown below is a simple alias, ps64, to execute 64bit Powershell scripts.
|
Sample call to execute 64bit Powershell script
|
Courtesy of this article: http://www.gregorystrike.com/2011/01/27/how-to-tell-if-powershell-is-32-bit-or-64-bit/
|
. . . or . . .
|
It is worth noting that the locations of the 32-bit and 64-bit versions of Powershell are somewhat misleading. The 32-bit PowerShell is found at C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
and the 64-bit PowerShell is at C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Execution of scripts security disabled
It is possible to disable Powershell environments on the system. If they are disabled, you will see the following error for any Powershell script that you try to execute.
|
To enable Powershell scripts to be executed, set the execution policy to Yes.
|
Now your Powershell scripts will be executed.
curl.exe
Not all Windows platforms have the cURL executable installed, the easiest way to install and use cURL on Windows is as follows:
Download the package from https://curl.se/windows/ and unzip.
In the "bin" folder find "curl.exe" and move it to "C:\Windows\System32".
Then you will be able to use the curl command from the Windows Command Prompt or PowerShell console.
|
Invoking the curl or curl.exe from Powershell command line.
|
If the alias curl name is to the Invoke-WebRequest, you will need to use the curl.exe command explicitly or remove the alias.
|
Verify that curl and/or curl.exe work from the respective Powershell environment:
|