Do you write your own shell scripts?

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
mmphosis
Level 1
Level 1
Posts: 25
Joined: Sat Apr 11, 2020 11:22 pm

Do you write your own shell scripts?

Post by mmphosis »

I read distrowatch each week, and this week there was a qa, poll and comments about the command line scripting tasks.

I'll answer my own question: Yes, I write my own shell scripts. As one example, here is my tally alias that uses awk to sum a bunch numbers from stdin:

Code: Select all

alias tally='awk -F '\''[^-.0-9]'\'' '\''{ for (i = 1; i <= NF; i++) s += $i } END { print s }'\'''

I am curious what types of shell scripts you write?
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.
User avatar
ricardogroetaers
Level 6
Level 6
Posts: 1372
Joined: Sat Oct 27, 2018 3:06 am
Location: Rio de Janeiro, Brasil

Re: Do you write your own shell scripts?

Post by ricardogroetaers »

I don't know how to write scripts.
User avatar
Mick-Cork
Level 4
Level 4
Posts: 493
Joined: Sun Mar 23, 2014 10:10 pm
Location: West Cork & London

Re: Do you write your own shell scripts?

Post by Mick-Cork »

I generally don't have a need but I did take a script from the net and expanded it to manage the hosts file as follows:

- Download recommended hosts files from three different URL sources
- Remove duplicates
- Remove comments
- Verify integrity
- Backup current hosts file
- Update hosts file with new build list

I run it once a month from a desktop shortcut.

I've also started writing a supplementary script to add or remove individual entries if needed.

Between using uBlock in the browser and the hosts file (approx 60k entries) I have a fairly good ad free / minimal pop-up web experience.

Edit: If I ever learn how to write an app with a GUI I'd start by creating the above.
User avatar
karlchen
Level 23
Level 23
Posts: 18211
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: Do you write your own shell scripts?

Post by karlchen »

Question: Do I write my own shell scripts :?:
Answer: Yes, I do. I would not trust the stuff, which somebody else has put together. :wink:

So let me share one of the very basic scripts, which I have created: show_grub_menu.sh

Code: Select all

#!/bin/bash
#
# Programme: show_grub_menu.sh
# Function : display the menu and submenu entries from /boot/grub/grub.cfg
# Author   : Karl <camouflaged>
# Date     : 13-Sept-2020 22:22
#
grep entry /boot/grub/grub.cfg | awk -F"'" '{ print $2 }'
Very handy (at least for me). Output formatting could be nicer, but then it would no longer be a simple one-liner. :wink:
Image
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 771 days now.
Lifeline
tuxoneseven

Re: Do you write your own shell scripts?

Post by tuxoneseven »

Yes, whenever I need to do some tedious tasks I'll just write a script and do it the lazy way. If the task is a bit more advanced, I'll use Python scripts instead of shell scripts though. For very advanced programs, I'll just see if someone else has made it or SaaS because I don't have time next to my work to create huge programs.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Do you write your own shell scripts?

Post by Termy »

Yeah -- I write quick scripts and write and maintain complete programs in Bourne Shell, BASH, and Perl, as I've done for ~7 years. My computing world has been many magnitudes better since I got into programming. :) The majority of my stuff can be found on GitHub, if you're curious. My Bourne Shell and BASH stuff is here. My BASH configurations, like functions, aliases, and the regular stuff can be found here. There's a link in my signature through which you can see all my repositories.
I'm also Terminalforlife on GitHub.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Do you write your own shell scripts?

Post by Termy »

karlchen wrote: Fri Mar 12, 2021 3:26 pmI would not trust the stuff, which somebody else has put together. :wink:

Code: Select all

while IFS="'" read -a Line; do
    case $Line in
        menuentry\ ) printf '%s\n' "${Line[1]}" ;;
    esac
done < /boot/grub/grub.cfg
Unfortunately, although we share the same mindset, we're both losing out. For example, the above (although only showing the main menu entries, not those in submenus) is a much more efficient approach, but you won't use it, because it came from another source. :lol: Remember, you're already running other people's code.

I think it's better to trust only certain people, and/or to execute only code you understand, not to just execute any old bit of code from some random person.
I'm also Terminalforlife on GitHub.
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Do you write your own shell scripts?

Post by Flemur »

Termy wrote: Sun Mar 14, 2021 11:47 am For example, the above (although only showing the main menu entries, not those in submenus) is a much more efficient approach,
Your script and karlchen's returned different results on my grub.cfg file because I entries like

Code: Select all

