26 KiB
Phundrak’s executable scripts
Presentation
This file will present all the executable scripts I wrote. It is also their original source code, all the following code snippets are exported and tangled from this file to the actual executables.
Please do not forget to run the following before tangling files from this file to make sure the tangled files will be executables.
(defun phundrak/make-tangled-files-executable ()
(set-file-modes (buffer-file-name) #o755))
(add-hook 'org-babel-post-tangle-hook 'phundrak/make-tangled-files-executable)
Askpass
Askpass is a simple script that invokes rofi
as a way to get from a GUI the
user’s sudo password. It is inspired by this original tool, rewritten in fish
and with rofi support instead of dmenu. As you can see, this is a oneliner if
we ignore the initial shebang. This executable is pointed at by the
#!/usr/bin/fish
rofi -dmenu -font 'DejaVu Sans 10' -password -no-fixed-num-lines \
-p (printf $argv[1] | sed s/://)
Dmenu
I wrote this very simple script in order to replace dmenu
with rofi’s
emulation of dmenu, since I prefer rofi’s appearance. It basically calls
rofi’s dmenu emulation with the arguments initially passed to dmenu.
#!/usr/bin/fish
/usr/bin/rofi -dmenu $argv
Emoji picker
The emoji picker is a simple fish script that uses rofi and
~/.config/emoji.txt to provide a small, local search for emojis. Once the
emoji is selected, it is copied to the clipboard using xclipboard
.
#!/usr/bin/fish
grep -v "#" ~/.config/emoji.txt | rofi -dmenu -i | awk '{print $1}' | tr -d '\n' | xclip -selection clipboard
Also, let’s send a notification telling the user the emoji has been copied!
set emoji (xclip -o -selection clipboard | tr -d '\n')
set -a emoji "copied to clipboard"
pgrep -x dunst >/dev/null && notify-send $emoji
It is inspired from this video from Luke Smith, rewritten in Fish.
Rofimount
rofimount
is a script inspired by this one, based on dmenu, which
interactively asks the user what to mount, and where to mount it. What I did
was replace dmenu with rofi, and fix a couple of bugs I encountered in the
original script. For the record, this is a fish script. Let’s declare our
shebang.
#!/usr/bin/fish
Get the mountable elements
begin
What the script does first is detect everything that can be mounted. Between
a begin
and end
, let’s set LFS
as a local variable. This si in order to
get sane variables in the current block.
set -l LFS
Now, let’s detect the amount of mountable Android filesystems, and if any are detected, let’s read them into a global variable.
set -l a (math (jmtpfs -l | wc -l) - 2)
test $a -ge 0 && jmtpfs -l 2> /dev/null | tail -n $a | read -zg anddrives
We’ll do the same for external and internal drives and partitions that can be mounted here.
lsblk -rpo "name,type,size,mountpoint" | \
awk '$2=="part"&&$4==""{printf "%s (%s)\n",$1,$3}' | \
read -zg usbdrives
Finally, we look for any CD drive that could be mounted on our device.
blkid /dev/sr0 | awk '{print $1}' | sed 's/://' | read -z cddrives
And that’s the end of our first block!
end
Alright, we’ll save what kind on drives we can mount in a temporary file
called /tmp/drives
. We’ll make sure it’s blank by erasing it then creating
it again with touch
, like so. The -f
flag on rm
is here so we get no
error if we try to delete a file that doesn’t exist (yet).
set -g TMPDRIVES /tmp/drives
rm -f $TMPDRIVES
touch $TMPDRIVES
Now, let’s write what type of drives we can mount in this temporary file.
test -n "$usbdrives" && echo "USB" >> $TMPDRIVES
test -n "$cddrives" && echo "CD" >> $TMPDRIVES
test -n "$anddrives" && echo "Android" >> $TMPDRIVES
Now, we want to declare where to look for mount directories. For now, we’ll
only look in /mnt
, but you can add more if you wish.
set -g basemount /mnt
Get the drive to mount
Now, let’s declare a function that will allow us to chose the drive we want to mount.
function getmount
First, we want to get our mount point. We’ll run a find
command on each of
the directories listed in $basemount
to look for folders on which our drive
could be mounted. This list will be passed to rofi from which we will chose
our mount point.
set -g mp (for d in $basemount
find $d -maxdepth 5 -type d
end | rofi -dmenu -i -p 'Type in mount point.')
We should verify that something has been actually selected, otherwise we should abort the script.
if test -z $mp || test $mp = ""
return 1
end
Now, if the selected mount point does not exist, we’ll ask the user whether the directory should be created. If no, the script will abort. If yes, an attempt will be made at creating the directory as the user; if that fails, a new attempt will be made as sudo.
if test ! -d $mp
switch (printf "No\\nYes" | rofi -dmenu -i -p "$mp does not exist. Create it?")
case 'Yes'
mkdir -p $mp || sudo -A mkdir -p $mp
case '*'
return 1
end
end
Finally, let’s close the function
end
Mount a USB drive, hard drive or partition
Alright, we want to mount a partition that answers by the name of
/dev/sdXX
, how do we do that? Let’s create first the function mountusb
that will take care of it for us.
function mountusb
Now, the first thing we want to do is select the partition we want to mount.
Remember, we stored those in $usbdrives
earlier, so let’s pipe them into
rofi so we can chose from it. Also, awk
will get their path in /dev
.
set -g chosen (echo $usbdrives | \
rofi -dmenu -i -p "Mount which drive?" | \
awk '{print $1}')
As usual after a user selection, let’s verify something has actually been selected. If not, let’s abort the script.
test -z $chosen && return 1
Now, let’s select the mount point of our partition. We’ll call the function
getmount
described in Get the drive to mount to select it.
getmount
Let’s verify the variable mp
set in getmount
is not empty, otherwise
abort the script.
test -z $mp && return 1
Now, let’s mount it! We’ll use a switch which will detect the filesystem used so we know how to mount the partition.
switch (lsblk -no "fstype" $chosen)
We have two named case: vfat
filesystems.
case "vfat"
sudo -A mount -t vfat $chosen $mp -o rw,umask=0000
And ntfs
filesystems.
case "ntfs"
sudo -A mount -t ntfs $chosen $mp -o rw,umask=0000
Else, we’ll let mount
determine which filesystem is used by the partition
(generally ext4
).
case '*'
sudo -A mount $chosen $mp
We’ll also run a chown
on this newly mounted filesystem so the user can
access it without any issues.
sudo -A chown -R $USER:(id -g $USER) $mp
Let’s close the switch block and send a notification the partition has been mounted.
end && notify-send -a "dmount" "💻 USB mounting" "$chosen mounted to $mp."
And let’s close the function.
end
Mount an Android device
The function that manages to mount Android filesystems is mountandroid
.
Let’s declare it.
function mountandroid -d "Mount an Android device"
We’ll select which Android we want to mount. We will be asked through rofi.
set chosen (echo $anddrives | rofi -dmenu -i -p "Which Android device?" | awk '{print $1 $2}' | sed 's/,$//')
Now, we need to get the bus of the Android device we want to mount. It will be useful later, after we authorized mounting from our device, to get the path to our partition.
set bus (echo $chosen | sed 's/,.*//')
Let’s temporarily mount our device.
jmtpfs -device=$chosen $mp
Now, we need to allow our computer to mount our Android device. Depending on the Android version it is running on, we either need to specify our device is USB connected in order to exchange files, or Android will explicitely ask us if it is OK for our computer to access it. Let’s inform the user of that.
echo "OK" | \
rofi -dmenu -i -p "Press (Allow) on your phone screen, or set your USB settings to allow file transfert"
Now, let’s get the actual path of our Android filesystem we wish to mount, and let’s unmount the previous temporary filesystem.
set newchosen (jmtpfs -l | grep $bus | awk '{print $1 $2}' | sed 's/,$//')
sudo -A umount $mp
Now we cam mount the new filesystem and send a notification if everything went well.
jmtpfs -device=$newchosen $mp && \
notify-send -a "dmount" "🤖 Android Mounting" "Android device mounted to $mp."
And now, we can close our function.
end
Mount a CD drive
This part is way easier than the previous functions. As we will see, the
function mountcd
's body is only three lines long. First, let’s declare the
function.
function mountcd
Now, let’s chose the CD drive we want to mount using rofi
.
set chosen (echo $cddrives | rofi -dmenu -i -p "Which CD drive?")
We’ll also get the mountpoint thanks to the getmount
function described
earlier.
getmount
And finally, let’s mount it and send the notification everything went well.
sudo -A mount $chosen $mp && \
notify-send -a "dmount" "💿 CD mounting" "$chosen mounted."
Finally, let’s close our function.
end
Ask what type of drive we want to mount
The first thing we will be asked if different types of drives are detected is
which of these types the user wishes to mount. This is done with the function
asktype
which is declared below.
function asktype
We will use a switch statement which will use our anwser to rofi about what we wish to mount.
switch (cat $TMPDRIVES | rofi -dmenu -i -p "Mount which drive?")
If we chose the option "USB", we’ll mount a hard drive, partition or USB
drive. In which case we’ll call the mountusb
function.
case "USB"
mountusb
If we chose the "Android" option, the mountandroid
function is called.
case "Android"
mountandroid
Else if we chose the "CD" option, we’ll call the mountcd
function.
case "CD"
mountcd
If nothing is selected, the function will naturally exit. Now, let’s close our switch statement and our function.
end
end
Launch the mounting functions
Now that we have declared our functions and set our variables, we’ll read the temporary file described in Get the mountable elements. The amount of lines is passed in a switch statement.
switch (wc -l < $TMPDRIVES)
If the file has no lines, i.e. it is empty, we have no mountable media. Let’s inform our user this is the case.
case 0
notify-send "No USB drive or Android device or CD detected" -a "dmount"
If we only have one line, we have only one type of mountable media. We’ll pass this line to a second switch statement.
case 1
switch (cat $TMPDRIVES)
This will allow the script to automatically detect what type of media it is, and mount the corresponding function.
case "USB"
mountusb
case "Android"
mountandroid
case "CD"
mountCD
Let’s close this nested switch case.
end
If we have more than one line, we’ll have to ask the user what type of media they want to mount.
case '*'
asktype
Now, let’s end our switch statement!
end
Finally, we’ll delete our temporary file.
rm -f $TMPDRIVES
And with that, this is the end of our script!
Rofiumount
rofiumount
is the counterpart of rofimount
for unmounting our mounted
partitions. It is a fish script, so let’s declare it as that with its shebang.
#!/usr/bin/fish
Get the unmountable drives
First, we will need to list all the drives that can be safely unmounted. Let’s run this.
set -g drives (lsblk -nrpo "name,type,size,mountpoint" | \
awk '$2=="part"&&$4!~/\/boot|\/home$|SWAP/&&length($4)>1{printf "%s (%s)\n",$4,$3}')
Now, let’s get the android devices that are mounted.
set -g androids (awk '/jmtpfs/ {print $2}' /etc/mtab)
And let’s get the CD drives that are mounted.
set -g cds (awk '/sr0/ {print $2}' /etc/mtab)
We’ll store all of our information in a temporary file, /tmp/undrives
.
set -g undrivefile /tmp/undrives
Let’s make sure we begin with a clean, empty file.
rm -f $undrivefile
touch $undrivefile
Depending on if the related variables are set, write the different types of mounted drives in the temporary file.
test -n "$drives" && echo "USB" >> $undrivefile
test -n "$cds" && echo "CD" >> $undrivefile
test -n "$androids" && echo "Android" >> $undrivefile
Unmount disk partitions
The function unmountusb
will take care of unmounting any drive we can
safely unmount. First, let’s declare the function.
function unmountusb
Let’s chose the drive to unmount with rofi.
set chosen (echo $drives | \
rofi -dmenu -i -p "Unmount which drive?" | \
awk '{print $1}')
Let’s verify if the user actually selected any drive. If no, let’s abort the script.
test -z "$chosen" && exit 0
Now, let’s unmount the chosen drive and send a notification if it has been done. Otherwise, a notification will be sent, saying the operation failed.
sudo -A umount $chosen && \
notify-send "💻 USB unmounting" "$chosen unmounted." -a "dumount" || \
notify-send "💻 USB unmounting" "$chosen unmounting failed." -a "dumount"
Now, let’s close the function.
end
Unmount Android device
The function unmountandroid
will take care of unmounting any mounted
Android device. First, let’s declare our function.
function unmountandroid
Let the user choose which Android device to unmount.
set chosen (echo $androids | rofi -dmenu -i -p "Unmount which device?")
We’ll verify the user chose any device.
test -z "$chosen" && exit 0
If a device has been chosen, let’s unmount it and send a notification whether it has been successfuly unmounted.
sudo -A umount -l $chosen && |
notify-send "🤖 Android unmounting" "$chosen unmounted." -a "dumount" || \
notify-send "🤖 Android unmounting" "$chosen failed to unmount." -a "dumount"
Finally, let’s close the function.
end
Unmount CD drive
unmountcd
will take care of unmounting any mounted CD drive. Let’s declare
this function.
function unmountcd
As before, let the user chose which CD drive to unmount.
set chosen (echo "$cds" | rofi -dmenu -i -p "Unmount which CD?")
We’ll verify the user chose any device.
test -z "$chosen" && exit 0
If a drive has been chosen, let’s unmount it and send a notification whether it has been successfuly unmounted.
sudo -A umount -l $chosen && \
notify-send "💿 CD unmounting" "$chosen unmounted." -a "dumount" || \
notify-send "💿 CD unmounting" "$chosen failed to unmount." -a "dumount"
Now, let’s close the function.
end
Ask what type of drive to unmount
If several types of unmountable drives are available, let’s ask the user which type to unmount based on the content of the temporary file declared in Get the unmountable drives. First, let’s declare the function.
function asktype
Let’s create a switch statement to which will be passed the selection of the user from rofi.
switch (cat $undrivefile | rofi -dmenu -i -p "Unmount which type of device?")
Three types of values can be returned: "USB", "CD", or "Android". These values will be used to launch their corresponding function.
case 'USB'
unmountusb
case 'CD'
unmountcd
case 'Android'
unmountandroid
Let’s close the switch statement.
end
Let’s now close the function.
end
Launch the unmounting functions
Now back to the body of our script, let’s input in a switch case the number of lines contained in our temporary file.
switch (wc -l < $undrivefile)
If the file containes no lines. i.e. it is empty, nothing is to be unmounted. Let’s inform the user of that.
case 0
notify-send "No USB drive or Android device or CD to unmount" -a "dumount"
Else, if there is only one type of drive, we’ll automatically let our script choose based on the content of this sole line.
case 1
switch (cat $undrivefile)
case 'USB'
unmountusb
case 'CD'
unmountcd
case 'Android'
unmountandroid
end
And if there are more types than one, let’s ask the user.
case '*'
asktype
Let’s close our main switch statement.
end
And finally, let’s delete our temporary file.
rm -f $undrivefile
Starwars
This is a one-liner that allows you to watch Star Wars episode 4 in ASCII art in your terminal. Here is the code:
#!/usr/bin/fish
telnet towel.blinkenlights.nl