Ubuntu – How to automate execution of commands in separate terminals

12.04command lineexecute-commandgnome-terminal

I am new to LINUX operating system. I am using ROS (Robotic Operating System) where I type few commands in separate terminal windows for doing the setup.

ie Before I run my program, I have to do (each in separate terminal):

~$ roscore 

~$ rosrun nodelet nodelet standalone
velodyne_pointcloud/CloudNodelet

~/Desktop/OpenCV$ source setup.bash 

~/Desktop/OpenCV$ rosrun pcl_tutorial
front_view_bag input:=/velodyne_points

/media/BE8C6D3A8C6CEDF9/Users/KARTHICK/Desktop/project/ros_datas$
rosbag play file2.bag

The above commands are a must before I can actually run my C++ program and it is to be in the above order, issued from the respective directories.

I am using Ubuntu 12.04.
Is it possible to write a program or something so that if I run that single program, all the above commands will be properly executed in separate terminals at the respective directories rather than manually executing them each time?

Best Answer

You can make a shell script, to do so, open a Terminal with Ctrl + Alt + T and type:

touch daily-script.sh

Now we need to edit this file contents

xdg-open daily-script.sh

Next copy paste the below code to your file:

#!/bin/bash

cd $HOME
xterm -hold -e "roscore" &
xterm -hold -e "rosrun nodelet nodelet standalone velodyne_pointcloud/CloudNodelet" &
cd $HOME/Desktop/OpenC
xterm -hold -e "source setup.bash; rosrun pcl_tutorial front_view_bag input:=/velodyne_points" &
cd /media/BE8C6D3A8C6CEDF9/Users/KARTHICK/Desktop/project/ros_datas
xterm -hold -e "rosbag play file2.bag"

exit 0

Once you save the file open a Terminal and type:

chmod +x daily-script.sh

Finally your file is executable, to run it:

./daily-script.sh