I wonder if there is a simpler way to execute a script in bash only if this script exists. What I want is equivalent to:
if [ -x $name ]
then
$name
fi
or
[ -x $name ] && $name
What I am looking for is something like
exec_if_exist $name
which eliminates repetition of the script name.
Is there a way to simplify this in bash? I do not want a function or "speculative" execution, which would give the command not found error.
Best
Just add the pidof line at the top of your Bash script, and you'll be sure that only one instance of your script can be running at a time.
There can be situations where we may want to validate the script syntactically prior to its execution. If so, we can invoke the noexec mode using the -n option. As a result, Bash will read the commands but not execute them.
The origin of != is the C family of programming languages, in which the exclamation point generally means "not". In bash, a ! at the start of a command will invert the exit status of the command, turning nonzero values to zero and zeroes to one.
-e: It returns True if any type of file exists. -c: It returns True if the character file exists. -r: It returns True if a readable file exists. –w: It returns True if a writable file exists.
type
did not seem to always work on OS X. What worked was
which -s command && command
Why not
exec_if_exist() {
test -x $1 && $1
}
And, the path may need to be considered when invoking $1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With