Scripting languages comparison
Comparing Scripting Languages
Bash | PowerShell | Ruby | Python | |
---|---|---|---|---|
Comments | # | # or <# #> | # or =begin =end | # |
Variables - assign | varName=value | $varName=value | varName=value | varName=value |
Variables - display | echo $varName | Write-Host $varName | puts varName | print(varName) |
Substitudion - environment variables | $envVarName | Get-item Env:varName | ENV['varName'] | Os.environ['varName'] |
String length | ${#string} | (string).Length | string.length | len(string) |
String - substring | ${string:position} | (string).Substring(start,end) | string[1..3] | string[start:end+1] |
String - replace substring | ${string/substring/replacement} | (string).Replace.(substr,replStr) | string[1..3] = replStr | string.replace(old, new,count) |
AND/OR | -a / -o | -and, -or, -not ! | and &&, or ||, not ! | and, or, not |
Comparisons | -eq(==), -ne(!=), -lt(<),-le(<=),-gt(>),-ge(>=) | -eq,-ne,-gt,-ge,-lt,-le | ==,!=,>,>=,<,<= | ==,!=(<>),>,>=,<,<= |
Looping | For | For,While,Do-While,Do-Until | while, until, for | for, while |
Flow control | if condition then commands elif commands else commands fi |
if (condition){ statements }elseif(condition){ statements }else{ statements } |
if condition then statements elsif statements else statements end |
if condition: statements elif condition: statements else: statements |
Input - file | Input="filename" While IFS=read -r f1 f2 f3 |
(lines = Get-Content filename<br/>Out-File -FilePath filename -InputObject<br/>)lines - Encoding ASCII | inFile = File.new("filename","r") inFile.each_line{|line|puts"#{line.dump}"} inFile.close |
f = open('inFile.txt','r') for line in f: do something here f.close() |
Input - terminal | Read -p "Prompt:" var | $firstName = Read-Host -Prompt'Enter first name' | name = gets | name = raw_input('Please enter your name') |
Input - network | While read -r inline < dev/ttyS1 | (socket = new-object<br/>System.Net.Socket.TcpClient()ip,(port)<br/>if()socket.Connected){} | client = TCPSocket.open('hostname','port') Client.send("string",o) |
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) If sock.connect_ex((remoteServerIP, port) )==0: print('Port {}: is Open'.format(port)) |
Error handling | If["$?" = "0"] then | try{ Command } catch{ errHandling commands } |
begin statements rescue statements if error occurred else statements if no error end |
try: statements raise customErrorObject except errorObject: statements except customErrorObject: statements finally: statements to clean up |
Arrays | bashArray = (val1, val2, val3) For i in 123 Do echo ({bashArray[)i]} done |
(PSarray=@(1.3.5.7.9);<br/><br/>for ()i=0;$i -lt (PSarray.Length;)i++){ (PSarray[)i] } foreach($element in $PSarray) { $element } |
rubyArray = ["val1","val2","val3"] print rubyArray[1] print rubyArray.index("val2") |
pythonArray = [10,20,30,40,50] Print(pythonArray[1]) len(pythonArray) |
Encoding | Echo plainText | base64 | (Text = 'Hello world'<br/><br/>)Bytes = [System.Text.Encoding]::Unicode.GetByteps((Text)<br/><br/>)EncodedText = [Convert]::ToBase64String.($Bytes) | Require "base64" encString = Base64.encode64('Hello World!') |
Import base64 encString = base64.encodestring('Hello World!') |
Decoding | Echo encString | base64 --decode | (EncodedText = 'encodeString'<br/><br/>)DecodedText = [System.Tet.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($EncodedText)) | plaintext = Base64.decode(enc) | plaintext = base64.decodestring(encString) |
QUICK REVIEW
- Recognize unique Bash script syntax - output(echo), error-handling("$?")
- Recognize unique PowerShell script syntax - output (Write-Host), flow-control(elseif),error-handling(try/catch)
- Recognize unique Ruby syntax - output(puts), flow-control(elsif),error-handling(rescue)
- Recognize unique Python syntax - output(print), error-handling(try/except/finally)