Scripting
- Any scripting is sequence of instructions.
- In Linux instruction is a command & in Powershell the instruction is also command but called as Cmd-Let
- Scripts are created for two possible ways of execution
- interactive scripts:
- to simplify the execution of complex problems/installations/configurations
- non-interactive scripts:
- to automate certain activity.
- interactive scripts:
- Lets try to setup dev environment for shell and powershell scripts.
Developer Environment
- Windows
- Ensure Visual studio code is installed
- Ensure Git Bash is installed
-
Mac:
- Ensure Visual Studio code is installed.
- Shell script has an extension of .sh
- Powershell scripts have an extension of .ps1
- Ensure Powershell extension is installed.
- Ensure bashdebug is installed
- To run the scripts:
- ps1: run them from powershell
- cd in to the folder
- execute
.\<filename>.ps1
- sh: run the gitbash or mac terminal
- cd in to the folder
- execute
bash ./<filename>.sh
- ps1: run them from powershell
Debugging
- IDE based Debugging:
- Breakpoint: The line or instruction where the control is handed over to us.
- Step Over: Execute the current instruction or line.
- Powershell: Debugging is possible by installing Powershell extension
- Shell Scripting: We need to do little more work to get the things in place.
Exit Codes in Linux
- Every Linux command returns a code
- Zero: Command exectuion is success
- Non-Zero: Command execution is failed
- To find the last return code use the following expression
$?
- Refer Here for the bash cheatsheet.
Combining multiple Linux commands in one line
- Commands can be combined by using semi-colon
;
and ampersand&&
;
: All the commands will be executed irrespective of errors
&&
: All the commands will be executed if there are no errors. From the moment a command returns an error code the subsequent commands will not be executed
Note
- PS script
echo hello
echo 1
echo 2
- shell script
echo hello
echo 1
echo 2
- python script
def printargs(a,b):
print(f"values passed are {a}, {b}")
def add(a, b):
printargs(a,b)
return a+b
if __name__ == '__main__':
result = add(3, 5)
print(result)
result = add(5, 8)
print(result)