Linux-Fu: Help Messages for Shell Scripts and Here Documents

Imagine that you want to output multiple lines of text in Bash, or any shell script. Maybe it’s for a help string for a particularly convoluted shell script you’re writing. You could have a separate echo command for each line.  Or you could use the “here document“.

The “here document” construction takes the text between two delimiters and passes it, as if it were piped, to a command.

if [[$# == 0 ]] || [[ "$1" == "-h" ]]; then
cat << EOF
This is my help message. There are many like it but this one is mine.
My help message is my best friend.
EOF

All of the text, as written, with line breaks and spaces and all, get passed to cat and your helpful formatted message is printed to the user.

You can use here documents interactively at the command prompt, and you can use them in functions:

#!/bin/bash
help() {
    cat << EOF
Here is your help.
EOF
}

More About Here Documents

More than just writing help scripts, here documents are useful any time that you want to enter multiple lines and have them piped to a command. We use them most often from the shell directly. There are a few nuances, however.

You probably figured out that the “EOF” string in the above examples marks the beginning and end of the here document. There’s nothing special about it. You can use any word that doesn’t appear in your document. So “<< END_OF_HERE_DOC” would work.

The end of document string has to be at the beginning of a line, and by itself. You should not have whitespace before or after it. It is usually a good idea to use something odd and more than a few characters as the end string, although — in theory — you could use even a single character.

But that requirement messes up code indentation — see the help() function above.  If you put a dash between the redirection brackets and the string, the shell will consume any leading tab characters (but not spaces!). That lets you indent your message so it doesn’t all run together. But here, you have to be careful about spaces in the declaration: cat <<- EOF and cat <<-EOF work, while cat << -EOF doesn’t.  (It expects the delimiter “-EOF”.)  Still, it lets you indent your code if you can get it right, and use tabs instead of spaces.

 
#!/bin/bash 
help() { 
    cat <<- EOF 
        Here is your help. 
    EOF 
} 

Substitution

One thing that might not be obvious is that the here document can handle variable expansion. Try typing:

cat << EOF
Your path is $PATH
Your prompt is $PS1
EOF

Your path is /usr/local/sbin:/usr/local/bin:...
Your prompt is ($HOSTNAME \w)\$

That even works with things like command substitution. So you could put things in like $(ls), for example. Sometimes you don’t want that, though. In those cases, just quote the end string. For example:

cat << "EOF"
Your path is $PATH
Your prompt is $PS1
EOF

Your path is $PATH
Your prompt is $PS1

Peculiarities

You can even send the input to a program that does nothing like : which might seem odd. But some people use that to comment out large blocks of script for debugging or other purposes. It is a bit wasteful, because the lines you don’t want get put in a temporary file and fed to the input of a program that will ignore them, but you might see it done.

Clearly, here documents are niche. I use them a lot for writing help strings in scripts, but they’re just the ticket when you need to pass multiple lines in a script or to a script as you typed multiple lines into stdin.

 

source https://hackaday.com/2020/08/03/linux-fu-help-messages-for-shell-scripts-and-here-documents/

Reach the right target audience with Junk Mail’s digital marketing products

We have some great news for South African businesses! Junk Mail offers a comprehensive variety of digital marketing products that can help you reach the right target audience, spread the word about your products or services, and get the leads and conversions you need to expand your business!

Sign Up For Junk Mail's Digital Marketing Products

Read more Reach the right target audience with Junk Mail’s digital marketing products ›

Cheap And Effective Mosquito Trap Looks Like A Disco

Words cannot quite articulate the collective loathing humankind has for mosquitoes, and rightfully so! These parasite peddling, blood sucking little critters are responsible for a great deal of human suffering. Mosquito-borne diseases such as malaria still account for a significant proportion of human mortality, especially in under-developed parts of the world . So it’s no wonder that people try to reduce their numbers; see this latest $40 mosquito trap by [jacobsk]. (Video, embedded below.)

The idea is critically simple, opening up the potential for widespread deployment. The base and body of the trap are made out of three five-gallon buckets with a mini desk fan sandwiched in between, providing suction into the main trap bin. An opening is cut in the top bucket as a point of entry, and an old school incandescent blacklight is mounted in the centre, with just enough IR and UV output to entice these little vermin, who will definitely regret mistaking it for a black-light rave.

[jacobsk] also does a very good job of showing every step of its construction in his videos. Whilst this solution is purposefully low tech, check out this admittedly overcooked way of killing mosquitoes, with a laser turret.

source https://hackaday.com/2020/08/03/cheap-and-effective-mosquito-trap-looks-like-a-disco/

Rhombicdodecahedron

Polyhedra

https://www.polyhedra.net/en/

So I saw a video on the Rhombic Icosahedron made by Adam Savage and the Numberphile.

I thought I may try my hand at it and hunted for the relevant angles.

Found this site that has cardboard cutouts with the relevant measurements and layouts to assemble.

Pop around and have a look, most can be done with cardboard, though i want mine as an infinity box.

Pinephone Gets Thermal Imaging Backpack

When you buy a mass-market mobile phone, you’re making the decision to trust a long list of companies with your private data. While it’s difficult for any one consumer to fully audit even a single piece of consumer technology, there have been efforts to solve this problem to a degree. The Pinephone is one such example, with a focus on openness and allowing users to have full control over the hardware. [Martijn Braam] is a proud owner of such a device, and took advantage of this attitude to add a thermal imager to the handset.

The build is not a difficult one, thanks to the expansion-friendly nature of the Pinephone hardware. The rear of the phone sports six pogo pins carrying an I2C bus as well as power. [Martin] started by modifying the back cover of the phone with contacts to interface with the pogo pins. With this done, the MLX90640 thermal imager was attached to the case with double-sided tape and wired up to the interface.

While the 32×24 output from the sensor isn’t going to help you build cutting edge heat-seeking missiles, it’s an affordable sensor with good performance for low-end thermal imaging tasks. We’ve featured teardowns of thermal imaging hardware before, too.

source https://hackaday.com/2020/08/03/pinephone-gets-thermal-imaging-backpack/

See the Science Behind VR Display Design, and What Makes a Problem Important

VR headsets are more and more common, but they aren’t perfect devices. That meant [Douglas Lanman] had a choice of problems to address when he joined Facebook Reality Labs several years ago. Right from the start, he perceived an issue no one seemed to be working on: the fact that the closer an object in VR is to one’s face, the less “real” it seems. There are several reasons for this, but the general way it presents is that the closer a virtual object is to the viewer, the more blurred and out of focus it appears to be. [Douglas] talks all about it and related issues in a great presentation from earlier this year (YouTube video) at the Electronic Imaging Symposium that sums up the state of the art for VR display technology while giving a peek at the kind of hard scientific work that goes into identifying and solving new problems.

Early varifocal prototype

[Douglas] chose to address seemingly-minor aspects of how the human eye and brain perceive objects and infer depth, and did so for two reasons: one was that no good solutions existed for it, and the other was that it was important because these cues play a large role in close-range VR interactions. Things within touching or throwing distance are a sweet spot for interactive VR content, and the state of the art wasn’t really delivering what human eyes and brain were expecting to see. This led to years of work on designing and testing varifocal and multi-focal displays which, among other things, were capable of presenting images in a variety of realistic focal planes instead of a single flat one. Not only that, but since the human eye expects things that are not in the correct focal plane to appear blurred (which is itself a depth cue), simulating that accurately was part of things, too.

The entire talk is packed full of interesting details and prototypes. If you have any interest in VR imaging and headset design and have a spare hour, watch it in the video embedded below.

Things have certainly come a long way since mobile phone-based VR headsets, and displays aren’t the only thing getting all the attention. Cameras have needed their own tweaks in order to deliver the most to the VR and AR landscape.

source https://hackaday.com/2020/08/03/see-the-science-behind-vr-display-design-and-what-makes-a-problem-important/

Electrochemistry at Home

A few years ago, I needed a teeny, tiny potentiostat for my biosensor research. I found a ton of cool example projects on Hackaday and on HardwareX, but they didn’t quite fulfill exactly what I needed. As any of you would do in this type of situation, I decided to build my own device.

Now, we’ve talked about potentiostats before. These are the same devices used in commercial glucometers, so they are widely applicable to a number of biosensing applications. In my internet perusing, I stumbled upon a cool chip from Texas Instruments called the LMP91000 that initially appeared to do all the hard work for me. Unfortunately, there were a few features of the LMP91000 that were a bit limiting and didn’t quite give me the range of flexibility I required for my research. You see, electrochemistry works by biasing a set of electrodes at a given potential and subsequently driving a chemical reaction. The electron transfer is measured by the sensing electrode and converted to a voltage using a transimpedance amplifier (TIA). Commercial potentiostats can have voltage bias generators with microVolt resolution, but I only needed about ~1 mV or so. The problem was, the LMP91000 has a resolution of ~66 mV on a 3.3 V supply, mandating that I augment the LMP991000 with an external digital-to-analog converter (DAC) as others had done.

However, changing the internal reference of the LMP91000 with the DAC confounded the voltage measurements from the TIA, since the TIA is also referenced to the same internal zero as the voltage bias generator. This seemed like a problem other DIY solutions I came across should have mentioned, but I didn’t quite find any other papers describing this problem. After punching myself a little, I thought that maybe it was a bit more obvious to everyone else except me. It can be like that sometimes. Oh well, it was a somewhat easy fix that ended up making my little potentiostat even more capable than I had originally imagined.

I could have made a complete custom potentiostat circuit like a few other examples I stumbled upon, but the integrated aspect of the LMP91000 was a bit too much to pass up. My design needed to be as small as possible since I would eventually like to integrate the device into a wearable. I was using a SAMD21 microcontroller with a built-in DAC, therefore remedying the problem was a bit more convenient than I originally thought since I didn’t need an additional chip in my design.

I am definitely pretty happy with the results. My potentiostat, called KickStat, is about the size of a US quarter dollar with a ton of empty space that could be easily trimmed on my next board revision. I imagine this could be used as a subsystem in any number of larger designs like a glucometer, cellphone, or maybe even a smartwatch.

Check out all the open-source files on my research lab’s GitHub page. I hope my experience will be of assistance to the hacker community. Definitely a fun build and I hope you all get as much kick out of it as I did.

source https://hackaday.com/2020/08/02/electrochemistry-at-home/

Electrochemistry at Home

A few years ago, I needed a teeny, tiny potentiostat for my biosensor research. I found a ton of cool example projects on Hackaday and on HardwareX, but they didn’t quite fulfill exactly what I needed. As any of you would do in this type of situation, I decided to build my own device.

Now, we’ve talked about potentiostats before. These are the same devices used in commercial glucometers, so they are widely applicable to a number of biosensing applications. In my internet perusing, I stumbled upon a cool chip from Texas Instruments called the LMP91000 that initially appeared to do all the hard work for me. Unfortunately, there were a few features of the LMP91000 that were a bit limiting and didn’t quite give me the range of flexibility I required for my research. You see, electrochemistry works by biasing a set of electrodes at a given potential and subsequently driving a chemical reaction. The electron transfer is measured by the sensing electrode and converted to a voltage using a transimpedance amplifier (TIA). Commercial potentiostats can have voltage bias generators with microVolt resolution, but I only needed about ~1 mV or so. The problem was, the LMP91000 has a resolution of ~66 mV on a 3.3 V supply, mandating that I augment the LMP991000 with an external digital-to-analog converter (DAC) as others had done.

However, changing the internal reference of the LMP91000 with the DAC confounded the voltage measurements from the TIA, since the TIA is also referenced to the same internal zero as the voltage bias generator. This seemed like a problem other DIY solutions I came across should have mentioned, but I didn’t quite find any other papers describing this problem. After punching myself a little, I thought that maybe it was a bit more obvious to everyone else except me. It can be like that sometimes. Oh well, it was a somewhat easy fix that ended up making my little potentiostat even more capable than I had originally imagined.

I could have made a complete custom potentiostat circuit like a few other examples I stumbled upon, but the integrated aspect of the LMP91000 was a bit too much to pass up. My design needed to be as small as possible since I would eventually like to integrate the device into a wearable. I was using a SAMD21 microcontroller with a built-in DAC, therefore remedying the problem was a bit more convenient than I originally thought since I didn’t need an additional chip in my design.

I am definitely pretty happy with the results. My potentiostat, called KickStat, is about the size of a US quarter dollar with a ton of empty space that could be easily trimmed on my next board revision. I imagine this could be used as a subsystem in any number of larger designs like a glucometer, cellphone, or maybe even a smartwatch.

Check out all the open-source files on my research lab’s GitHub page. I hope my experience will be of assistance to the hacker community. Definitely a fun build and I hope you all get as much kick out of it as I did.

source https://hackaday.com/2020/08/02/electrochemistry-at-home/