Create a select menu with smenu

Multiple Choice

The -T option allows for the selection of multiple points by pressing the T or Insert keys in the running script for each relevant point. Complete the selection by pressing Enter. A marked entry appears underlined. Remove the marker by repeatedly pressing T or Delete.

The software uses the space as a field separator, unless you specify otherwise. However, the example shown in Listing 6 (Figure 6) uses a colon, which allows you to use tools (e.g., cut) during further processing of the variable's contents, to address a column.

Figure 6: Some applications require the selection of multiple entries.

Menu Preselection

If you have no way to limit the list of menu options from the command in the pipe, filter the data when you call smenu. If necessary, use regular expressions (-e option, Listing 7). Alternatively, work with the positive hits (-i option, Listing 8).

The effect is generally the same: The unwanted entries appear in a color that offer less contrast and cannot be selected (Figure 7). When using both options simultaneously, keep in mind that exclusion has a higher priority. As a result, you might not get any items to select.

Figure 7: If the software supplying the data offers no way to filter it, use smenu functions to include or exclude points.

PDF Search

To browse a (quite large) portfolio of PDF files, use the shell script (Listing 9). Using smenu, the small program displays the selection of hits and allows you to take further actions.

Listing 9


The script only runs under Bash because it uses the read command. Either assign the appropriate rights to run it directly, or call it with:


(For PDFs, check out qpdfview or Evince: Both accept a search term at launch.)

When launching, enter the search term (Figure 8), which the script then transfers to pdfgrep. It shows only the number of hits, ignoring case sensitivity. Any hits are provided for selection via smenu.

Figure 8: After entering a search term, the search launches and provides a selection of hits.

If you have selected a file to display, the script browses the search term in the PDF viewer. It then highlights it in the document (Figure 9). After closing the display program, either reuse the selection or enter a new search term (Figure 10).

Figure 9: Certain programs allow you to transfer a term when launched, which the software then highlights in the document.
Figure 10: After viewing a PDF document, the script gives you the option of displaying another file or launching a new search.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • FOSSPicks

    Graham explores GnuCash 3, Kawaii-Player 3.3.1, tig, HandBrake 1.1.0, GameMode, XLEngine, and more!

  • Workspace: Text Expander

    A couple of utilities and a dash of Bash scripting are all you need to roll out a simple yet flexible text expander.

  • Zenity and KDialog

    Zenity and KDialog let you integrate your scripts with the native KDE or Gnome environment.

  • Bash scripting

    A few scripting tricks will help you save time by automating common tasks.

  • Automating FTP

    If you find your self executing the same few steps in FTP, you’ll save time and effort with a little automation.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95