2

I am fairly new to Ubuntu 14.04. I am doing a school project where I need to use navigation stacks. At this point the navigation works fine. My problem at this time is:
Before I can launch rviz and to do the actual navigation I have to execute the following commands:

sudo mknod -m 666 /dev/ttyS51 c 4 115  
sudo socat PTY,link=/dev/ttyS51, TCP4:192.168.0.60:10002  
sudo chmod a=r+w /dev/pts/*  

There are some extra commands I need to run, but I think you guys get the picture.
At this point I have to manually enter all these commands into the terminal. I have added them to a shell file which I can launch manually.

I would like to know if it is possible to execute this shell file from a .launch file. The same way a node is launched from within a .launch file.
It would make it a lot easier if I can add a piece of code to the existing launch file so that these commands and the rest of the nodes necessary for the navigation are executed when I input the following command:

roslaunch navigation2d_example move_base.launch

The name of my shell file is: mobi_mani.sh Code inside is:

#!/bin/sh  
sudo mknod -m 666 /dev/ttyS51 c 4 115  
sudo socat PTY,link=/dev/ttyS51, TCP4:192.168.0.60:10002  
sudo chmod a=r+w /dev/pts/* " 

Long story short.
How can I execute the commands stated in a .sh file from a .launch file?
Also, can I get feedback on the screen of my terminal? so I know what is going on.

Apologies for my English, I am Dutch.
Thanks in advance!

10
  • Ha Gijs, just curious, did you manage? please let me know. May 22, 2015 at 17:22
  • Thank you for the information, due to some holidays I have not been able to test it. Tuesday, Dutch time, will be the first possibility for me to test it out. Will keep you posted!
    – Gijs
    May 23, 2015 at 17:51
  • the desktop file works. I only have one unresolved issue: how can i get information back from the different commands? For example: after executing a command i expect to receive some text data telling me if the command is successful. How do I get this information into the terminal window after it launches?
    – Gijs
    May 26, 2015 at 12:20
  • Hi Gijs, try adding a line to the .desktop file: Terminal=true possibly you'll need to edit the terminal preferences to keep it open after the command ran. Let me know! May 26, 2015 at 12:29
  • Ha Gijs, did it work? May 27, 2015 at 8:56

1 Answer 1

2

You are referring to a .desktop file. These files can be used to run commands, applications etc.

The most basic version of a .desktopfile:

[Desktop Entry]
Name=Some Name
Exec=<command> 
Type=Application

In your case, you want to run a series of commands, using sudo. In that case you need:

[Desktop Entry]
Name=Some Name
Exec=/bin/bash -c "command1&&command2&&command3"
Type=Application
Terminal=true

You need to save the file with the extension .desktop, e.g. run_commands.desktop. To use these files from the desktop, make it executable.

Explanation

  • To use complicated commands in a .desktopfile, the Exec= line should look like:

    Exec=/bin/bash -c "command1&&command2&&command3"
    
  • to chain commands in a .desktop file, as if each command would be on a new line in a script, you have to use && between the commands.

  • If you use sudo in a command, you need to either set:

    Terminal=true
    

    so the commands will run in the terminal and ask you for the password, or you need to use gksu instead of sudo, so gksuwill ask you for your password. In that case, you would have to install gksu, which is not by default on your system:

    sudo apt-get install gksu
    

If the command is too complicated or too long to handle in a .desktop file

If your series of commands is too long to handle in a .desktop file, write them in a small script, like you did, and call the script from the .desktop file. The Exec= line in the .desktop file should then look like:

Exec=/bin/sh mobi_mani.sh

EDIT if you need to run commands in the terminal

You can also use the following to run commands from the terminal:
Set in the .desktopfile as Exec= line:

Exec=gnome-terminal -x /bin/bash -c "<command1>&&<command2>&&<command3>"

Then, assuming you use gnome-terminal, go to Preferences (gnome-terminal) > Profile Preferences > Title & Command, then from the dropdown list "When command it finished" > keep terminal open:

enter image description here

This practically cannot fail.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .