Using a shell script to call a ROS launch file - bash

I want create a file *.sh and run it as an executable like any other software with double click on it. The command is the follwing
roslaunch my_pro test_qt.launch
I simply run it in the terminal and it launches my software. Now I want to make it as an executable, so I tried this but it didn't work
gnome-terminal -e roslaunch my_pro test_qt.launch &
it said:
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.#
# Use “-- ” to terminate the options and put the command line to execute after it.#
How can I write my *.sh file?
EDIT
Something like that?
#!/bin/bash
gnome-terminal -e
roslaunch my_pro test_qt.launch

I know this post is old, but i recently wanted to do a similar thing (roslaunch using desktop shortcut ) and gave me some trouble the past few days so I decided to write the steps i took for anyone that might be interested in the future.
First of, I created a hidden folder in my home directory to place the script and image I was going to use for the shortcut:
mkdir .dir_name
I then placed the image I wanted to use in there and created a script using the sublime text editor ( my choice, you can use your favourite one - it makes absolutely no difference at all )
subl .dir_name/launch.sh
The launch file looks like this:
#!/usr/bin/env bash
# Launch the robot
source /opt/ros/melodic/setup.bash
source /home/user/ros_ws/devel/setup.bash
echo "Launching application, please wait!"
roslaunch your_pkg your_launch.launch
Important note: you MUST include the sourcing of the bash files, otherwise the roslaunch command is not recognised!
Important note 2: you MUST make the script executable
chmod +x .my_dir/launch.sh
The next step is to create the desktop shortcut:
subl ~/Desktop/my_shortcut.desktop
and adding the following inside the file what we just created:
[Desktop Entry]
Type=Application
Terminal=false $ true opens extra gnome-terminal
Name=Robot Launch
Icon=/home/user/.my_dir/logo.png
Exec=terminator -e "bash -c '/home/user/.my_dir/launch.sh;$SHELL'"
$Exec=gnome-terminal -e "bash -c '/home/user/.my_dir/launch.sh;$SHELL'"
Note: I am using terminator, if you are using the default gnome terminal, comment the terminator line and uncomment the gnome-terminal line.
Finally, you need to right click the desktop shortcut ( icon will appear after it has been run once ) and go to permissions tab. There you must click
allow executing file as program
Once the shortcut is now double clicked it will run. The very first time you double click the system will ask you again if you want to execute this file (for security reasons) and you must click yes ( after all its your file ;) )
That's it! Enjoy your desktop shortcut launching ros code!

Related

Make .sh file to .exe file [duplicate]

