How to source a file via shell script - bash

I wrote a shell script where I copy my .bashrc file as well as custom dotfiles to a backup folder and then replace them in my home folder with another .bashrc file which will then source my custom dotfiles.
However, after the script does its job, if I try to execute the aliases I included in the new files I get the error No command found. Only after I source the .bashrc file manually in the terminal I have access to them.
From what I understand, the script I'm running is executing in a sub-shell (?) which will terminate on execution.
How can I run the script and have new commands/aliases/functions available without having to source the .bashrc file myself or restarting the terminal?

Well, it appears that instead of running my script via sh script.sh, I can source it like source script.sh, which will behave exactly as I wanted.
Solution

Related

Script for copying a folder somewhere using Git Bash

I want to copy a folder ~/Projects/LocalProject onto my server //VM-Server/ServerProject.
I know that I can use GitBash:
cp -r directory-name-1 directory-name-2
But what I'm curious about is, can I create a script to do that by double clicking that script, or adding it as a command to my GitBash, cause I will need that alot?
--Edit--
Tried nothing, as I don't know how to do that. Yes there are hidden files, I don't want them to be copied. There shouldn't be newer files on the destination. I need to manually run it, I thought that's clear as I mentioned the option to have a executable script / or a terminal command.
Option 1: Batch file
You don't even need git-bash; you can make a batch file in any text editor, name it copy to server.bat, and type in cp C:\Users\<Your username>\Projects\LocalProject \\VM-Server\ServerProject.
You can also make a .sh file for use in bash. The command is the same, just make note that Windows uses \, while bash uses / for directory tree
Option 2: Alias
Open your bash_profile file (it's in your git bash install location).
Add a line at the end of the file that says alias copyToServer = 'cp ~/Projects/LocalProject //VM-Server/ServerProject'. Then close git-bash, reopen it and use the command by typing copyToServer as a bash command. (It doesn't need to be named copyToServer)

how to run script with simple command

I have project structure like this
project
|app
|script
inside script folder, there are files such as 'run'
run file content:
#!/bin/bash
npm start
I want to run the file 'run' while I'm at the root of my project by typing only command 'run'. How would you do this?
This is sh file. In order to execute sh file on linux this file has to be executable.
Make sure this file has X permission.
If there is no x permission on file simply execute the command
chmod +x run.sh
Then execute the file by typing
./run.sh
For windows you need to create .bat file.
I'm not quite sure what you want but assuming you need a way to execute a file from node.js, you can use child_process module and child_process.exec method to start any executable.
Assuming the run file in the script directory is executable (if not, run chmod +x script/run), it can be executed by running ./script/run.
If you want to avoid having to type the name of the directory (script), you could append the script directory to your PATH environment variable. If you’re running a POSIX compatible shell (not csh or tcsh), this can be done using:
export PATH="$PATH:/path/to/project/script"
This will allow you to run any executable command in the script directory without having to specify the name of the directory, e.g., run.
NB: be sure that there aren’t common command names in the script directory as these commands can be run from any directory (including outside the project directory) after it has been added to the PATH. That’s also why I suggest adding it to the end of the PATH (so it’s the last directory that’s searched for executable commands).

Running an executable by calling it in a .sh file

I am very new to bash and using .sh files. I am trying to run the program aescrypt by calling it in a .sh file as follows (aescrypt is in the same directory as the .sh file) :
./aescrypt -e -p password file.txt
It throws the following error:
./aescrypt no such file or directory
Am I doing it wrong?
ps- I realy don't want to add it to the PATH variable as I will be using this on more than one computer that resets every day.
The location of the script is irrelevant. The thing that matters is the working directory of the process executing the script. The simplest solution really is to add aescrypt to a standard location like /bin or /usr/bin. If neither of those is acceptable, perhaps /usr/local/bin is an option. Otherwise, just use the full path of aescrypt in your script. Either hard code it, or if it is in the same directory as the script, try:
$(dirname $0)/aescrypt ...
(Note that hardcoding is more reliable, but less flexible. If you move the executable, the script will break. But using dirname will break if the script changes directory during execution.)
how about if you call the program like ./aescrypt.sh, thats the way to call an .sh programm througt the terminal
First off all, you have also to change the permissions of the file to make it executable, the way to make that is to write in the terminal, the command:
sudo chmod 765 aescrypt.sh
For that you have to be located where the file is

How to run Matlab from the command line?

Is it possible to run Matlab commands from Mac command line?
The matlab script is in the bin subdirectory of the MATLAB app bundle. On my machine, this means I can run it like so:
/Applications/MATLAB_R2012a_Student.app/bin/matlab
If you want this bin directory on your path (so that you can just run matlab, mex, etc), edit or create a new text file called .bash_profile in the top level of your home directory with the following line:
export PATH=/Applications/MATLAB_R2012a_Student.app/bin:$PATH
Replacing the "MATLAB_R2012a_Student" part with the name of your actual MATLAB app bundle. This will not come into effect for currently open terminals, but newly opened terminals should work properly.
You need the full path to the MATLAB executable, and you can use the -r option to run a command in the MATLAB that you start, as per the doc here.
As already mentioned above you need to first edit your .bash_profile file by adding the following line (replace 'MATLAB_R2020b' with your MATLAB version)
export PATH=/Applications/MATLAB_R2020b.app/bin:$PATH
Then after restarting the terminal you can open MATLAB by inserting the command
/Applications/MATLAB_R2020b.app/bin/matlab
You can also run your .m scripts by defining the working directory folder and the executable scripts and their paths. You just need to add more commands to the above.
/Applications/MATLAB_R2020b.app/bin/matlab -r "addpath(genpath('{Your working directory folder path}')); cd {Your working directory folder path}; {Your script name}; {Your other script name}; quit;"
Please find a more detailed description of MATLAB command line arguments from:
https://www.mathworks.com/help/matlab/ref/matlabmacos.html#d122e801165

How to make a shell script global?

I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default
There are two ways to do it -
Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
Create a folder in your home directory called bin. (For your personal scripts)
cd ~ (Takes you to your home directory)
mkdir bin (create a bin folder)
vim .bash_profile (to set path environment variable)
export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
Now you can just type the name of your script and run it from anywhere you want.
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)
Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.
In mac operating system
Open bash ~/.bashrc file.
add path of your script in your bashrc file , using
export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
open new terminal window
Now whenever you will open your terminal your script will be loaded
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
The code to be placed in your .bashrc file:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
Hope I've helped someone!
Sincerely,
AnonymousX
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
Now execute the script by function call in the shell.
Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).
You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.
You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

Resources