LinkedIn Sourceforge

Vincent's Blog

Pleasure in the job puts perfection in the work (Aristote)

List of some Shell goodies for OpenBSD

Posted on 2021-01-02 17:46:00 from Vincent in OpenBSD

I'm sharing here some practices I'm following and some small tips/tools which facilitate my usage of OpenBSD in my day to day.

Some a really specific to my usage, others could be re-used.


Introduction

I'll list some of my tips and tricks around OpenBSD.
I'm using OpenBSD as daily machine since +10 years now and I'm still amazed how simple and powerful this system is.

So most of the elements listed here under will match KSH on OpenBSD. I'm using Openbox as window manager, xterm as terminal and neovim as editor.

If you want to re-use some of those ideas, you will have to adapt it to your own config.

Path organization

On OpenBSD folder's organization is :

- Base applications in /
- Packages in /usr/local

So I push all my own application is ~/.local

So, one of my first line in ~/.profile is the following

PATH=$HOME/.local/bin:$PATH

An other important element for Windows Manager and xdg's tool (pkg_add xdg-utils) is to find .desktop files in :

~/.local/share/applications

Xterm window title

I often have lot of xterm windows open. Generally they are split between the Openbox virtual desktops depending on the scope/activity.

But even that, I have lot of windows. So, I like to change their window title, in order to retrieve them more easily when doing a Alt-TAB or by checking the task bar.

To modify the window's title of xterm, here after a simple command I've added in my .profile:

set_title()
{
    echo -n "\033]0;$1\007"
}

When you want to change the window's title, you just have to type in the window where you want a new title:

set_title "dev branch xxx"

Some colors in ls

To avoid the monochrome of xterm, I'm using colorls (pkg_add colorls).

Then in .profile, I put:

export LSCOLORS=gxfxcxdxbxegedabagacad
alias ls='colorls -GF'

The first line defines the color to use in the different situation. With colorls we have 11 different situations (directory, link, socket, pipe, exe, ...). For each of those 11, you must provide the foreground color and the background color.
The second line is just to over-write the standard /bin/ls command. The -G is to use the defined colors, with -F colorls adds some visual characters to differentiate folders, executable, ...

Other colors for the battery status

As daily machine, I have a laptops, which travel a lot. So I need to know the exact state of the battery. This information is displayed on my status bar. But I do not look it so frequently. So, I prefer to have this information in my preferred shell.

I'm using the following script from my .profile file:

battery()
{
    local bat=""
    local level=""
    local charg=""
    bat=$(apm -b 2>/dev/null)
    if [ -n "$bat" -a "$bat" -lt "4" ]; then
        level=$(apm -l 2>/dev/null)
        charg=$(apm -a 2>/dev/null)
        if [ -n "$charg" -a "$charg" -eq "1" ]; then
            #charging
            print -- "\033[01;32m$level%\033[00m"
        else
            if [ "$level" -lt "20" ]; then
                #print red
                print -- "\033[01;31m$level%\033[00m"
            else 
                #print blue
                print -- "\033[01;34m$level%\033[00m"
            fi
        fi
    else
        #no battery
        print -- ""
    fi
}

It does report the battery status, if there is one, with 3 colors: green when the power cable is connected, blue when the battery is in good state, red when the battery is bellow 20%.

To have such information directly in my shell, I'm using it in PS1:

export PS1='$(battery)\h:\w\$ '

By doing this, battery's usage is refreshed each time I press "enter".

The other parameters of PS1 does not provide colors, they display hostname "\h", folder "\w" and default prompt "\$".

Here after a print screen with colorls showing different type of files. Note that I've changed the window's title ;-)

Completions

It's frustrating to type keywords completely, so I'm a big fan of the completion feature of KSH.

It's really easy to configure: just create an array with the elements you have to select. Then you just have to type TAB to have the relevant choice displayed in front of you.

I group them by command.

# ssh, scp
set -A SSH_KNOWN_HOSTS ~/.ssh/known_hosts
if [ -f /etc/ssh/ssh_known_hosts ]; then
 SSH_KNOWN_HOSTS="${SSH_KNOWN_HOSTS[@]} /etc/ssh/ssh_known_hosts"
fi
HOST_LIST=$(awk '{split($1,a,","); gsub("].*", "", a[1]); gsub("\[", "", a[1]); print a[1] " root@" a[1]}' $SSH_KNOWN_HOSTS | sort | uniq)
set -A complete_ssh -- $HOST_LIST
set -A complete_scp -- $HOST_LIST

set -A complete_ifconfig_1 -- $(ifconfig | grep ^[a-z] | cut -d: -f1)

set -A complete_signify_1 -- -C -G -S -V
set -A complete_signify_2 -- -q -p -x -c -m -t -z
set -A complete_signify_3 -- -p -x -c -m -t -z

