Skip to main content
Skip table of contents

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:

ps -p $$
ps -p $$ -ocomm=
echo $0
ps -ef | grep $$ | grep -v grep
ps -ef | egrep "^\s*\d+\s+$$\s+"

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:

Operating_System_Prompt> bash

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.

PS> $PSVersionTable.PSVersion

Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1

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.

PS> set-alias ps64 "$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe"

Sample call to execute 64bit Powershell script

PS> ps64 [path\to\any_64bit_powershell_script].ps1

Courtesy of this article: http://www.gregorystrike.com/2011/01/27/how-to-tell-if-powershell-is-32-bit-or-64-bit/

PS> if ($env:Processor_Architecture -eq "x86") { write "running on 32bit" } else {write "running on 64bit"}
running on 32bit

. . . or . . .

PS> if ([System.IntPtr]::Size -eq 4) { "32-bit" } else { "64-bit" }
32-bit

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.

PS> . .
[any_powershell_script].ps1
File [any_powershell_script].ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about signing" for more details.
At line:1 char:2
+ . <<<< .\ [any_powershell_script].ps1
 + CategoryInfo : NotSpecified: ( [], PSSecurityException
 + FullyQualifiedErrorId : RuntimeException

To enable Powershell scripts to be executed, set the execution policy to Yes.

PS> set-executionpolicy remotesigned
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution policy?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
PS>

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:

  1. Download the package from https://curl.se/windows/ and unzip.

  2. 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.

PS> curl.exe --version
curl 7.49.1 (x86_64-w64-mingw32) libcurl/7.49.1 OpenSSL/1.0.2h zlib/1.2.8 libidn/1.32 libssh2/1.7.0 nghttp2/1.12.0 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smtp smtps telnet tftp
Features: IDN IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL libz TLS-SRP HTTP2 Metalink

Invoking the curl or curl.exe from Powershell command line.

PS> Get-Command curl

CommandType Name ModuleName
----------- ---- ----------
Alias curl -> Invoke-WebRequest


PS> Get-Command curl.exe


CommandType Name ModuleName
----------- ---- ----------
Application curl.exe

If the alias curl name is to the Invoke-WebRequest, you will need to use the curl.exe command explicitly or remove the alias.

PS> Remove-item alias:curl

Verify that curl and/or curl.exe work from the respective Powershell environment:

PS> curl.exe --version
curl 7.49.1 (x86_64-w64-mingw32) ...
 
PS> curl --version
curl 7.49.1 (x86_64-w64-mingw32) ...


JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.