10

I am new in Ubuntu. I have an application which I open in the following way. I type in the console:

cd ~/MyDirectory
./myapp +some arguments

How can I find a solution, so I could launch my application without typing these commands in the console every time? I am thinking of a script, like a bat-script or .lnk in windows.

In other similar questions I didn't find a solution because there was only some mention and discussion of scripting. I didn't find how I can use "cd" command in other questions, and this question is not duplicate of others I suggest.

nick
  • 190
  • @steeldriver, no, i didn't found solution at this question. there are no anything about cd command and running app via script. – nick Apr 01 '19 at 13:35
  • #1 Do you want the solution to be an icon you can click on your desktop, or will this be something which you need/want to run from the command prompt? #2 How often will the argument change? – RonJohn Apr 01 '19 at 22:23
  • The conversation (safely in this case) assumes there is a desktop. Quite often in the scripting world there will not be. – mckenzm Apr 02 '19 at 00:22

3 Answers3

21

A script is quite overkill.

Use a .desktop file like:

[Desktop Entry]
Exec=/bin/bash -c "cd ~/MyDirectory && myapp some_arguments"
Name=Some App
Type=Application
  • Save it as some_app.desktop
  • Make it executable and double click

N.B.

The question is if it needs to be run from its own directory or not. If not, the command could even be simpler:

Exec='/home/MyUserName/MyDirectory/myapp' some_arguments
Jacob Vlijm
  • 83,767
  • 2
    Really thank you. .desktop file is enough for this task. – nick Apr 01 '19 at 13:52
  • 2
    @allo If OP asks for a solution, which isn't the optimal one for his problem, but he isn't aware a better one exists, you should give him the better solution for his problem if you know one. If you don't see that you are quite missing the point on what an answer should be here. – Jacob Vlijm Apr 01 '19 at 15:34
  • 1
    @dessert Thanks for the edit. We might have to deal with paths with spaces :) – Jacob Vlijm Apr 01 '19 at 16:27
  • 1
    You might want to write ./myapp … instead of just myapp … because the current directory, ., by default is not part of the PATH variable. – PerlDuck Apr 02 '19 at 09:42
  • 1
    @allo Please feel free to add your own answer if you think the existing ones are not sufficient. – Fabby Apr 02 '19 at 18:01
  • @JacobVlijm can you look at https://askubuntu.com/questions/1130775/open-any-file-with-specific-program if you have time? – DK Bose Apr 03 '19 at 04:25
  • 1
    @DKBose quite frankly, I don't get his issue, but he is using terrible/deprecated code :) – Jacob Vlijm Apr 03 '19 at 06:13
  • @fabby The answer from leonid is sufficient, so no need to add another answer. And personally I think this answer is an elegant solution, but the answer from leonid answers the actual question. – allo Apr 03 '19 at 08:24
  • The discussion is moot now: the OP decided that this was the actual answer to his question. 0:-) @allo – Fabby Apr 03 '19 at 11:17
14

Create a file with following content:

#!/bin/bash
cd ~/MyDirectory
./myapp +some arguments

Then make it executable:

chmod u+x scriptname

Now you can call this script like this: /pathtoscript/scriptname

You can combine this with answer by v010dya, and put this script in any bin that is in the $PATH, so both calling methods are possible: in the shell in any directory type scriptname or use the desktop shortcut explained below. Just change /pathtoscript to the actual placement of the script.

Make a shortcut to the script, place it in ~/.local/share/applications/ and name it like somename.desktop with the following content:

[Desktop Entry]
Version=1.0
Name=Script
Comment=
Keywords=Script
Exec=/pathtoscript/scriptname
Terminal=false
X-MultipleArgs=true
Type=Application
Icon=preferences-system
Categories=GTK;Development;
StartupNotify=false

Then it will appear in the applications list

Fabby
  • 34,259
LeonidMew
  • 2,734
  • 1
  • 21
  • 38
  • 1
    Your answer is working, thank you. But really sorry, script is too hard, I didn't knew about it. And my answer was concretically in script but not in just finding any way of solution. I will +1 to you. – nick Apr 01 '19 at 13:54
1

Although you have already found a solution that you can double click on, there's a solution that makes it more trivial to run from command line:

Step 1 - Write a script

This is already covered by an answer of LeonidMew, i will copy it here:

#!/bin/bash
cd ~/MyDirectory
./myapp +some arguments

and then

chmod u+x scriptname

Step 2 - Make it easy to execute

You need to create a directory ~/bin and make bash search it for commands. So in terminal we write:

mkdir ~/bin
mv scriptname ~/bin
vim ~/.profile

Here i am doing everything in terminal, you can of course create a directory dir in your home directory and also move your script there via graphical tools that you are comfortable with. Also i am using vim as an editor, but it is a little difficult for beginners to use, i would advise you to learn, but you can just use a different editor (even graphical) to edit .profile inside your home directory, note that this file is hidden by default.

You need to add the following to an end of that file

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

If you are using vim you press i to start adding text, and then Esc to end adding. After that you input :wq to write file and exit. Or, as i have already said, use a more simple editor.

Step 3 - Enjoy

Now, it will not matter what directory you are in, you can always just type scriptname and press Enter. You can also place other scripts and applications that you want to be executable in ~/bin.

v010dya
  • 1,472
  • 1
    You wrote how to exit of vim, +1 Just kidding, nice answer. Your solution is useful. It still possible to create .desktop file, to run script from apps list or menu(depending on DE). After patching .profile logout/login is needed. Also home can be mounted with noexec option, in this case its needed to put script in /usr/local/bin/ (using sudo). – LeonidMew Apr 07 '19 at 09:14