DEV Community

Mike Lockhart
Mike Lockhart

Posted on

1 3

Share your most embarrassing shell pipeline

Sometimes airing your code smells can be cathartic, and motivate you to fix it.

Here's possibly the most embarrassing shell pipeline I currently have in my Bash dotfiles:

            \grep -Rn "alias ${1}=" ${DOTFILES}/source \
                | awk -F: '{print "⍺:\t" $3 "\t--> " $1 ":" $2}' \
                    | sed "s/${1}=//g"
Enter fullscreen mode Exit fullscreen mode

Yes, I'm combining all the filters, grep,awk, and sed to print out an information of where a shell alias is defined in my files. It's part of my describe function for handling aliases:

$ describe gdfs
⍺:      alias 'cpulimit -l 2 -p $(pgrep -f "crash_handler_token=")&'    --> /Users/mjl/.dotfiles/source/40_osx.sh:28
Enter fullscreen mode Exit fullscreen mode

I'm sure all of this can actually be done in awk alone (especially the sed part), but I haven't figured a way to make it so.

Top comments (5)

Collapse
 
glsolaria profile image
G.L Solaria • Edited

As for me and my embarrassing command lines, I did one recently:

lxc network list | grep bridge | cut -f2 -d"|" | while read ii
do
  lxc delete network $ii
done

I probably should have used

lxc network list | nawk -F"|" '/\|/ && /bridge/ {system("lxc delete network " $2)}'
Collapse
 
bananabrann profile image
Lee

Noob here. In your first block, what are those backslashes \ doing at the end of the lines?

Collapse
 
glsolaria profile image
G.L Solaria

It is just to allow the one line to be broken across two lines. It aids readability because you do not have to scroll horizontally.

Collapse
 
sinewalker profile image
Mike Lockhart

Yes, there's a lot of different use for the back slash here. They all mean "escape" in different flavours.

First escapes my grep alias and runs normal grep

Then there is the slosh immediately before the newline, makes the line break but escapes the meaning as end-of-command to bash

Then in the awk command there is \t which is an "escape code" for a TAB

Collapse
 
bananabrann profile image
Lee

Well he'll! Thanks!

Excuse me as I go rewrite all my shell files

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay