Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


hpr3625 :: Shell Tips and Snippets - Collaborative Effort

Carl and special guests provide some shell tips and examples.

<< First, < Previous, Latest >>

Hosted by Carl on Friday 2022-06-24 is flagged as Clean and is released under a CC-BY-SA license.
Tags: shell.
Listen in ogg, spx, or mp3 format. | Comments (2)

Part of the series: Bash Scripting

This is an open series in which Hacker Public Radio Listeners can share their Bash scripting knowledge and experience with the community. General programming topics and Bash commands are explored along with some tutorials for the complete novice.

Carl talks about a method to move function definitions to the bottom of a script using sed:

#!/bin/sh
source <(sed '1,/^exit/ d' $0)

__say "hello"

exit

__say()
{
    echo $1
}

Guest Host #1 (scroll to the bottom to ruin the surprise) talks about the shift command using this example:

startdate="$1" # Pick up date
shift
days=0

# Loop through args and create events
while [ $1 ] ; do # as many times as you add a timestamp
        [ $1 != "off" ] && khal new $(date -j -v+"$days"d -f %Y-%m-%d +%Y-%m-%d $startdate) $1 8H Work
        let days++
        shift
done

Guest Host #2 provides tips and examples on how to use variables safely and politely provide default values. One example of assigning a default value is:

foo=${foo:-"blah"}

Carl then closes out with the : (colon) shell builtin and provides a variation on the above default value:

: ${foo:="blah"}

Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2022-06-25T03:17:08Z by dnt

shift

Thanks for this, Carl! Couldn't imagine a better host! You are exactly right about the reason to talk about shift: you see it in scripts out there and it can be quite hard to glean what it does if you don't already know it. I also thank you for the observation on the word shift, which I actually hadn't noticed! The name of that script was myshifts.sh, so it's impossible to tell if it refers to the repeated running of the shift command or to the resulting calendar events. I like it!

Comment #2 posted on 2022-06-27T11:23:40Z by Carl

Thanks

Thanks for the comment and the contribution to the show dnt, you and klaatu really helped me get something out the door. My timing was off as usual as I should have had my recording process figured out beforehand but I'll improve it before the next show. The work/shift example was perfect!

<< First, < Previous, Latest >>

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the P in HPR stand for ?
Are you a spammer →
Who hosted this show →
What does HPR mean to you ?