Running an executable by calling it in a .sh file - bash

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

Related

Executing a bash script from anywhere on Windows

I am on Windows.
I have a script file named basics.sh and here is what it contains:
cd opt-out-exam/abduvosid_malikov/IT
mkdir made_by_my_script
cd made_by_my_script
echo "Hello World" > hello.txt
so basically, basics.sh script file is responsible to:
go to folder opt-out-exam/abduvosid_malikov/IT
make a directory made_by_my_script
create hello.txt file with content Hello World
Right now. to execute this basics.sh script, I am going to IT folder and writing this command in the terminal:
./basics.sh
In order to execute this basics.sh script, is it compulsory for me to go to IT folder
OR
is it possible to execute this script file even if I am staying in another folder (lets say currently working directory is opt-out-exam)
The first line is a change directory command followed by a relative path, not absolute. In such cases, it is important where you run the script. (An absolute path would start with the filesystem root, i. e. /.)
If you run this script from a directory (I wouldn't call it a folder in this context) where the relative path opt-out-exam/abduvosid_malikov/IT does not exist, it won't cd into it. But it will make a new directory without any problem, it will also create the file and write a line into it.
So only the first line will fail if it's run somewhere else.
UPD: As Gordon Davisson pointed out, this means that you want to check whether the directory change actually took place or not.

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).

Why I am not able to run executables from shell script

I have a shell script which calls for different executables from it.
The shell script and the executables are within same directory and I am trying to run it from there. Still, on running, I get the error, "executable" not found- No file/directory exists.
What should I do???
First, You need to provide permission to the File :
chmod +x filename
Then, execute the binary file by,
./filename
The current directory is typically not in your command path, for security reasons. You need to provide the path explicitly, that is
./mycommand
instead of
mycommand
(Keep in mind, though, that this will break if you run the script from a different directly. ./mycommand is relative to the directory you run from, not the directory where the script is stored.)

Cd in shell script not working

First off I am very very new to shell scripting. I am trying to write a script that takes in one parameter and then copies a folder in a different directory naming it using the parameter. This is the current code that I have:
#!/bin/sh
cd /var/www/html/fbplugin/chrome
sudo mkdir temp/$1
sudo cp -rf "/var/www/html/fbplugin/chrome/fbplugin" "/var/www/html/fbplugin/chrome/temp/$1"
When I run this code it says can't cd to /var/www/html/fbplugin/chrome. I'm not sure why it is saying this because I know the directory exists. I have copied the line directly and it works in terminal. If anyone could help me out that would be great.
If it matters in order to run the script I am typing "sh build.sh"
If that directory really exists, then you must have executed that script with a different user (cron, webserver, etc).
Check the rights for that directory.
I don't know why you're getting the error about cd, but it looks like you could just use absolute paths throughout. That would solve the larger problem of the script working correctly.

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