How do I call a shell script which calls python scripts from a different folder? - shell

Right now, I have shellScript.sh and test_me.py in a folder ABC/def. shellScript.sh calls test_me.py. I'm trying to call shellScript.sh from the ABC folder. So far, I keep on getting "No such file or directory" errors.
I've tried calling the python script from the shell script such as:
python /ABC/def/test_me.py
but this still gives me the same error.
How do I fix this?

Make sure your home directory starts by a capitalize H, if it does not (which is highly probable) your script won't work.

If the problem is not in the path or filename, then it could be in the way you are executing the shellScript.sh:
./shellScript.sh
might fix it provided you have the proper execute bit set on the script file. If not, try this:
sh /proper/path/to/shellScript.sh
If that does not fix it, then try to cd to the directory in the script before the python line:
cd /to/proper/folder
python test_me.py

Related

Change directory of bash script by default

I have a shell script in my local bin folder so I can run it anywhere. In this script, I perform search and replace commands using sed.
When I run that script, I set $PWD as argument of the script so the sed commands work on the files in the folder where I started the script and not in the bin folder.
What do I have to adapt such that my script is always in the path I am calling from without using the workaround applying $PWD as argument?
Many thanks in advance!
As written by Aaron:
You don't need to, it'll do that by default. What you need to be careful about is to avoid paths relative to the script's location : those will need to be made absolute (or will require a cd) to work when you're calling the script from another location

trying to write a script to run a command in a deep directory on entry to ssh

So I'm trying to write a script that will let me run a command to initialize some things. To be more specific, let's say I start in my home directory but to run this command I want I must be in a directory three folders deep into the home directory.
My script looks generically like this.
#!/bin/sh
cd home/path/to/final/directory/
command
Now usually, when I cd to this directory I can run the command on the command line and everything works fine.
When I tried to use a script to do it, the command line throws an error saying that that command isn't recognized like the computer doesn't know where to look.
The temporary fix I used was making a symbolic link to the directory I wanted but I was hoping someone could help me so that when I ssh to this node this script can be run immediately so I will not have to go into the deep directory, run the command and leave again.
Try defining full paths, for example:
#!/bin/sh
cd $HOME/path/to/final/directory && /path/to/your/command
In this case, it will try to cd into your defined directory but if it can't find the dir it will not run the command, this because of the &&
To test before running the command you could do a ls, for example:
cd $HOME/path/to/final/directory && ls

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 do I run bash shell scripts in cygwin?

I have a file called butcher and at the top of this file I put #!/bin/bash. The I changed permissions like so chmod 777 butcher. however when I try to run the script by typing butcher, I get this error: -bash: butcher: command not found. I'm not sure how to fix it, please help! Thank you in advance! :D
The file would have to be on your $PATH for your system to find it and execute it. Otherwise, the entire path to the file would have to be specified.
To show your path try:
echo $PATH
To run your script, either put it in one of those locations (such as /usr/local/bin), or go to the directory butcher is located in and run ./butcher.

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.

Resources