menuentry " ------------------------------------------------------" { any_thing }
as dividers. His script returned a blank line on those, and yours ignored them, so the output of his script looks more like the actual menu, but yours showed only valid boot entries. Which one is right?
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
karlchen
Level 23
Level 23
Posts: 18211
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: Do you write your own shell scripts?

Post by karlchen »

Hi, Flemur.

I honour the comment at the top of the file /boot/grub/grub.cfg:

Code: Select all

#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#
The file is created by grub-mkconfig and "sudo update-grub" only (which in turn invokes the other command).
As a result my commandline fulfills what I expect it to do. Also the "Advanced ..." sub-menu entries are not indented on the screen, the output is sufficient for my purpose.
But, yes, non-standard grub.cfg files may make my 1-liner display unwanted output.
(I just put the commandline into a script, because I am a lazybones and do not feel like typing the commandline frequently.)

Karl
Image
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 771 days now.
Lifeline
User avatar
all41
Level 19
Level 19
Posts: 9520
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: Do you write your own shell scripts?

Post by all41 »

karlchen wrote: Fri Mar 12, 2021 3:26 pm Question: Do I write my own shell scripts :?:
Answer: Yes, I do. I would not trust the stuff, which somebody else has put together. :wink:

So let me share one of the very basic scripts, which I have created: show_grub_menu.sh

Code: Select all

#!/bin/bash
#
# Programme: show_grub_menu.sh
# Function : display the menu and submenu entries from /boot/grub/grub.cfg
# Author   : Karl <camouflaged>
# Date     : 13-Sept-2020 22:22
#
grep entry /boot/grub/grub.cfg | awk -F"'" '{ print $2 }'
Very handy (at least for me). Output formatting could be nicer, but then it would no longer be a simple one-liner. :wink:
I use the bash history search to recall those long scripts. By adding a ~/.input file with these contents:

Code: Select all

$include /etc/inputrc

# command history search
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char

# extended auto-completion with tab
set show-all-if-ambiguous on
set completion-ignore-case on


# colors
set colored-completion-prefix on
set colored-stats on

# misc
set blink-matching-paren on
set mark-symlinked-directories = on
Then I just type a few beginning letters of the command and press the up-arrow to scroll
through the previously used commands beginning with those letters.
I modify .bashrc to allow more commands and storage space for this, and I carry the
history forward from release to release.

Here is a script in my history that finds locations of grub.

Code: Select all

sudo fdisk -l 2>/dev/null | egrep "Disk /|/dev/" | sed "s#^/dev/#Part /dev/#" | awk '{print $2}' | sed 's/://' | xargs -n1 -IX sudo sh -c "hexdump -v -s 0x80 -n  2 -e '2/1 \"%x\" \"\\n\"' X | xargs -n1 -IY sh -c \"case  \"Y\" in '48b4') echo X: GRUB 2 v1.96 ;; 'aa75' | '5272') echo X: GRUB Legacy ;; '7c3c') echo X: GRUB 2 v1.97 or v1.98 ;; '020') echo X: GRUB 2 v1.99 ;; *) echo X: No GRUB Y ;; esac\""
I maybe use it once a year but I would not want to type it. With the history search I typed sudo fdisk and the second up arrow press entered that entire command.
Everything in life was difficult before it became easy.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Do you write your own shell scripts?

Post by Termy »

Flemur wrote: Sun Mar 14, 2021 2:14 pm Your script and karlchen's returned different results on my grub.cfg file because I entries like

Code: Select all

menuentry " ------------------------------------------------------" { any_thing }
as dividers. His script returned a blank line on those, and yours ignored them, so the output of his script looks more like the actual menu, but yours showed only valid boot entries. Which one is right?
That would be Karlchen then, because his script works, simply put. I have no such dividers in my file, so my approach looks good on my end, albeit without the submenus or things like the recovery stuff. I didn't have the original file for reference, unfortunately. :lol: Perhaps the format was changed in newer versions of Ubuntu, or perhaps some sort of customization tool was used. I suspected (but hoped not) something would go awry, but whether it works or not thankfully wasn't my point. :P
I'm also Terminalforlife on GitHub.
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Do you write your own shell scripts?

Post by Flemur »

Termy wrote: Sun Mar 14, 2021 9:34 pm Perhaps the format was changed in newer versions of Ubuntu,
I make my own grub.cfg file by hand, with a text editor, and, not being able to find a better way, made up that non-functioning 'menuentry' as way of putting a space/divider in the menu display.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Do you write your own shell scripts?

Post by Termy »

Ah, that explains a lot. :roll: Interesting idea. I wonder if you could use Unicode to make it look even nicer, instead of hyphens.
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”