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:
- #!/bin/bash
- echo "********** 1) Run logic ***********"
- echo "********** 2) Run Flask ***********"
- echo "********** 3) Run tests ***********"
- read choice
- if [ $choice -eq 1 ]
- then
python main.py
- elif [ $choice -eq 2 ]
- then
- python web.py
- elif [ $choice -eq 3 ]
- then
- python tests.py
- else
- echo "***incorrect selection***"
- 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:
- run the backend logic of the FormBuilder project
- run the flask project that is aiming to display the form as an html page
- run some additional Python jobs for testing purposes
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.
Няма коментари:
Публикуване на коментар