Make .sh file to .exe file [duplicate] - bash

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.

Related

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!

navigate through folders using shell script

I am new to shell scripting. I have saved the script file as script_hdl in my home directory. From my home directory, I want to navigate using the script in the following order: cd ../../site/edu/ess/project/user/rark444
and then open a new tab from this new location in the terminal.
I used this as my script:
#!/bin/bash
alias script_hdl="cd ../../site/edu/ess/project/user/rark444"
I run the script like this
./script_hdl
But I don't see any response in the terminal. I feel I am missing something but I don't know what is it. Thanks in advance for your help.
You have two ways to change directory here.
Script
The first one is to write a script, in such a way that you can run other command after cd. It works without the alias command: let's say you remove it.
cd command is proper to the running process. When you execute your script, the following happen:
your shell spawns (forks as) a new shell process executing your code. The main process wait for its child to finish;
this new child process actually does change its own working directory with your cd command, then quits (it's over)
the original shell process stops waiting and prints the prompt again. But this process has not changed directory (only the child process did)
To perform what you want, (remove the alias command, then) call your script as follows:
source script_hdl
or with following shortcut:
. script_hdl
meaning that you want the instructions to run in the same shell process.
Alias
The second way to change directory is to use an alias. But you should not write your alias definition in a random script file, add it in your ~/.bashrc instead (this file is run each time you open a shell).
So:
alias script_hdl="cd ../../site/edu/ess/project/user/rark444"
to reload ~/.bashrc:
. ~/.bashrc
And then don't try to execute from the file, just launch your alias as if it was a normal command:
script_hdl
Looks like you are trying to set up an alias. You can do this by editing your .bash_profile file in your home directory (if it's not there you can create one and then run "source .bash_profile" after editing it) and make an entry like alias script_hdl='cd ../../site/edu/ess/project/user/rark444' and then run "script_hdl" from your terminal.
For more info on alias you can follow the link mentioned by Paul.
Make sure the spelling is correct as unix is case sensitive and that you have permissions. First try it on the command line to ensure that it works, if there is an error it will appear on the command line as sometimes scripts hide the errors and messages. If it works then copy the text to the script file and don't use alias.
Here is the correct usage of alias
https://en.wikipedia.org/wiki/Alias_(command)

On Mac OSX how can I associate a script file with Terminal so double clicking the icon launches Terminal?

I have a python script that I can run in the usual way using Terminal. For example, launch Terminal, cd to the directory, then type ./xxx.py where xxx.py is the name of the file that contains the script.
Now I want to make an icon on the desktop that launches Terminal and runs the python file when I double click it. How do I do that? I thought I could make a shell file with the cd and the launch command and then associate that shell file with Terminal. But I can't seem to associate it with Terminal.
Somebody said to name the shell file with the suffix '.command' but that causes it to launch Flash Builder. I don't know where that associate is set. I can't manually associate Terminal because I can't find it. It isn't in /Applications.
On your Desktop you coul create a file with the following content
#!/bin/bash
python PATH_TO_YOUR_PYTHON_FILE
Then you must make it executable via
chmod u+x FILE_ON_DESKTOP
Alternatively you can create a symbolic link to your python file and make it executable.
You can find Terminal in /Applications/Utilities

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