The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 351β€…    Votes:  5β€…    βœ… Solution
Tags: bash   nautilus   symbolic-link  
Link: πŸ” See Original Answer on Ask Ubuntu πŸ”—

URL: https://askubuntu.com/q/1019481
Title: Desktop shortcut to Bash script crashes and burns
ID: /2018/03/26/Desktop-shortcut-to-Bash-script-crashes-and-burns
Created: March 26, 2018    Edited:  June 12, 2020
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


The problem is the script relies on the TERM environmental variable being setup. The Ubuntu Unity Desktop does not have this initialized when scripts are called. If you open a terminal with Ctrl+Alt+T the variable is setup.

To test your system create a little script called test-term.sh and make it look like this:

#!/bin/bash

#See if $TERM has been set when called from Desktop shortcut

echo TERM environment variable: $TERM > ~/Downloads/test-term.txt
echo "Using env | grep TERM output below:" >> ~/Downloads/test-term.txt
env | grep TERM >> ~/Downloads/test-term.txt

exit 0

Create a link in Nautilus to test-term.sh and run the link. Then check the output file:

$ cat ~/Downloads/test-term.txt

TERM environment variable: dumb
Using env | grep TERM output below:
(... blank line appears here ...)

As you can see the environment variable TERM is blank when the command env | grep TERM is used. Also the variable $TERM is set to dumb which doesn’t suit the color-based, mouse-supported command dialog very well.


Boilerplate solution

The short term solution was to include boilerplate code at the top of the two scripts in question:

# $TERM variable may be missing when called via desktop shortcut
CurrentTERM=$(env | grep TERM)
if [[ $CurrentTERM == "" ]] ; then
    notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
    exit 1
fi
⇧ Display grub menu and options without rebooting? strace hwinfo --dump-db 0 does not dump to /var/lib/hardware  β‡©