navigate through folders using shell script - shell

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)

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!

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!

Very simple bash script not working

I'm making a series of bash scripts to remove to nuisance from manually typing out navigation commands into my cygwin terminal. He's one that navigates to my xampp /www/ directory:
#!/bin/bash
cd /cygdrive/c/xampp/htdocs/www
When I run it with the following command:
$ ./www.bat
I get the following error:
C:\Users\user>cd /cygdrive/c/xampp/htdocs/www
The system cannot find the path specified.
What's strange is that when I type that command out manually it navigates to the intended directory without issue. My first thought is that it's an issue with Cygwin's disk drive naming, but if that was an issue it would fail upon manual typing.
What gives?
The error you're getting is from the windows command line interpreter. It gets called because your script has the .bat extension. It should be called www.sh instead.
However, you can't do what you want with a script: a new process would be spawned to run your script, the new process would cd to your directory but at the end of the script the process would end and you'd be returned to the calling shell's process which would have the old current directory. You would need to source the script from bash (. /path/to/www.sh) so that it would run in the same process as the calling shell, but that would be overkill for what you want. Just add this to your .bashrc in your home directory (/home/<user>/.bashrc):
alias www='cd /cygdrive/c/xampp/htdocs/www'

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