I am writing a bash script to handle the installation of a repository, mostly it checks some basic dependencies and calls git clone often.
When running this script I want to perform a check to whether files have been installed or not.
git clone https://github.com/reticulatedpines/magiclantern_simplified.git
echo "Exit status: ($?)"
if [ $? -eq 0 ]; then
echo "Magic Lantern has been cloned into ${ML_PATH}."
else
exit 1
fi
The problem is that the repository will clone into whatever the present working directory is which is not what I want and if I try cd ~
to return to the home directory it actually sends me to the root shell:
root@debian:~# (repository installs here).
How can I avoid this?
cd
to the correct directory before executing the script. Most standard command act like this. If you know what the absolute path is to the working directory, you could use that in the script, e.g.cd ~theuser/the/dir
. I believe the missing piece of info in the question is where you tell us exactly how you run your script (via root's cron, or viasudo
, or in some other manner).[ $? -eq 0 ]
, you're checking the exit status of[
which will be successful ifecho
succeeded. Useif git...; then echo success; else echo "failure (status = $?"; fi
root
?root
is to make administrative tasks, not development, not compiling code. Only amake install
or equivalent as the last stage after building might need to be made asroot
if it needs to install files in system locations or more generally needs to make changes to the system.