Reputation: 661
I have 2 shell scripts and 2 mpkg installer, I am trying to use an unix excitable file to run them all. here is the script I have, but it always has error message "No such file or directory" ?
#!/bin/sh
# Find the absolute script current path
path=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
sudo sh $path/join.sh
sudo sh $path/join2.sh
#/usr/sbin/installer -dumplog -verbose -pkg $path/“esetv8.mpkg" -target /
#/usr/sbin/installer -dumplog -verbose -pkg $path/“sccm.mpkg” -target /
exit 0
Thanks so much!
Upvotes: 1
Views: 171
Reputation: 4551
The most common issue when handling variables containing paths of directories and files is the presence of special characters such as spaces. To handle those correctly, you should always quote the variables, using double quotes. Better code would therefor be:
sudo sh "$path/join.sh"
sudo sh "$path/join2.sh"
It is also advised to wrap the variables using curly braces, this can also help to avoid unwanted issues. Resulting in following code:
sudo sh "${path}/join.sh"
sudo sh "${path}/join2.sh"
While this should work, it's also appropriate to mention that it's advised to check whether the files actually exist before executing them. Checking a file for existence can be done using -f
and checking execute permission using -x
. The proper code is therefor:
[ -f "${path}/join.sh" ] && [ -x "${path}/join.sh" ] && sudo sh "${path}/join.sh"
[ -f "${path}/join2.sh" ] && [ -x "${path}/join2.sh" ] && sudo sh "${path}/join2.sh"
Note that if you have a bunch of these, you'd be better off executing them using a for loop. Note also that -f
becomes redundant when checking -x
so better code would be:
[ -x "${path}/join.sh" ] && sudo sh "${path}/join.sh"
[ -x "${path}/join2.sh" ] && sudo sh "${path}/join2.sh"
Upvotes: 1