You can check if the parent process is the shell. For example:
#! /bin/bash
if [[ $(readlink -f /proc/$(ps -o ppid:1= -p $$)/exe) != $(readlink -f "$SHELL") ]]
then
echo "Starting the shell..."
exec "$SHELL"
else
echo "Not starting a shell."
fi
ps -o ppid:1= -p $$
prints the PID of the parent process (ppid
) of the current process (-p $$
). A readlink
on /proc/<pid>/exe
should print the path to the executable, which would be the shell if you ran it in a shell, or something else otherwise.
Another possibility is the SHLVL
variable, which indicates how nested the current the shell instance is. If run within a shell, the script should have SHLVL
2 or greater. When run by double clicking, or from a desktop launcher, it should be 1:
#! /bin/bash
if (( SHLVL > 1 ))
then
echo "Starting the shell..."
exec "$SHELL"
else
echo "Not starting a shell."
fi
I would use a .rules file.
First, find out the ID_VENDOR_ID
and the ID_MODEL_ID
of your mouse. Disconnect the mouse, run this command and connect the mouse (the |grep ID
part is only to filter information that you don't need).
udevadm monitor --property|grep ID
Lets say that you get these values:
ID_VENDOR_ID=0a12
ID_MODEL_ID=0001
Now create a file in the rules folder (96 is the priority of the rule):
sudo gedit /etc/udev/rules.d/96-myusb.rules
Add these two lines using your values for the ID_VENDOR_ID
and ID_MODEL_ID
. If you don't want to do anything when you remove it, don't include the second line.
ACTION=="add", SUBSYSTEM=="usb",ENV{ID_VENDOR_ID}=="0a12", ENV{ID_MODEL_ID}=="0001",RUN+="/usr/local/bin/myusb-add.sh"
ACTION=="remove", SUBSYSTEM=="usb",ENV{ID_VENDOR_ID}=="0a12",ENV{ID_MODEL_ID}=="0001",RUN+="/usr/local/bin/myusb-remove.sh"
You can test that it works creating the two scripts:
$ sudo gedit /usr/local/bin/myusb-add.sh
Add something like this (change add
to remove
in the other one):
#!/bin/bash
echo "added" >> /tmp/myusb.log
Finally, tail the file with tail -f /tmp/myusb.log
and connect/disconnect your mouse. You should see that the text gets added to the file.
Best Answer
Yep. It is called
perl
. Some examples, with the corresponding interpreter in the shebang line of the file (the actual file extension doesn't matter):This is mentioned in Perl's documentation: