I have written a bash script which creates a series of directories and clones a project to selected directories.
For that, I need to cd
to each directory (project 1
and project 2
), but the script doesn't cd
to the second directory nor executes the command.
Instead, it stops after cd
and cloning in theproject2
directory. Why doesn't it call the cd_project1
function in the following code?
#!/bin/bash
#Get the current user name
function my_user_name() {
current_user=$USER
echo " Current user is $current_user"
}
#Creating useful directories
function create_useful_directories() {
if [[ ! -d "$scratch" ]]; then
echo "creating relevant directory"
mkdir -p /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2
else
echo "scratch directory already exists"
:
fi
}
#Going to project2 and cloning
function cd_project2() {
cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2 &&
git clone https://[email protected]/teamsinspace/documentation-tests.git
exec bash
}
#Going to project1 directory and cloning
function cd_project1() {
cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/ &&
git clone https://[email protected]/teamsinspace/documentation-tests.git
exec bash
}
#Running the functions
function main() {
my_user_name
create_useful_directories
cd_project2
cd_project1
}
main
Terminal output:
~/Downloads$. ./bash_install_script.sh
Current user is mihi
creating relevant directory
Cloning into 'documentation-tests'...
remote: Counting objects: 125, done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 125 (delta 59), reused 0 (delta 0)
Receiving objects: 100% (125/125), 33.61 KiB | 362.00 KiB/s, done.
Resolving deltas: 100% (59/59), done.
~/Downloads/scratch/mihi/project1/project2$
The culprits are your
exec bash
statements in some of your functions. Theexec
statement is a bit weird and not easily understood in the first place. It means: execute the following command instead of the currently running command/shell/script from here on. That is: it replaces the current shell script (in your case) with an instance ofbash
and it never returns.You can try this out with a shell and issue
This will replace your current shell (the
bash
) with the commandsleep 5
and when that command returns (after 5 seconds) your window will close because the shell has been replaced withsleep 5
.Same with your script: If you put
exec something
into your script, the script gets replaced withsomething
and when thatsomething
stops execution, the whole script stops.Simply dropping the
exec bash
statements should do.From
help exec
:The key word here is replace - if you
exec bash
from inside a script, no further script execution can occur.if you want a return to the directory you started you could use
But if you are not sure whether a
cd
command was executed at all it would be better to use the commands for putting working directories onto a stack:and return to it (even after multiple directory changes)
be sure to have equaly
pushd
andpopd
commands.