B
B
BottleneckPC2021-01-17 13:40:23
linux
BottleneckPC, 2021-01-17 13:40:23

How to execute a script in the same place where it is located?

I do through the task scheduler (crontab) the launch of the script when it is turned on. the script is located in /home/andrey/mine/Vanila/bar/start.sh. When executing, I noticed that it does all operations with files in home / andrey. I can not understand how to tell him to execute the program in the same place where the script is located.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
X
xibir, 2021-01-17
@xibir

at the beginning of the script, you can write
cd `dirname $0`

H
HighMan, 2021-01-17
@HighMan

as an option: all actions with the files participating in the script should be carried out using absolute, not relative paths:

# cp file.txt /mnt
cp /home/andrey/mine/Vanila/bar/file.txt /mnt

Then it doesn't matter where the script is located, and it's clearer.

S
Sergey, 2021-01-17
@KingstonKMS

The simplest is to combine two commands and write in the cron line: cd /home/andrey/mine/Vanila/bar/ && /home/andrey/mine/Vanila/bar/start.sh

S
Sanes, 2021-01-17
@Sanes

./start.sh

S
Sergey Sokolov, 2021-01-17
@sergiks

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
there $DIRwill be a folder where the script file is located.
Source

V
Victor Taran, 2021-01-18
@shambler81

cd /home/test && ./scrypt.sh

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question