Shell Scripting to create a .desktop file for a portable application

Bash Shell Scripting: Create a Link of a Portable App.

Bash Shell Scripting: Create a Link of a Portable App.

The term Shell applied to Operating Systems refers to the command interpreter of the Operating System. In general, it is a high-performance text interface that manifests itself in the form of a Terminal (Console) and that serves essentially for 3 important areas of work: Administering the Operating System, Running applications and interacting with them, and serving as a basic environment programming. And Scripting refers to the technique of designing and creating Scripts using a Shell.

Shell Scripts are extremely useful. It is a good idea to write those needs that we have and then edit scripts that do this work for us. And in this particular case we will use it to create links (shortcuts) of applications in the start menu and desktop of portable, self-executing and self-contained apps.

Alacarte Linux application

Introduction

Many times we have downloaded and installed applications that, being or not in the repositories of our GNU / Linux Distribution, when installing or executing it, it does not create the respective .desktop files of the same or in the best case it does not place it in the correct path so that after updating the Start Menu it is read and displayed in the list of installed packages of the Operating System.

Therefore, we have to appeal to the use of applications such as "Alacarte" or "Menulibre" to manually create the respective link in the Start Menu.

And although these graphic applications are very simple to use for this purpose, it is never too much to know create our own script to perform this operation and thus know from within how said operation is performed within the Operating System.

If you are not familiar with the creation and / or use of Shell Scripting files you can read this previous post (Shell, Bash and Scripts) to get started and then if necessary to explore all publications on the subject.

Linux Free Menu Application

Creating the program using Shell Scripting

We will assume for reasons of space that we already know how to create a script from scratch, that is, we already know how to create the header or initial parts of our script and we will go directly to its content.

However, if you have doubts about it, check this previous post (Build your program step by step using Shell Scripting - Part 1) to clarify doubts.

Content


#!/usr/bin/env bash
set -eou pipefail
IFS=$'\n\t'
setterm --reset
# NOMBRE: MI-APP LINUX POST INSTALL - SCRIPT BICENTENARIO (MIAPP-LPI-SB)
# VERSIÓN: 1.0+0
# TIPO DE PROGRAMA: SISTEMA EXPERTO
# FUNCIÓN: ASISTENTE TECNICO PARA S.O. GNU/LINUX BASADOS EN DEBIAN
# NOMBRE CODIGO: MIAPP (MIAPP-LPI-SB 1.0+0)
# PAIS ORIGEN: Mi país
# CREADO POR: Mi Nombre
# LICENCIA: Licencia Pública General de GNU 3.

###############################################################################
# INICIO DEL MODULO DE VALIDACION PERMISO DE SUPERUSUARIO (ROOT) SOBRE EL MIAPP-LPI-SB              
###############################################################################

# ESTE MODULO VALIDA QUE SOLO EL SUPERUSUARIO (USUARIO ROOT) PUEDA EJECUTAR
# EL LINUX POST INSTALL - SCRIPT BICENTENARIO.

clear

setterm -background red

if [[ "$(id -u)" != "0" ]]; then
   echo "ESTE SCRIPT DEBE SER EJECUTADO COMO ROOT"
   sleep 3
   clear      
  else
   echo "ESTE SCRIPT SERA EJECUTADO COMO SUPERUSUARIO (ROOT)"
   sleep 3
   clear
fi

###############################################################################
# FINAL DEL MODULO DE VALIDACION PERMISO DE SUPERUSUARIO (ROOT) SOBRE EL MIAPP-LPI-SB
###############################################################################


###############################################################################
# INICIO DEL MODULO DE ORDENES DE COMANDO DE POST INSTALACIÓN
###############################################################################

rm -f $HOME/mi_app/mi_app.desktop
rm -f $HOME/.local/share/applications/mi_app.desktop
rm -f $HOME/Desktop/mi_app.desktop
rm -f $HOME/Escritorio/mi_app.desktop
rm -f /usr/share/applications/mi_app.desktop

###############################################################################

echo "
[Desktop Entry]
Name=My Applicattions
GenericName=My Applicattions
GenericName[es]=Mi Aplicación
Comment=Mi Aplicación
Exec=/opt/mi_app/mi_app
Icon=`echo $HOME`/mi_app/icono_app.png
Terminal=false
Type=Application
Encoding=UTF-8
Categories=Network;Application;
MimeType=x-scheme-handler/mozilla;
X-KDE-Protocols=mozilla
" > `echo $HOME`/mi_app/mi_app.desktop

chown $USER:$USER -R `echo $HOME`/mi_app/mi_app.desktop

chmod 755 `echo $HOME`/mi_app/mi_app.desktop

ln -s `echo $HOME`/mi_app/mi_app.desktop $HOME/.local/share/applications/mi_app.desktop
ln -s `echo $HOME`/mi_app/mi_app.desktop $HOME/Desktop/mi_app.desktop
ln -s `echo $HOME`/mi_app/mi_app.desktop $HOME/Escritorio/mi_app.desktop
ln -s `echo $HOME`/mi_app/mi_app.desktop /usr/share/applications/mi_app.desktop

update-menus

###############################################################################

clear

su - $USER -c "xdg-open 'https://www.mi-app.com/'"

clear

echo ''
echo ''
echo '#------------------------------------------------------------------#'
echo '# GRACIAS POR USAR MI-APP LINUX POST INSTALL #'
echo '#------------------------------------------------------------------#'
echo ''
echo ''

sleep 3

###############################################################################
# FINAL DEL MODULO DE ORDENES DE COMANDO DE POST INSTALACIÓN
###############################################################################
Script content on Mousepad

Script content on Mousepad

Comments

As you can analyze from the code and omitting the header, the following script performs the following operations in an automated way:

  1. Validate that you are being used as super-user root or super-user root permission.
  2. Delete previous links in the application that have been created by a previous version.
  3. Create the new .desktop file (link) of the application in its own folder within the user's home.
  4. Assign the user owner permission to the created file.
  5. Give the created file the appropriate read / write / execute permissions.
  6. Create the new symbolic links to the necessary routes.
  7. Update the start menu to display the created .desktop file.
  8. Execute a specific URL on the created application.

Recommendation

It is recommended that this file be created within the folder that contains the application / executable in question with its respective icon (image / logo) specified within it and that said folder is located in the path /opt instead of the route `echo $HOME`/mi_app/ it decir, /home/mi_usuario/mi_app.

I hope this little script allows you to solve your needs in this regard on this subject! Until the next article.

While I leave you this link on the official Gnome website on the topic and this video: