How to Execute an Arbitrary Script with a Working Directory of The Directory Its In

How do I execute an arbitrary script with a working directory of the directory its in?

/etc/init.d

probably you are runnig (starting) that script from /etc/init.d?

Add cd /opt/script at the first line of the script

OR

...to keep it dynamic, add:
cd "$(dirname "$0")"

How do I run a program with a different working directory from current, from Linux shell?

Call the program like this:

(cd /c; /a/helloworld)

The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to /c, then executes helloworld from /a. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.

Error handling: To avoid running the program without having changed the directory, e.g. when having misspelled /c, make the execution of helloworld conditional:

(cd /c && /a/helloworld)

Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call helloworld via exec:

(cd /c && exec /a/helloworld)

[Thanks to Josh and Juliano for giving tips on improving this answer!]

how do you call shell scripts from an arbitrary directory?

In crontab, simply use:

(cd /home && php /home/testfile.php >> /home/logFile.txt)

As for referencing your own script with cron, just use its path, like so:

* * * * * /home/myscript.sh

Remember to set the executable bit first with

chmod +x /home/myscript.sh

How to run a shell script when a file or directory changes?

Use inotify-tools.

The linked Github page has a number of examples; here is one of them.

#!/bin/sh

cwd=$(pwd)

inotifywait -mr \
--timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
-e close_write /tmp/test |
while read -r date time dir file; do
changed_abs=${dir}${file}
changed_rel=${changed_abs#"$cwd"/}

rsync --progress --relative -vrae 'ssh -p 22' "$changed_rel" \
usernam@example.com:/backup/root/dir && \
echo "At ${time} on ${date}, file $changed_abs was backed up via rsync" >&2
done

Run N Shell Scripts in Folder

find the scripts, get the head, then execute with xargs.

find . -name '*.sh' | head -n 10 | xargs -n1 sh

You can run the scripts in parallel with xargs with a simple -P0 option. You can script the xargs with some xargs sh -c 'bash "$@" -H || exit 125' -- to make xargs exit with nonzero status or immediately after any of the scripts fail to run or something.

If you feel like unfamiliar with xargs, just do a simple while read loop:

find . -name '*.sh' | head -n 10 | 
while IFS= read -r script; do
bash "$script" -H || break
done

And in parallel you have to get out of the pipe subshell:

while IFS= read -r script; do
bash "$script" -H || break &
done < <(
find . -name '*.sh' | head -n 10
)
wait # for all the childs

or wait for childs in the subshell itself:

find . -name '*.sh' | head -n 10 |
{
while IFS= read -r script; do
bash "$script" -H || break &
done
wait
}

How to create a directory within a directory using shell scripting?

This can be done in a single step, since mkdir -p will create all needed parent directories on the way (that's what the -p option does).

#!/bin/bash
IFS=/
mkdir -p "$*"

Explanation: $* expands to all of the script's arguments, spliced together into a single string, separated by the first character of IFS. That's normally a space character, but here it's set to "/" instead. The double-quotes around it prevent unexpected word-splitting or wildcard expansion.

So, if you run direct.sh dir1 dir2 dir3, it executes mkdir -p "dir1/dir2/dir3", which creates all 3 directory levels (or at least, those that don't already exist).

BTW, in general you should set IFS back to normal after changing it like this. But since there's nothing afterward in the script that might get messed up, and changing it in a script doesn't affect the parent process (the shell you used to run the script), there's no need to set it back in this case.

Perform an action in every sub-directory using Bash

for D in `find . -type d`
do
//Do whatever you need with D
done


Related Topics



Leave a reply



Submit