Why using two cd commands in bash script does not execute the second commandBash script: How to execute...
Make a transparent 448*448 image
How to simplify this time periods definition interface?
How could a female member of a species produce eggs unto death?
Happy pi day, everyone!
Brexit - No Deal Rejection
Professor being mistaken for a grad student
Good allowance savings plan?
How to explain that I do not want to visit a country due to personal safety concern?
Why is "das Weib" grammatically neuter?
PTIJ: Who should pay for Uber rides: the child or the parent?
Humanity loses the vast majority of its technology, information, and population in the year 2122. How long does it take to rebuild itself?
What does it mean to make a bootable LiveUSB?
Why is a Java array index expression evaluated before checking if the array reference expression is null?
Check this translation of Amores 1.3.26
Life insurance that covers only simultaneous/dual deaths
What is IP squat space
Can anyone tell me why this program fails?
Does this AnyDice function accurately calculate the number of ogres you make unconcious with three 4th-level castings of Sleep?
What do these Greek words say? Possibly 2nd century
Is it true that real estate prices mainly go up?
What is a good source for large tables on the properties of water?
How to make healing in an exploration game interesting
Replacing Windows 7 security updates with anti-virus?
How to generate globally unique ids for different tables of the same database?
Why using two cd commands in bash script does not execute the second command
Bash script: How to execute commands consecutively without waiting for the previous one?Why does “if [ 0 ]” execute the “then” statement in bash script?sudo bash can't execute scriptWhy does this bash script not work?Bash script doesn't execute command from PATHWhy won't this php script execute bash script?Bash script: command not foundScript for Opening Two Terminals and Execute Three Consecutive CommandsFIFO commands in bash scriptCreate bash script that allows you to choose multiple options instead of just one?
So I have written a bash script which create series of directories and clone a project to selected directories. But for that I need to cd to each directory (project 1 and project 2) but script doesn't cd to the second directory and execute the command. Instead, it stops after cd and cloning in theproject2 directory. Why it doesn't call the cd_project1 function in 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://username@bitbucket.org/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://username@bitbucket.org/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$
bash scripts cd-command
New contributor
Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
So I have written a bash script which create series of directories and clone a project to selected directories. But for that I need to cd to each directory (project 1 and project 2) but script doesn't cd to the second directory and execute the command. Instead, it stops after cd and cloning in theproject2 directory. Why it doesn't call the cd_project1 function in 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://username@bitbucket.org/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://username@bitbucket.org/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$
bash scripts cd-command
New contributor
Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.
– LeonidMew
9 mins ago
Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.
– Jenny
4 mins ago
add a comment |
So I have written a bash script which create series of directories and clone a project to selected directories. But for that I need to cd to each directory (project 1 and project 2) but script doesn't cd to the second directory and execute the command. Instead, it stops after cd and cloning in theproject2 directory. Why it doesn't call the cd_project1 function in 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://username@bitbucket.org/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://username@bitbucket.org/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$
bash scripts cd-command
New contributor
Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
So I have written a bash script which create series of directories and clone a project to selected directories. But for that I need to cd to each directory (project 1 and project 2) but script doesn't cd to the second directory and execute the command. Instead, it stops after cd and cloning in theproject2 directory. Why it doesn't call the cd_project1 function in 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://username@bitbucket.org/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://username@bitbucket.org/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$
bash scripts cd-command
bash scripts cd-command
New contributor
Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 41 mins ago
Jenny
New contributor
Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 46 mins ago
JennyJenny
112
112
New contributor
Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.
– LeonidMew
9 mins ago
Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.
– Jenny
4 mins ago
add a comment |
Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.
– LeonidMew
9 mins ago
Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.
– Jenny
4 mins ago
Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.
– LeonidMew
9 mins ago
Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.
– LeonidMew
9 mins ago
Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.
– Jenny
4 mins ago
Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.
– Jenny
4 mins ago
add a comment |
2 Answers
2
active
oldest
votes
From help exec:
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
The key word here is replace - if you exec bash from inside a script, no further script execution can occur.
Thanks! It works.
– Jenny
12 mins ago
add a comment |
The culprits are your exec bash statements in some of your functions.
The exec 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 of bash and it never returns.
You can try this out with a shell and issue
exec sleep 5
This will replace your current shell (the bash) with the command sleep 5
and when that command returns (after 5 seconds) your window will close because
the shell has been replaced with sleep 5.
Same with your script: If you put exec something into your script, the script
gets replaced with something and when that something stops execution, the
whole script stops.
Simply dropping the exec bash statements should do.
Oh thanks! it worked. Never thought it would be that simpler. Jenny :)
– Jenny
13 mins ago
@Jenny Nice to hear. Anecdote: The Perl language also has anexecstatement with the same behaviour and if you put some statements after anexecstatement (likeexec something; print "This won't run";) then Perl will warn you that theprintstatement will never get executed.
– PerlDuck
3 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Jenny is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1125755%2fwhy-using-two-cd-commands-in-bash-script-does-not-execute-the-second-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
From help exec:
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
The key word here is replace - if you exec bash from inside a script, no further script execution can occur.
Thanks! It works.
– Jenny
12 mins ago
add a comment |
From help exec:
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
The key word here is replace - if you exec bash from inside a script, no further script execution can occur.
Thanks! It works.
– Jenny
12 mins ago
add a comment |
From help exec:
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
The key word here is replace - if you exec bash from inside a script, no further script execution can occur.
From help exec:
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
The key word here is replace - if you exec bash from inside a script, no further script execution can occur.
answered 25 mins ago
steeldriversteeldriver
69.2k11114185
69.2k11114185
Thanks! It works.
– Jenny
12 mins ago
add a comment |
Thanks! It works.
– Jenny
12 mins ago
Thanks! It works.
– Jenny
12 mins ago
Thanks! It works.
– Jenny
12 mins ago
add a comment |
The culprits are your exec bash statements in some of your functions.
The exec 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 of bash and it never returns.
You can try this out with a shell and issue
exec sleep 5
This will replace your current shell (the bash) with the command sleep 5
and when that command returns (after 5 seconds) your window will close because
the shell has been replaced with sleep 5.
Same with your script: If you put exec something into your script, the script
gets replaced with something and when that something stops execution, the
whole script stops.
Simply dropping the exec bash statements should do.
Oh thanks! it worked. Never thought it would be that simpler. Jenny :)
– Jenny
13 mins ago
@Jenny Nice to hear. Anecdote: The Perl language also has anexecstatement with the same behaviour and if you put some statements after anexecstatement (likeexec something; print "This won't run";) then Perl will warn you that theprintstatement will never get executed.
– PerlDuck
3 mins ago
add a comment |
The culprits are your exec bash statements in some of your functions.
The exec 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 of bash and it never returns.
You can try this out with a shell and issue
exec sleep 5
This will replace your current shell (the bash) with the command sleep 5
and when that command returns (after 5 seconds) your window will close because
the shell has been replaced with sleep 5.
Same with your script: If you put exec something into your script, the script
gets replaced with something and when that something stops execution, the
whole script stops.
Simply dropping the exec bash statements should do.
Oh thanks! it worked. Never thought it would be that simpler. Jenny :)
– Jenny
13 mins ago
@Jenny Nice to hear. Anecdote: The Perl language also has anexecstatement with the same behaviour and if you put some statements after anexecstatement (likeexec something; print "This won't run";) then Perl will warn you that theprintstatement will never get executed.
– PerlDuck
3 mins ago
add a comment |
The culprits are your exec bash statements in some of your functions.
The exec 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 of bash and it never returns.
You can try this out with a shell and issue
exec sleep 5
This will replace your current shell (the bash) with the command sleep 5
and when that command returns (after 5 seconds) your window will close because
the shell has been replaced with sleep 5.
Same with your script: If you put exec something into your script, the script
gets replaced with something and when that something stops execution, the
whole script stops.
Simply dropping the exec bash statements should do.
The culprits are your exec bash statements in some of your functions.
The exec 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 of bash and it never returns.
You can try this out with a shell and issue
exec sleep 5
This will replace your current shell (the bash) with the command sleep 5
and when that command returns (after 5 seconds) your window will close because
the shell has been replaced with sleep 5.
Same with your script: If you put exec something into your script, the script
gets replaced with something and when that something stops execution, the
whole script stops.
Simply dropping the exec bash statements should do.
answered 18 mins ago
PerlDuckPerlDuck
6,92111535
6,92111535
Oh thanks! it worked. Never thought it would be that simpler. Jenny :)
– Jenny
13 mins ago
@Jenny Nice to hear. Anecdote: The Perl language also has anexecstatement with the same behaviour and if you put some statements after anexecstatement (likeexec something; print "This won't run";) then Perl will warn you that theprintstatement will never get executed.
– PerlDuck
3 mins ago
add a comment |
Oh thanks! it worked. Never thought it would be that simpler. Jenny :)
– Jenny
13 mins ago
@Jenny Nice to hear. Anecdote: The Perl language also has anexecstatement with the same behaviour and if you put some statements after anexecstatement (likeexec something; print "This won't run";) then Perl will warn you that theprintstatement will never get executed.
– PerlDuck
3 mins ago
Oh thanks! it worked. Never thought it would be that simpler. Jenny :)
– Jenny
13 mins ago
Oh thanks! it worked. Never thought it would be that simpler. Jenny :)
– Jenny
13 mins ago
@Jenny Nice to hear. Anecdote: The Perl language also has an
exec statement with the same behaviour and if you put some statements after an exec statement (like exec something; print "This won't run";) then Perl will warn you that the print statement will never get executed.– PerlDuck
3 mins ago
@Jenny Nice to hear. Anecdote: The Perl language also has an
exec statement with the same behaviour and if you put some statements after an exec statement (like exec something; print "This won't run";) then Perl will warn you that the print statement will never get executed.– PerlDuck
3 mins ago
add a comment |
Jenny is a new contributor. Be nice, and check out our Code of Conduct.
Jenny is a new contributor. Be nice, and check out our Code of Conduct.
Jenny is a new contributor. Be nice, and check out our Code of Conduct.
Jenny is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1125755%2fwhy-using-two-cd-commands-in-bash-script-does-not-execute-the-second-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.
– LeonidMew
9 mins ago
Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.
– Jenny
4 mins ago