executing a python code located in a subfolder - bash

I have a directory called NHOME. In this directory there is a folder named subFolder. It's like this:
/NHOME/subFolder
In NHOME directory I'm executing a bash script which intends to execute a python code located in subFolder. I have problem doing this. I tried something like this:
./subFolder/file.py
but it didn't work. Is this possible?

Use pwd to get the script's location instead of the relative ./
pythonScript="$PWD/subFolder/file.py"
To run:
#!/bin/bash
"$PWD"/subFolder/file.py
Note: The " around $PWD are needed if the path contains spaces ()

Related

Shell Script - opening files from a different directory

I am trying to cd to a different directory by using a path to open a file, but it fails to load correctly.
I am in the directory of ~/Dev/.../Examples to run a ./test.sh script.
Inside the shell script, I have the following:
#!/bin/bash
path = '~/Datasets/foo'
....
"path"/bar
The Dataset folder is right after cd ~/
I have tried $HOME instead of ~, but it still fails to load the respective file.
Here is a code to open the bar directory with nautilus (ubuntu's explorer):
#!/bin/bash
path='/home/your_user_name/foo'
nautilus $path/bar/
Rules:
Use absolute path: /home/your_user_name/, not ~/ nor $HOME.
Bash doesn't like spaces: path='~/Datasets/foo', and not path = '~/Datasets/foo'

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 do I get all the files with a particular extension inside a directory?

I need to get all the files with a particular extension inside a folder without having to cd into that folder in cmd.
Some thing similar to a path in bash: /path/to/my/directory/*.txt
I have tried this path in cmd: D:\path\to\my\directory*.txt
But it gives me No such file or directory.
Actually it works much like in bash (with dir instead of ls, and with backslashs):
dir "D:\path\to\my\directory\*.txt"
Don't forget the backslash in front of "*".

Calling a script within a script that was run via nohup

I have a script, which when I run to screen, works perfectly.
The directory structure is as follows:
/home/username/processing/ScriptRunning
/home/username/processing/functions/include_me
In the script, it opens another script, which contains a function by simply doing this:
#!/bin/bash
#This is ScriptRunning script
. functions/include_me
Now when I call the script using the following nohup command:
nohup /home/username/processing/ScriptRunning
this is the output:
/home/username/processing/ScriptRunning: line 3: /home/username/functions/include_me: No such file or directory
It seems to be missing out the processing directory
I've altered the line within the ScriptRunning to have a full path, both hardcoded to /home/username/processing and also having this as a variable created by calling $(pwd), but the error is the same.
Am I really missing something so stupid?
This isn't a nohup issue. You are including a source file using a relative file name. Try:
. $(dirname ${BASH_SOURCE})/functions/include_me
to include a source file located relative to ${BASH_SOURCE}

Script location execution

I've been working on a script but always with a fixed directory (/opt/mw/script).
I need to change that to be able to execute the script from any directory.
I think I will need to add a "." at the beginning of the line to be able to execute the script?
For example ./mw/script
Is this correct?
Thanks
You already can execute that script from any directory with that absolute file path. It's the relative file paths (that start with ./ or ../) that can only be executed from a specific directory.
You need to add the "." if the script is in the current directory (e.g. ./script), but it is optional if the script is already inside another directory (e.g. mw/script).
Also notice that if your script contains relative references to other files and directories, you may need to use this trick to properly refer to them from any directory.
For instance, consider the following script using absolute paths:
#!/bin/bash
# lists all files in this directory
ls /opt/mw
If it is converted to relative paths as follows:
#!/bin/bash
# lists all files in this directory
ls mw
Then this script will only work if you run it from its own directory (e.g. cd /opt/ && ./script).
But then you can generalize it like this:
#!/bin/bash
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# lists all files in this directory
ls $SCRIPT_DIR/mw
Now the script works even when executed from another directory.

Resources