Understanding The `mapfile` builtin in Bash - You Suck at Programming

Поделиться
HTML-код
  • Опубликовано: 19 янв 2025

Комментарии • 19

  • @rogo7330
    @rogo7330 Месяц назад

    When I scrape site for new wallpapers or music through a network tab, I usually save a link and the name of the file on a separate lines. Previously I did two separate 'read -r', one in the while condition and the second one in the loop itself. With 'mapfile' I just creating a function to call back to on each 2-nd line. 'mapfile' provides an index and the line on which callback was called as the arguments to the function, which is very usefull in this case.

  • @deanbell5164
    @deanbell5164 Месяц назад +2

    Excellent

  • @MavikBow
    @MavikBow Месяц назад +3

    What are the benefits of this program other than saving 10 lines in a script? Does it objectively work faster? Does it use less memory? Give us examples of cases when this program will be most useful compared to other already well known ones?

    • @MarcoAntonio-jq7lo
      @MarcoAntonio-jq7lo Месяц назад +2

      That's like saying why did they add iterator functions in javascript (find, map, slice, reduce, etc) when we already have foreach loops, what are the benefits of those functions other than saving 10 lines of code?

    • @MarcoAntonio-jq7lo
      @MarcoAntonio-jq7lo Месяц назад

      Or better yet why have foreach when we have for (int i=0; i

    • @StunnerAlpha
      @StunnerAlpha Месяц назад

      Look at this video as an informative info nugget that you can keep in the back of your mind so that when you are in a situation where you need to read file contents in bash you can pull this guy out. Also he covered some extra nifty features such as limiting and callback per line so this adds that bit of functionality for free.

    • @MavikBow
      @MavikBow Месяц назад

      @@StunnerAlpha I get your point. But that's a bad mindset for if you work on a codebase with a group of people. Because every time you go "oh, gotta learn a cool new tool, gotta use it here" your teammates go "oh shit, gotta learn a new tool every time I debug this guy's code, as if I didn't have anything important to do".
      A new tool should justify it's use case.

    • @StunnerAlpha
      @StunnerAlpha Месяц назад

      @@MavikBow I mean I think this new tool is a small enough benign value add that I believe it can easily be explained away with a comment in the code above it’s use explaining what it does and what each parameter passed to it does.
      If you needed something like a callback to be run for each line in a file then this is a huge value add over rolling your own solution which is a liability. Sure there is a perfect middle ground. But I’d advocate more for putting comments in the code that very clearly explains what certain 3rd party scripts are doing.
      Additionally, maybe leave a link to this video in your code comment? I mean it’s a sub 3 minute video. Doesn’t get much shorter than that!

  • @jsaenzMusic
    @jsaenzMusic Месяц назад

    I was like, "this looks awfully similar to the readarray built in?". Then looking at the man page I realized theyre the same! 😅

  • @vasiliigulevich9202
    @vasiliigulevich9202 Месяц назад +1

    If you use arrays, it is time to move on from Bash.

  • @lilikoi90
    @lilikoi90 Месяц назад

    Probably faster with `IFS=$'
    '; printf '%s
    ' $( < file.txt ); IFS=$' \t
    '`

    • @rogo7330
      @rogo7330 Месяц назад +1

      Yes, but that just a redirect to printf. 'mapfile' actually reads data to the array where you can then process it as a whole. With 'read' you can do that line by line though, but mapfile is much more consise.

    • @lilikoi90
      @lilikoi90 Месяц назад

      @@rogo7330 You can do similarly to fill an array: `IFS=$'
      ' ar=( $( < file.txt ) ) IFS=$' \t
      ';`
      This is actually *faster* than mapfile for large files, but you have to deal with $IFS.

    • @lilikoi90
      @lilikoi90 Месяц назад

      @@rogo7330 mapfile will KEEP the newlines, so lines with nothing but a newline will disappear with the IFS method...

    • @lilikoi90
      @lilikoi90 Месяц назад

      @@rogo7330 You can also do that to fill an array: `IFS=$'
      '; arr=( $( < file.txt ) ); IFS=$' \t
      `. Much faster than mapfile for large files, and removes empty lines from the input array.