set -A complete_sndioctl_1 -- $(sndioctl | cut -d= -f 1)

set -A complete_chown_1 -- $(users)

alias drcctl="doas rcctl"
set -A complete_drcctl_1 -- get getdef set check reload restart stop start disable enable order ls
set -A complete_drcctl_2 -- $(/bin/ls /etc/rc.d)

The first lines allow me to list all known hosts from known_hosts files.

The next group display possible interfaces on my machine for the ifconfig command.

The next group is to simplify the usage of signify, per type of command.

For sndioctl it display the possible parameters we can change.

for chwon's command, completion display the known users on my machine.

The last group of command concerns rcctl. Since I'm never using "root", I've created a drcctl command in order to have the usual commands as 1st parameter, then the list of possible daemon available on my machine.

With such completion, I'm no more forced to type the full list of parameters.

For example, if I type:

ifc "TAB" i "TAB" scan, I will execute the command
ifconfig iwm0 scan

drc "TAB" sta "TAB" pos "TAB", will become
doas rcctl start postgresql

Conclusion

Very simple tricks with facilitate the usage of a system like OpenBSD and KSH

In the same "laziness" I'm also a big fan if fzf. I'll describe how I'm using is in a next blog's post.



50, 46
displayed: 21849
Comments:

1. From Privacy-REX on Thu Nov 9 16:56:37 2023

Hello Vincent. Just a quick Thank You for all your excellent posts on OBSD. I've been messing around w/ ramdisks on OBSD74 (mfs in fstab) for heavy programs like GIMP, Seamonkey, etcetera, in order to speed up loading times & harden security even furtherr. It works well, just don't push system files to RAM unless you have oodles of time to fine tune them - LOL. You can also set the ramdisks to readOnly which is very important for web browser security. The ramDisks are destroyed at shutdown, or reboot, & rebuilt at boot time, so any nasties that happen to get in, are destroyed at shutdown. :-) Obsd-Dev Solene (@ dataswamp) has an article on pushing tmp to ram. The article is here... https://dataswamp.org/~solene/2018-05-08-mfs-tmp.html I don't know if you've seen it. I'm still messing around w/ the ramdisks and fine tuning them, but I'm having a lot of fun doing it. Hey, thanks once again for all the awesome articles you have here.

2. From Vincent on Sat Nov 11 21:27:51 2023

Many thanks for this encouraging feedback. Appreciated. Concerning blog of Solene ... indeed I read it as much as I can. What I amazed with OpenBSD is how flexible it is ;). Like memory Filesystems

3. From Privacy-REX on Sun Sep 8 05:13:11 2024

Hello again Vincent: I hope this post finds you doing well. I do a lot of "cd"ing around the file system, & got tired of having to 'ls' every directory that I wanted to view. So, i wrote a handy little script to take care of this for me. I thought you - & your readers - might be interested, so here it is: (I put this in: ~/.profile .)


function _cd {
		  #!/usr/bin/ksh
             	  path=$(pwd)
               	  echo "You are now leaving      " $path " "
	      	  cd $@
               	  path2=`pwd`
              	  echo "...And entering -->      " $path2 " 
"
              	  echo "Contents of $path2 are:
"
               	  ls -lax
}

alias cd='_cd'
The code is pretty simple, so modify it for your own use. Please note that the second variable required backticks in order to properly function. (Might be something w/ my system setup.) I'm happy with it & works as intended. Oh, one more thing, my Colorls is: export LSCOLORS=gxBxcxdxExegedAbAgCxDx (in .profile) It follows my system color schema more closely. (man colorls) gives the color codes

4. From Privacy-REX on Sun Sep 8 05:43:34 2024

Hello again Vincent: I hope this post finds you doing well. I do a lot of "cd"ing around the file system, & got tired of having to 'ls' every directory that I wanted to view. So, i wrote a handy little script to take care of this for me. I thought you - & your readers - might be interested, so here it is: (I put this in: ~/.profile .)


function _cd {
		  #!/usr/bin/ksh
             	  path=$(pwd)
               	  echo "You are now leaving      " $path " "
	      	  cd $@
               	  path2=`pwd`
              	  echo "...And entering -->      " $path2 " 
"
              	  echo "Contents of $path2 are:
"
               	  ls -lax
}

alias cd='_cd'
The code is pretty simple, so modify it for your own use. Please note that the second variable required backticks in order to properly function. (Might be something w/ my system setup.) I'm happy with it & works as intended. Oh, one more thing, my Colorls is: export LSCOLORS=gxBxcxdxExegedAbAgCxDx (in .profile) It follows my system color schema more closely. (man colorls) gives the color codes

5. From Vincent on Sun Sep 8 21:29:24 2024

Many thanks for your contribution ;). If I'm doing the same $() in path2, than in PAth, it works. At least on my OpenBSD.




What is the first vowel of the word Python?