I am writing a shell script which runs pwd command and uses its output for some others tasks performed by the script. The script is working totally fine when I go to the particular directory and run it. But when I am trying to run the script from some other location by giving the full path of the script, I am not getting the desired output as pwd command gives current directory's path. How can I solve this issue? How can I write something that will hold correct irrespective of where I run the script from?
The same issue is being faced when I am using ..to get to previous directory. I want the script to take path with respect to its location instead of path where the script is being run. Please let me know if there are some other details required.
You can use $0 to extract script and then combine it with pwd
$ cat abc.sh
echo $0
DIR1=$(dirname `pwd`/$0)
echo $DIR1
DIR2="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR2
There are other ways to achieve it in all cases:
Reliable way for a bash script to get the full path to itself?
Getting the source directory of a Bash script from within
Related
Why can't I run a bash script in the current directory I'm in?
Whenever I run the script the commands are executed in the home directory.
The only answers I found are included below.
I do use the zsh shell. I don't know if that changes anything.
Thanks in advance!
What I have tried so far:
#!/bin/bash
touch test.txt
#!/bin/bash
cd $PWD
touch test.txt
#!/bin/bash
variable = $PWD
cd $variable
touch test.txt
#!/bin/bash
variable= pwd
cd $variable
touch test.txt
#!/bin/bash
cd -
touch test.txt
If I run the script for example from /home/user/dir1/dir1.1 the test.txt file is created in the home directory (/home/user) and I get redirected to the home directory as well.
in bash there are two things to do:
ensure that the shell script file is saved properly and is chmod'd to be an executable.
To do so, save the file (e.g. script.sh) with the code you want, and then run chmod +x script.sh to make linux understand that this file is an executable.
call the executable properly using the ./script.sh command. alternatively, you can also call the script from remote folder by calling it using the absolute path the script is in (e.g. /folder/folder/folder/script.sh).
This should execute the file. from there, it's about your code and if you need help there, please update your question.
I tried multiple scenarios but these things are more of a headace. I tried writing this bash also for the same, but need to change in the same file which i need to execute. Can someone help me with the same.
#!/bin/bash -p
if [ $# -lt 1 ]; then
echo "No arguments provided"
exit 0
fi
CURRENT_DIR=`pwd`
EXEC_DIR=`dirname "$1"`
FILENAME="`basename $1`"
cd $EXEC_DIR
CMD="./$FILENAME ${#:2}"
$CMD
cd $CURRENT_DIR
i need some two liner to work with the same file which i need to execute.
If your script is present in /opt/ and you are in /opt/dir/ then you can run the script from there only like this --> ../script.sh it will run the script from previous folder and run it in present folder
The easiest way is to use the full path of the file/script you want to execute :
/path/to/file/example.bash
You will stay in your current directory, but your script example.bash will be executed.
I have 3 directories:
/A/B/C and 1 bash script in C directory.
How can I execute this bash script from A into in C directory.
I understand from your comments that you want your script to have its current working directory to be in A/B/C when it executes. Ok, so you go into directory A:
cd A
and then execute script.sh from there:
(cd B/C; ./script.sh)
What this does is start a subshell in which you first change to the directory you want your script to execute in and then executes the script. Putting it as a subshell prevents it from messing up the current directory of your interactive session.
If it is a long running script that you want to keep in the background you can also just add & at the end like any other command.
Whenever I want to make sure that a script can access files in its local folder, I throw this in near the top of the script:
# Ensure working directory is local to this script
cd "$(dirname "$0")"
It sounds like this is exactly what you're looking for!
Assuming /A/B/C/script.sh is the script, if you're in /A, then you'd type ./B/C/script.sh or bash B/C/script.sh or a full path, /A/B/C/script.sh. If what you mean is that you want that script always to work, then you'll want to add it to your PATH variable.
Go to A, then run your script by providing the path to it:
cd /A
bash B/C/somescript.sh
You could also add C to your PATH variable, making it runnable from anywhere
(i.e. somescript.sh, without the path)
If you want to access the directory the script is stored in, within the script, you can use this one-liner:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
or simply dirname. Taken from this thread. There are many more suggestions there.
The easy way to make your script execute within C is to simply be in that folder.
I want to execute statements like (shell is bash)
/# source /workspace/scripts/script.sh
/workspace/scripts# source script.sh
Inside the script.sh I want to get its own location. (script.sh wont be in the PATH variable)
I could do this using readlink -f $0 when run the script, but the same doesnt work when I source it.
I dont want the solution to be dependent on where I run the source command from. (otherwise pwd would have been enough)
Is this possible?
It is not possible to find the location reliably in 100% of all cases.
If you use bash your best bet is $BASH_SOURCE variable.
This link is very helpful on this topic.
Since script.sh is in your path, you should be able to get it's full path using which. So, in the script, if $0 isn't a full path, you can do which $0.
carabiner$ cat ~/bin/test.sh
#!/bin/sh
echo test - $0 $1
which $0
carabiner$ source test.sh
test - test.sh
test.sh is /home/zigdon/bin/test.sh
I have created a simple script:
echo "the path of the current directory is `pwd`"
and saved it by the name pathinfo
then i have created a bin directory at my home page with path as
/home/vpnsadmin/bin
and copied my script(pathinfo) to that bin directory.
Now i want run this script as a command but it is showing error
-bash: /usr/bin/test2: No such file or directory
but if copy my script(pathinfo) to "/usr/bin/" then it runs as a command.
the PATH environment variable is set as-
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/vpnsadmin/bin
My question is why does the shell not run it as a command when it is present in /home/vpnsadmin/bin.
or else
why does it only check for the binary at /usr/bin and not at /home/vpnsadmin/bin or at /bin
The shell that is to execute your command needs to have the correct PATH variable set at the time of execution and, depending on shell, might need to have created its own internal (hash)map of the available commands.
Assuming you are using bash, try the following with your script saved in /usr/bin:
$ PATH=/ test2
$ PATH=/usr/bin test2
In the first case you should get an expected "not found" error, in the second it should work. The third test to perform is left as an exercise...
And I have to say that the supplied error message looks a bit odd if you actually tried to do
$ test2
and not
$ /usr/bin/test2
before copying the command to /usr/bin.
Edit:
Also, avoid naming your scripts test, in any way shape or form. This causes so much confusion for beginners.
Hint:
man test
Did you have the path to bash at the top of your script and did you use backticks around pwd?
#!/bin/bash
echo "the path of the current directory is `pwd`"
Did you make the file executable?
chmod +x pathinfo
There is another script pathinfo somewhere in your path which contains a call to /usr/bin/test2
Try whereis pathinfo to see how many there are and which pathinfo to see which one your shell currently prefers.