dmenu piping script with a few small bugs.

Need help with C, C++, perl, python, etc?

dmenu piping script with a few small bugs.

Postby bedtime » 2018-02-17 21:28

You must have dmenu installed and working for this script to work! Do not try until you've figured that out!

First of all, don't let all the code lines discourage you; there are only 14 simple lines that make this program run. The rest is just notes and space. :wink:

This is a script that allows the user to use a 9menu-like configuration file and run the programs with it. Just as if one was using 9menu.

For example, here is my config:

Code: Select all
links2:/usr/local/bin/links -g
urxvtcd -e top
mpsyt:urxvtcd -e mpsyt
mail:urxvtcd -e neomutt
logout:killall xinit
reboot:sudo shutdown -r now
off:sudo shutdown -h now

It also allows the user to run aliases, which dmenu does not allow by default.

The code works, but some of it I would deem as poor code. And there are 3 light and non-fatal bugs, the biggest one of which I am getting is a 'bad file command' issued by shell when the user decides to type in a non-menu item into dmenu, though the program with its parameters will still be executed. This is likely caused by me using variables to store commands, which I know is a big no-no. Grep just keeps wanting to run the code and not just store it... :roll:

The instructions and what I've just said, plus examples and more info on the bugs, is all in the code. I've made the script as simple and easy to understand as possible and have included notes. Nothing needs to be compiled or installed.

If anyone has ideas on how to squash a bug or two, please let me know. Perhaps, after the bugs are sorted, I'll put this up in the 'How To' section; this kind of thing seems to be in high demand with dmenu users. I know that Arch has a program that does a similiar thing (which I used as a template for this one and so deserves the credit :wink:) which one can install via AUR.

Would be nice to have something that all distros could benefit from. :)

Code: Select all

# This script was inspired by another script which should take most
# of the credit:
# It can be procured through the Arch Linux AUR repos.

# You MUST have dmenu installed and properly working for this
# to work!!!
# Save this script as 'menu' (or whatever name you'd like).
# Give executable permissions:
# $ chmod +x menu
# Move it to a directory that is in your $PATH, such as /usr/bin,
# /usr/local/bin or execute anywhere via:      ./menu

# What to do:
# Make a simple list consisting of the name of the apps you want dmenu
# to display delimited by a colon (WITH NO SPACES!!!) and the app and
# executable parameters following, as shown below. Apps with no colon
# will run as they are named as will apps with parameters with no colon.
# If you want to use colons in your command arguements then you must have
# an app name + a colon + the command and arguements; the program will
# then extract from the first colon.
# You cannot do this:
# links -g
# The colon after 'https' will be taken as the starting point.
# You would have to do:
# links2:links2 -g

# In the example below, the app 'xterm' just runs as 'xterm'. The entry
# 'st -e top' will both show in the menu and run as such.
# I used my already existing 9menurc file, but you can chose whatever file
# name you would like so long as this code reflects your choice. Aliases are
# allowed. Make sure you set the 'mybashrc' variable if you chose to use
# them.
# Example of an app list:
# links2:links2 -g
# xterm
# st -e top
# urxvt:urxvtcd
# mpsyt:urxvtcd -e mpsyt
# mail:urxvtcd -e neomutt
# off:sudo shutdown -h now

# Bugs:
# #1. There is an extra field at the end of the dmenu selection.
#     This is due to inserting '\n's into the string for dmenu.
# #2. When a command with parameters is manually entered the grep
#     function will try to run it and it result in a non-fatal
#     error. Ex, if you type 'xterm -e top' into the input field,
#     an error will be shown in shell, but the command will still
#     run.
# #3. This script will run under #!/bin/sh but there is a strange character
#     inserted near in the first entry in dmenu. Somehow, it still runs fine.
# #4. All beginning app name entries must be different for the
#     program to work. Though I cannot imagine why someone
#     would want to have two or more apps with the exact same title.
#     You cannot have:
#     st:st
#     st:st -e top
#     But you can have:
#     st:st
#     stterm:st

# don't open if another dmenu is already open
if [ $(pgrep 'dmenu') ]; then

# you must have a file with your apps for this to run properly

# make sure your aliases are under this directory or change the path below

source $mybashrc

# gather words into a string that dmenu will accept
apps=$(awk -F':' '{print $1 "\\n"}' $myapplist)

# One of many failed attempts to trim the '\n' from the last field
#trimmed=$($apps | awk -F. '{print $NF}')

selected=$(echo -e $apps | dmenu)
execute=$(grep $selected":" $myapplist | cut -f2- -d ":")

#A one-liner of the last two commands
#selected=$(echo -e $(awk -F':' '{print $1 "\\n"}' $myapplist) | dmenu)

# if the app is not delimited by a colon above, then just run as is
if [ -z "$execute" ]; then

# For troubleshooting
#echo "Executed Command: " $execute

# run it!

# Possible workaround if the above does not execute (Not tested!)
# echo "$execute" | bash
Posts: 112
Joined: 2012-12-16 19:34

Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests