Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Darkly)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Uncategorized
  3. I feel like I'm getting better at shell fu.

I feel like I'm getting better at shell fu.

Scheduled Pinned Locked Moved Uncategorized
18 Posts 8 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Graham Sutherland / PolynomialG This user is from outside of this forum
    Graham Sutherland / PolynomialG This user is from outside of this forum
    Graham Sutherland / Polynomial
    wrote last edited by
    #1

    I feel like I'm getting better at shell fu. I'm not necessarily "optimal" at it, but I'm figuring stuff out.

    last night's zsh one-liner, put together on my phone no less:

    find -s /dev -name 'da*' -exec smartctl -a {} \; | grep Power_On_Hours | cut -w -f 11 | xargs -n 1 -I {} sh -c 'echo $(( ({}-({}%(24*365))) / (24*365) ))y $(( ( {} - ({}-({}%(24*365))) ) / 24 ))d' \;

    it shows me how many power-on hours my disks have based on SMART data, converted into years and days.

    Graham Sutherland / PolynomialG Aaron Sawdey, Ph.D.A classabbyampC 3 Replies Last reply
    0
    • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

      I feel like I'm getting better at shell fu. I'm not necessarily "optimal" at it, but I'm figuring stuff out.

      last night's zsh one-liner, put together on my phone no less:

      find -s /dev -name 'da*' -exec smartctl -a {} \; | grep Power_On_Hours | cut -w -f 11 | xargs -n 1 -I {} sh -c 'echo $(( ({}-({}%(24*365))) / (24*365) ))y $(( ( {} - ({}-({}%(24*365))) ) / 24 ))d' \;

      it shows me how many power-on hours my disks have based on SMART data, converted into years and days.

      Graham Sutherland / PolynomialG This user is from outside of this forum
      Graham Sutherland / PolynomialG This user is from outside of this forum
      Graham Sutherland / Polynomial
      wrote last edited by
      #2

      shells / coreutils could really use a built in way to parse and convert timespans, though. there's some limited ability within gnu versions of it, but it's kinda awkward and I can't use them anyway 'cos I'm on BSD.

      Graham Sutherland / PolynomialG 1 Reply Last reply
      0
      • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

        I feel like I'm getting better at shell fu. I'm not necessarily "optimal" at it, but I'm figuring stuff out.

        last night's zsh one-liner, put together on my phone no less:

        find -s /dev -name 'da*' -exec smartctl -a {} \; | grep Power_On_Hours | cut -w -f 11 | xargs -n 1 -I {} sh -c 'echo $(( ({}-({}%(24*365))) / (24*365) ))y $(( ( {} - ({}-({}%(24*365))) ) / 24 ))d' \;

        it shows me how many power-on hours my disks have based on SMART data, converted into years and days.

        Aaron Sawdey, Ph.D.A This user is from outside of this forum
        Aaron Sawdey, Ph.D.A This user is from outside of this forum
        Aaron Sawdey, Ph.D.
        wrote last edited by
        #3

        @gsuberland one-liners like that are pretty excellent. I tend use awk for stuff like that sh command at the end, I think it doesn't require quite so many parens and you also probably don't need the cut because you can just use $11 in awk. Plus you can do summary statistics and print out in END{ }.

        Graham Sutherland / PolynomialG 1 Reply Last reply
        0
        • Aaron Sawdey, Ph.D.A Aaron Sawdey, Ph.D.

          @gsuberland one-liners like that are pretty excellent. I tend use awk for stuff like that sh command at the end, I think it doesn't require quite so many parens and you also probably don't need the cut because you can just use $11 in awk. Plus you can do summary statistics and print out in END{ }.

          Graham Sutherland / PolynomialG This user is from outside of this forum
          Graham Sutherland / PolynomialG This user is from outside of this forum
          Graham Sutherland / Polynomial
          wrote last edited by
          #4

          @acsawdey I've tried to use awk a few times but I find the syntax non-obvious (in a similar way to perl) so it never sticks

          honestly `python -c` would probably be easier for me to remember lol

          🇺🇦 haxadecimal 🚫👑B 1 Reply Last reply
          0
          • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

            shells / coreutils could really use a built in way to parse and convert timespans, though. there's some limited ability within gnu versions of it, but it's kinda awkward and I can't use them anyway 'cos I'm on BSD.

            Graham Sutherland / PolynomialG This user is from outside of this forum
            Graham Sutherland / PolynomialG This user is from outside of this forum
            Graham Sutherland / Polynomial
            wrote last edited by
            #5

            the main annoyance I have with shell stuff is how the command names defy discoverability.

            "I would like to flush my current shell history to the history file"

            "ok run fc -W"

            "huh. what does fc stand for?"

            "fix command"

            "... ok"

            Alastair McBain :unverified:A Graham Sutherland / PolynomialG Eli the BeardedE 3 Replies Last reply
            1
            0
            • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

              the main annoyance I have with shell stuff is how the command names defy discoverability.

              "I would like to flush my current shell history to the history file"

              "ok run fc -W"

              "huh. what does fc stand for?"

              "fix command"

              "... ok"

              Alastair McBain :unverified:A This user is from outside of this forum
              Alastair McBain :unverified:A This user is from outside of this forum
              Alastair McBain :unverified:
              wrote last edited by
              #6

              @gsuberland but sometimes they lead to fun stuff:

              `git push -fu [...]`

              😁

              1 Reply Last reply
              0
              • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

                the main annoyance I have with shell stuff is how the command names defy discoverability.

                "I would like to flush my current shell history to the history file"

                "ok run fc -W"

                "huh. what does fc stand for?"

                "fix command"

                "... ok"

                Graham Sutherland / PolynomialG This user is from outside of this forum
                Graham Sutherland / PolynomialG This user is from outside of this forum
                Graham Sutherland / Polynomial
                wrote last edited by
                #7

                I also hate that shell builtins don't have manpages. I have looked up the justification for this and I find it deeply unsatisfying.

                classabbyampC Marcus MüllerF F3715HF 3 Replies Last reply
                0
                • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

                  I feel like I'm getting better at shell fu. I'm not necessarily "optimal" at it, but I'm figuring stuff out.

                  last night's zsh one-liner, put together on my phone no less:

                  find -s /dev -name 'da*' -exec smartctl -a {} \; | grep Power_On_Hours | cut -w -f 11 | xargs -n 1 -I {} sh -c 'echo $(( ({}-({}%(24*365))) / (24*365) ))y $(( ( {} - ({}-({}%(24*365))) ) / 24 ))d' \;

                  it shows me how many power-on hours my disks have based on SMART data, converted into years and days.

                  classabbyampC This user is from outside of this forum
                  classabbyampC This user is from outside of this forum
                  classabbyamp
                  wrote last edited by
                  #8

                  @gsuberland smartctl has a json output option, which would mean jq could give something maybe more readable, but the format it emits is kinda annoying... it's like tables as json

                  Graham Sutherland / PolynomialG 1 Reply Last reply
                  0
                  • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

                    I also hate that shell builtins don't have manpages. I have looked up the justification for this and I find it deeply unsatisfying.

                    classabbyampC This user is from outside of this forum
                    classabbyampC This user is from outside of this forum
                    classabbyamp
                    wrote last edited by
                    #9

                    @gsuberland in zsh, it's all one page, zshbuiltins(1)

                    Graham Sutherland / PolynomialG 1 Reply Last reply
                    0
                    • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

                      I also hate that shell builtins don't have manpages. I have looked up the justification for this and I find it deeply unsatisfying.

                      Marcus MüllerF This user is from outside of this forum
                      Marcus MüllerF This user is from outside of this forum
                      Marcus Müller
                      wrote last edited by
                      #10

                      @gsuberland not on their own; bash builtins share one giant manpage you never want when you get it, and never when you want it. man test.
                      zsh has `man zshbuiltins`.

                      Graham Sutherland / PolynomialG 1 Reply Last reply
                      0
                      • classabbyampC classabbyamp

                        @gsuberland smartctl has a json output option, which would mean jq could give something maybe more readable, but the format it emits is kinda annoying... it's like tables as json

                        Graham Sutherland / PolynomialG This user is from outside of this forum
                        Graham Sutherland / PolynomialG This user is from outside of this forum
                        Graham Sutherland / Polynomial
                        wrote last edited by
                        #11

                        @classabbyamp yeah you could do

                        smartctl -a -j /dev/da0 | jq '.ata_smart_attributes.table[] | select(.name=="Power_On_Hours") | .raw.value'

                        but the grep and cut was shorter here

                        1 Reply Last reply
                        0
                        • classabbyampC classabbyamp

                          @gsuberland in zsh, it's all one page, zshbuiltins(1)

                          Graham Sutherland / PolynomialG This user is from outside of this forum
                          Graham Sutherland / PolynomialG This user is from outside of this forum
                          Graham Sutherland / Polynomial
                          wrote last edited by
                          #12

                          @classabbyamp what a mess

                          1 Reply Last reply
                          0
                          • Marcus MüllerF Marcus Müller

                            @gsuberland not on their own; bash builtins share one giant manpage you never want when you get it, and never when you want it. man test.
                            zsh has `man zshbuiltins`.

                            Graham Sutherland / PolynomialG This user is from outside of this forum
                            Graham Sutherland / PolynomialG This user is from outside of this forum
                            Graham Sutherland / Polynomial
                            wrote last edited by
                            #13

                            @funkylab which then makes it impossible to search >_<

                            Marcus MüllerF 1 Reply Last reply
                            0
                            • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

                              @funkylab which then makes it impossible to search >_<

                              Marcus MüllerF This user is from outside of this forum
                              Marcus MüllerF This user is from outside of this forum
                              Marcus Müller
                              wrote last edited by
                              #14

                              @gsuberland yep, exaxtly my gripe. `man zsh-for` or even `man zsh for` would be possible on modern `man`s

                              1 Reply Last reply
                              0
                              • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

                                @acsawdey I've tried to use awk a few times but I find the syntax non-obvious (in a similar way to perl) so it never sticks

                                honestly `python -c` would probably be easier for me to remember lol

                                🇺🇦 haxadecimal 🚫👑B This user is from outside of this forum
                                🇺🇦 haxadecimal 🚫👑B This user is from outside of this forum
                                🇺🇦 haxadecimal 🚫👑
                                wrote last edited by
                                #15

                                @acsawdey @gsuberland
                                Awk is a bit awkward, but it's a piece of cake compared to Perl. But I, too, prefer Python.

                                Aaron Sawdey, Ph.D.A 1 Reply Last reply
                                0
                                • 🇺🇦 haxadecimal 🚫👑B 🇺🇦 haxadecimal 🚫👑

                                  @acsawdey @gsuberland
                                  Awk is a bit awkward, but it's a piece of cake compared to Perl. But I, too, prefer Python.

                                  Aaron Sawdey, Ph.D.A This user is from outside of this forum
                                  Aaron Sawdey, Ph.D.A This user is from outside of this forum
                                  Aaron Sawdey, Ph.D.
                                  wrote last edited by
                                  #16

                                  @brouhaha @gsuberland well, yes for anything other than 1-liners I too prefer python. But awk somehow is very amenable to doing tiny things like this:

                                  awk '{count[$0]++} END{for(x in count) { printf("%-10d %s\n",count[x],x) } } ' | sort -n

                                  1 Reply Last reply
                                  0
                                  • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

                                    I also hate that shell builtins don't have manpages. I have looked up the justification for this and I find it deeply unsatisfying.

                                    F3715HF This user is from outside of this forum
                                    F3715HF This user is from outside of this forum
                                    F3715H
                                    wrote last edited by
                                    #17

                                    @gsuberland consider toybox?

                                    1 Reply Last reply
                                    0
                                    • Graham Sutherland / PolynomialG Graham Sutherland / Polynomial

                                      the main annoyance I have with shell stuff is how the command names defy discoverability.

                                      "I would like to flush my current shell history to the history file"

                                      "ok run fc -W"

                                      "huh. what does fc stand for?"

                                      "fix command"

                                      "... ok"

                                      Eli the BeardedE This user is from outside of this forum
                                      Eli the BeardedE This user is from outside of this forum
                                      Eli the Bearded
                                      wrote last edited by
                                      #18

                                      @gsuberland

                                      I use fc (through aliases) all the time and I have never heard a good explanation of the name.

                                      100% agreed discoverability is a huge problem in shell commands, built-in or not. I wrote a tool, "nums",then literally years later found out that "seq" does almost the same thing. Mine is only better in being smart about zero padding: "nums 01-10" pads, "nums 1-10" doesn't

                                      1 Reply Last reply
                                      0
                                      • R ActivityRelay shared this topic
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      Powered by NodeBB Contributors
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups