ArchLabs Linux

Bash Functions

Who uses bash functions in their .bashrc (if using bash)?

This thread just to share and talk about various bash functions.

Info: https://ryanstutorials.net/bash-scripting-tutorial/bash-functions.php

I found a great bash one liner that uses zathura and ghostscript to convert man pages to pdf via a dmenu search. Im not very good at bash scripting so the bash function worked well for this. I like to print info out an keep a hard copy file on some man pages and being able to put them in a pretty looking pdf file and print straight away is really handy.

so here is the function im using, you could probably turn this into a script but im unsure how to do it.

man2pdf () {
man -k . | awk '{ print $1 " " $2 }' | dmenu -i -p man | awk '{ print $2 " " $1 }' | tr -d '()' | xargs man -t | ps2pdf - - | zathura -
}

More info on the command here http://www.bashoneliners.com/oneliners/271/

4 Likes

Nicee @s7l . Might be usefull, just did a fresh AL install / bash in Sakura.

1 Like

Nice;

I assume you have you tried this ArchLabs. I get “-t is not a valid option for man” error, and “man man” confirms it’s not an option. Arch may be using a different version of man than Void. I’ll have to try it on ArchLabs later.

you could probably turn this into a script but im unsure how to do

just put the command that defines the function in a bash script - an oh so un-elligant example:

#!/bin/bash
man -k . | awk '{ print $1 " " $2 }' | dmenu -i -p man | awk '{ print $2 " " $1 }' | tr -d '()' | xargs man -t | ps2pdf - - | zathura -

exit 0;

Save it and make it executable. That function shouldn’t be too difficult since it’s a one-liner.

I use this command:

man foo | col -b -l80 >> foo.txt

to make a nicely formatted text file of a man page.

That could probably be converted to a function as well.

2 Likes

@PackRat

Oops i was on debian when i did this, sorry. Im now on Archlabs and it is not working.

Hmm, ill have to try and figure this out somehow, what does that command do for you in Archlabs Packrat?

Ok well it works but i dont see the list of man pages in the dmenu bar, its just blank.

I had to just type in the man page i wanted like say “date” and it worked. On my debian side the dmenu came up with an alphabetical list.

1 Like

I haven’t tried in ArchLabs yet.

Debian does some interesting things under the hood. On Debian, that command I use to create a text file does not require the pipe to the col command;

man foo >> foo.text

should be perfectly formatted.

1 Like

It works on my ArchLabs setup. And ArchLabs is using a different version (compile options?) of man than Void. All kinds of different options listed in “man man”.

Edit - Void uses mandoc (mdocml) for man pages, not man-db

I use this as an alternative to the arch-chroot command:

function mnt {
        for i in proc sys dev{,/pts} ; do sudo mount --bind /$i "$1"$i ; done &
        sudo chroot "$1" /bin/sh
        sudo umount -R "$1"{proc,sys,dev}
}

It’s a bit superfluous for ArchLabs (and Debian now that buster also has the arch-chroot command available) but I still use it.

EDIT: just made some new ones for QEMU:

function qemu {
   qemu-system-x86_64 -enable-kvm -m 4G -cpu host -soundhw hda -vga virtio -display gtk,gl=on -drive file="$1",format=raw,cache=none,if=virtio
}

function qemu-iso {
   qemu-system-x86_64 -enable-kvm -m 4G -cpu host -soundhw hda -vga virtio -display gtk,gl=on -cdrom "$1" -boot order=d -drive file="$2",format=raw,cache=none,if=virtio
}

function qemu-live {
   qemu-system-x86_64 -enable-kvm -m 4G -cpu host -soundhw hda -vga virtio -display gtk,gl=on -cdrom "$1"
}

The first launches the VM with the first argument being the name of the disk image, the second starts an installer with the first argument being the ISO image and the second argument the disk image, the last function just launches a live ISO (the name of the image being the first argument).

1 Like