New to automation/scripting, Switch to python?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Guhum

New to automation/scripting, Switch to python?

Post by Guhum »

Code: Select all

#!/bin/bash

# Shows enabled sockets, mounted devices, and wifi router(?) information



(echo "Sockets: 


"
sudo systemctl -l|grep .socket
echo "
______________________________

"

echo "Mounted: 


"
sudo fdisk -l 

echo "
______________________________

"
echo
echo "Enter name of your internet device: "
read userInt 

echo "Wifi: 


"
sudo ifconfig ${userInt}) | more

exit

I wanted spaces between the titles and outputs and the different outputs, I know I can put lines inbetween the command outputs with "echo ___" instead of spaces and I plan on doing it soon, so that was the best my fiddling (truly the best way I learn) could come up with while I was on a video series on Bash scripting/automation. Is there a cleaner way to add line breaks in there. Please (dont make me beg) be critical of all of it and critque it in a way I can understand.
Or should I hold my horses with the questions til I actually finish the series.

Also its a bit time consuming trying find resources for bash programming (is it called programming? or scripting?) and what the "if, fi" version of bash scripting is called instead of the shell commands. If I search youtube for "bash scripting tutorials" its 99% about the commands and not the language.
So my question is should I just switch to learning python since there are so many more resources, although what made bash so fun for me was the immediate changes and info I could see in real life on my sensually beautiful Mint 19 Cinnamon (I love you baby, and thank you to anyone involved in its development again linux really gives me hope in the future of technology and this distro makes it look like classy). And Id usually keep trying to find out what exactly i'm supposed to do with all the syntax i'm learning from the other languages I tried but bored me to sleep (python, javascript which i gotta kinda further on cause i needed website functionality, C sharp for computer science class which I dropped after the first day). Bash seems further off than the other languages I tried learned so its a little off-putting, reminds me when I was a kid and picked up a book on visual basic.

Or, or do my python scripts run just as easily on the terminal? Or even after writing them? No compiling? If yes is the answer the latter string of questions i'll be binging python this weekend

Edited: Added the lines between outputs
Edit2: User types in their internet device (im not good with terminology; device sounded right)
Edit3: Think I found the right series folks, waiting for if statements so I can do other things if the user doesnt know their internet device. Maybe possible to figure it out for them by checking packets and returning that device. Or maybe just give them a list of all possible connections. This is seriously fun
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
FreedomTruth
Level 4
Level 4
Posts: 443
Joined: Fri Sep 23, 2016 10:19 am

Re: New to automation/scripting, Switch to python?

Post by FreedomTruth »

Just a suggestion to make it a little more readable, I prefer using echo -e "blahblah\n\n" -- the \n will insert a newline for you. In fact you could even combine the command you're running along with the echo command. Such as:

Code: Select all

echo -e "Sockets:\n\n\n$(sudo systemctl -l|grep .socket)\n______________________________\n"
Obviously, it's up to you how you would like the code to look at the end... I believe writing a bash script is probably considered more "scripting" than "programming" but the lines may blur somewhat. Sorry, I don't python, but you can call a python script from the terminal as easily as a bash script.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: New to automation/scripting, Switch to python?

Post by xenopeek »

There's ways to improve on your script (require root at start, prompt for questions upfront or even go as far to automate the question away, and replace all echo's with a simple single here document) but it's good to have a working script and be able to iteratively improve it as you learn more about Bash.

Doing this script in Python wouldn't be as trivial as it is in Bash. While both Bash and Python are scripted programming languages (you don't need to compile a source file but can just run it as is), Bash is more suited for stringing together Linux commands (which you can also run yourself on the terminal). You can make simple user interfaces in Bash, you can even do multi threaded programming in Bash and a lot more. At some point—when you're programming more logic into your script and it's not just stringing together Linux commands as in this script—Python will be the more obvious choice. You can write full blown programs in Python that you can't in Bash. Like Software Manager and Update Manager on Linux Mint are written in Python.

The learning curve for Python will be higher than for Bash. The benefit of programming with Bash is that'll also pick up more skills with Linux commands.

You can also work through the Bash manual at https://www.gnu.org/software/bash/manual/ and/or read the Bash manpage on your system with man bash or online at https://manpages.ubuntu.com/manpages/bi ... ash.1.html. If you prefer a book I'd suggest "Linux Command Line and Shell Scripting Bible" would be a good read if you want to get into Bash programming and it covers various Linux commands that will be helpful.

The Bash guide at http://mywiki.wooledge.org/BashGuide and accompanying Bash FAQ at http://mywiki.wooledge.org/BashFAQ/ are good resources to further develop your Bash programming skills.
Image
Locked

Return to “Scripts & Bash”