First off I'm using Mac.
Next, I need to execute this "file.sh" we will call it. Everytime I need to execute it I have to open Terminal and type:
cd /Users/Jacob/Documents/folderWithFileInIt
bash file.sh
This is okay, but I feel like it would be a lot quicker if I make the file execute on double click, don't you think?
So my question is, how do I make this file executable via double click?
My ideas were either:
a) type something like chmod into terminal and change permissions?
b) make a file, put code I wrote above in it ^ and then make that file executable?
c) make an automation somehow to do this?
Which way is best, or is there an even better way?
By default, *.sh files are opened in a text editor (Xcode or TextEdit). To create a shell script that will execute in Terminal when you open it, name it with the “command” extension, e.g., file.command. By default, these are sent to Terminal, which will execute the file as a shell script.
You will also need to ensure the file is executable, e.g.:
chmod +x file.command
Without this, Terminal will refuse to execute it.
Note that the script does not have to begin with a #! prefix in this specific scenario, because Terminal specifically arranges to execute it with your default shell. (Of course, you can add a #! line if you want to customize which shell is used or if you want to ensure that you can execute it from the command line while using a different shell.)
Also note that Terminal executes the shell script without changing the working directory. You’ll need to begin your script with a cd command if you actually need it to run with a particular working directory. E.g. you can use cd "$(dirname "$0")" to set the current working directory to the directory where your shell script lies.
Remove the extension altogether and then double-click it. Most system shell scripts are like this. As long as it has a shebang it will work.
You can just tell Finder to open the .sh file in Terminal:
Select the file
Get Info (cmd-i) on it
In the "Open with" section, choose "Other…" in the popup menu
Choose Terminal as the application
This will have the exact same effect as renaming it to .command except… you don't have to rename it :)
Launch Terminal
Type -> nano fileName
Paste Batch file content and save it
Type -> chmod +x fileName
It will create exe file now you can double click and it.
File name should in under double quotes.
Since i am using Mac->In my case content of batch file is
cd /Users/yourName/Documents/SeleniumServer
java -jar selenium-server-standalone-3.3.1.jar -role hub
It will work for sure
you can change the file executable by using chmod like this
chmod 755 file.sh
and use this command for execute
./file.sh
nano ~/FILENAME
Write your bash script and exit nano with Ctrl + x and hit y
Make file an executable
chmod 700 ~/FILENAME
Bingo, the file turns an executable, double click to launch
Works without .sh extension or shebang (#!) prefix.

How to run Script per shortcut

I have a Terminal cmd running that executes a function from the .bash_profile file (as described here).
From the description, I added the function to the ~/.bash_profile (can be found here /home/user/.bash_profile)
From my Terminal everything works !
But how do I create a shortcut in Mac OS that executes this Terminal cmd ??
I tried Apple Automator (--> but error: Cmd not found)
I tried Alfred (--> but same error: Cmd not found)
How can I make this Terminal cmd execute from a keyboard-shortcut ??
Do I need to place the script-functions in another file (other than bash_profile ?) - or what is here to do ?
Here is an image of the Automator trial:
Place the functions in separate files, such as suspend-script.sh, resume-script.sh, and kill-script.sh; put them in a common directory like /home/user/scripts. To keep using them in your .bash_history, do:
# in .bash_history
source "suspend-script.sh"
source "resume-script.sh"
source "kill-script.sh"
Make sure you set the executable bit on each script (chmod +x /home/user/scripts/*.sh). Then, use the following for shortcut keybindings:
/home/user/scripts/suspend-script.sh thing_to_suspend
/home/user/scripts/resume-script.sh thing_to_resume
/home/user/scripts/kill-script.sh thing_to_kill
Since it's a shortcut to a function, you have to pick one script to suspend/resume/kill (the thing* above).
For example, if you wanted to suspend/resume/kill the top process, you would use the following:
/home/user/scripts/suspend-script.sh top
/home/user/scripts/resume-script.sh top
/home/user/scripts/kill-script.sh top
Open Automator, choose Application, add a Run Shell Script action and put in your Shell command between quotes (if you have a file, you can just drag and drop it).
Other than playing it, now you can save it (as an app anywhere) and even set the icon. Then you got an app and then you can define a keyboard shortcut for it in system-preferences.app
This should work, at least it does for me.
make sure your script is executable: sudo chmod u+x <your-scriptname>
and also add the filename extension .command to your filename. So the name of the script should be filename.command.
When you then click the script in Finder, it gets executed.
I hope that works!

How do I run a script on Linux just by double clicking it

I have a script written (which are basically the commands for the terminal for Ubuntu) on a file. Yes, the code starts with
#!/bin/bash
How do I run this script just by double clicking it? It can be run using the terminal but I want to make it more accessible through my desktop.
I was just trying to mimic a *.bat file on windows. A *.bat file on windows contains series of scripts operable on command prompt and executable just by double clicking on it.
Follow these steps:
Hit Alt+F2, type dconf-editor and hit ``Enter.
In dconfg-editor goto: org ➤ gnome ➤ nautilus ➤ preferences
Click on executable-text-activation and from drop down menu select:
launch: to launch scripts as programs.
OR
ask: to ask what to do via a dialog.
Close dconf-editor Thats it!
Source https://askubuntu.com/a/286651/113065
Edit -> preferences -> Behavior -> Executable Text Files = Run ...
It should run, but you can't see output.
An alternative way, mostly for developers, is to create self-contained desktop launchers. The idea is to use a launcher that includes a tag under which there is the script; the launcher executes a command that:
put the script into a temporary file
run the script which deletes itself
Of course, the script should delete itself, for this reason, it's a good practice to start the script with rm $0. Note that if the script needs to read the script itself, you will need to use rm $0 somewhere else...
You can use the following example stolen from my project:
#!/usr/bin/env xdg-open
[Desktop Entry]
Name=Launch Assistance
Comment=A simple app to setup remote assistance
Exec=ttt=/tmp/$(date +"%s%N").sh; tail -n+$(awk '/^#__SCRIPT__#/ {print NR + 1; exit 0; }' %k) %k > $ttt ; sh $ttt;
Icon=computer
Terminal=true
Type=Application
Categories=Network;Internet;Utilities;Administration;Settings;
#__SCRIPT__#
rm "$0"
# Put here the script
# note that if the script needs to read $0, you will have to edit it
You need to make it an executable file, either use chmod +x <filename> or go into the file properties and set it there.
Open nautilus
Behavior tab
Executable Text files -> Ask each time
Profit
Source. Tested on Ubuntu 20.04.

Mac execute bash script

I'm on a Mac, and have a bash script that works very nicely.
I'd like to make it so that a double-click will run it, but I don't know the "open with" operand. Please, what am I missing?
You'll need to make the file an executable.
On the first line, before any of your code put in a shebang
#!/usr/bin/env bash
REST OF YOUR CODE HERE
Next, you'll need to change the permissions. On the terminal run:
chmod +x your_bash_file
Finally, you will need to make sure OS X opens the file using the Terminal and not the application that created the file e.g. your favourite text editor. You can accomplish this in 1 of two ways:
Save the file with no file extension (eg. bash_file, instead of bash_file.sh)
Or, choose File -> Get Info and set Open with: to Terminal.app
You should now be able to click on the script to execute it!

How do I make this file.sh executable via double click?

First off I'm using Mac.
Next, I need to execute this "file.sh" we will call it. Everytime I need to execute it I have to open Terminal and type:
cd /Users/Jacob/Documents/folderWithFileInIt
bash file.sh
This is okay, but I feel like it would be a lot quicker if I make the file execute on double click, don't you think?
So my question is, how do I make this file executable via double click?
My ideas were either:
a) type something like chmod into terminal and change permissions?
b) make a file, put code I wrote above in it ^ and then make that file executable?
c) make an automation somehow to do this?
Which way is best, or is there an even better way?
By default, *.sh files are opened in a text editor (Xcode or TextEdit). To create a shell script that will execute in Terminal when you open it, name it with the “command” extension, e.g., file.command. By default, these are sent to Terminal, which will execute the file as a shell script.
You will also need to ensure the file is executable, e.g.:
chmod +x file.command
Without this, Terminal will refuse to execute it.
Note that the script does not have to begin with a #! prefix in this specific scenario, because Terminal specifically arranges to execute it with your default shell. (Of course, you can add a #! line if you want to customize which shell is used or if you want to ensure that you can execute it from the command line while using a different shell.)
Also note that Terminal executes the shell script without changing the working directory. You’ll need to begin your script with a cd command if you actually need it to run with a particular working directory. E.g. you can use cd "$(dirname "$0")" to set the current working directory to the directory where your shell script lies.
Remove the extension altogether and then double-click it. Most system shell scripts are like this. As long as it has a shebang it will work.
You can just tell Finder to open the .sh file in Terminal:
Select the file
Get Info (cmd-i) on it
In the "Open with" section, choose "Other…" in the popup menu
Choose Terminal as the application
This will have the exact same effect as renaming it to .command except… you don't have to rename it :)
Launch Terminal
Type -> nano fileName
Paste Batch file content and save it
Type -> chmod +x fileName
It will create exe file now you can double click and it.
File name should in under double quotes.
Since i am using Mac->In my case content of batch file is
cd /Users/yourName/Documents/SeleniumServer
java -jar selenium-server-standalone-3.3.1.jar -role hub
It will work for sure
you can change the file executable by using chmod like this
chmod 755 file.sh
and use this command for execute
./file.sh
nano ~/FILENAME
Write your bash script and exit nano with Ctrl + x and hit y
Make file an executable
chmod 700 ~/FILENAME
Bingo, the file turns an executable, double click to launch
Works without .sh extension or shebang (#!) prefix.

Resources