четвъртък, 12 март 2020 г.

A pinch of bash

    The word "bash" may sound funnny in some languages (and especially in Bulgarian it is quite often used in the meaning of "exactly") but in the computer world it stands for "Bourne again shell". This is in fact the scripting language that contains all commands used in a Linux shell. Each command can be written separately in the console or multiple/a lot of commands can be collected in
one file which can be executed when needed, this is providing huuuuuuge potential for automating wide variety of tasks. As mentioned in this article I use bash scripting to automate my work in Python and now I will provide a simple example of how it works. Below is the full script I use at my FormBuilder project:


  1. #!/bin/bash
  2. echo "********** 1) Run logic ***********"
  3. echo "********** 2) Run Flask ***********"
  4. echo "********** 3) Run tests ***********"
  5. read choice
  6. if [ $choice -eq 1 ]
  7.     then 
  8.   python main.py
  9. elif [ $choice -eq 2 ]
  10.     then
  11.         python web.py
  12. elif [ $choice -eq 3 ]
  13.     then
  14.         python tests.py
  15. else
  16.     echo "***incorrect selection***"
  17. fi

    The first line of code is not completely necessary, it tells the system you will run a bash script. If you don't specify it the Unix/Linux command line will try to use bash by default, and although not mandatory it is a sign of good scripting habits.
    Lines 2-4 are just printing 3 text messages, echo " ... "  acts similarly to print( ) in Python. On these lines we initiate a menu that allows us to chose between 3 options:
  1. run the backend logic of the FormBuilder project
  2. run the flask project that is aiming to display the form as an html page
  3. run some additional Python jobs for testing purposes
    After that we have read choice which acts like =input( ) in Python - collects user input. On line 7 starts the logical core of the script - here we tell the shell what to do in each predefined case. It is in fact simple if - elif - else construct and each condition should be typed in square brackets - [ $choice -eq 1 ]. I'm pretty sure you've noticed the dollar sign before the name of the variable, that's how we refer to a variable once it is defined in the script. -eq 1 stands for pythonic "==1" BUT be careful - you should have one interval between [ and the beginning of condition and one interval also between end of condition and ], otherwise the terminal will spit out "command not found" message and nothng will happen. Next we need the keyword "then" and finally we reach the command for running our Python script. With elif we move to the next case we need, the syntax is analogous so I believe it is not necessary to explain it in detail.
    It is important when starting logical sequence with if to close it with fi at the end of it. And that's all, now we have fully working bash script automating 3 simple tasks :)
    IMPORTANT: It is really important to adjust line ending of your script file to Unix, otherwise the script won't run properly. On Notepad++ it is done via Edit=>EOL Conversion=>Unix (LF).
    And voila, we have fully working script now. Save it in your working directory as "[file_name].bash" and type in your Unix/Linux terminal "cd [path/to/your/working/directory]". Once you reach this directory you will need to run the script file, this is done via "sh [file_name].bash" command typed in the terminal.



Няма коментари:

Публикуване на коментар