Write output of command in bash script returns an empty file
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a bash script used to write all the committers' details to a file for each repository on a list.
If I perform the steps manually, the file is written and there's content. However, if I do this using the bash script, the written file is empty (without content).
Can you help me find what is wrong?
Thanks in advance.
Below, you can find my script.
#!/usr/bin/env sh
if [ $# -lt 1 ];then
echo "usage: $0 <repos_list>"
exit 1
fi
repos_list=$1
cat $repos_list | while read -r line || [[ -n "$line" ]];
do
# Control will enter here if $line exists.
if [ -d "$line" ]; then
cd $line
git shortlog -sne > git_commits.file 2>&1
echo --------------------------------
echo "#Commits and committers for $line"
echo --------------------------------
cat git_commits.file
echo --------------------------------
rm -rf git_commits.file
cd ..
fi
done
bash sh stdout
|
show 2 more comments
I have a bash script used to write all the committers' details to a file for each repository on a list.
If I perform the steps manually, the file is written and there's content. However, if I do this using the bash script, the written file is empty (without content).
Can you help me find what is wrong?
Thanks in advance.
Below, you can find my script.
#!/usr/bin/env sh
if [ $# -lt 1 ];then
echo "usage: $0 <repos_list>"
exit 1
fi
repos_list=$1
cat $repos_list | while read -r line || [[ -n "$line" ]];
do
# Control will enter here if $line exists.
if [ -d "$line" ]; then
cd $line
git shortlog -sne > git_commits.file 2>&1
echo --------------------------------
echo "#Commits and committers for $line"
echo --------------------------------
cat git_commits.file
echo --------------------------------
rm -rf git_commits.file
cd ..
fi
done
bash sh stdout
First, note that the or part of the while condition will only be executed if theread
sets an exit code, i.e. if $repos_list is exhausted. In this case,$line
still holds the content of the last input line, but you have processed it already. Aside from this, I guess that what is stored in$line
is not a directory; you could verify it by adding anelse
part, where you print out:echo Not a directory: $line
.
– user1934428
Jan 3 at 14:05
2
An aside -Don'tcat $repos_list
. Trydone < $repos_list
.
– Paul Hodges
Jan 3 at 14:21
1
@user1934428 The|| [[ -n "$line" ]]
bit is for the case where the input file doesn't have a newline on the last line.
– Benjamin W.
Jan 3 at 14:29
1
You have ash
shebang line, but you use bashisms such as[[ ]]
. Use a bash shebang line instead, or use[ ... ]
instead.
– Benjamin W.
Jan 3 at 14:30
1
Which file do you expect to be written? The only file I see is deleted in every loop.
– Benjamin W.
Jan 3 at 14:31
|
show 2 more comments
I have a bash script used to write all the committers' details to a file for each repository on a list.
If I perform the steps manually, the file is written and there's content. However, if I do this using the bash script, the written file is empty (without content).
Can you help me find what is wrong?
Thanks in advance.
Below, you can find my script.
#!/usr/bin/env sh
if [ $# -lt 1 ];then
echo "usage: $0 <repos_list>"
exit 1
fi
repos_list=$1
cat $repos_list | while read -r line || [[ -n "$line" ]];
do
# Control will enter here if $line exists.
if [ -d "$line" ]; then
cd $line
git shortlog -sne > git_commits.file 2>&1
echo --------------------------------
echo "#Commits and committers for $line"
echo --------------------------------
cat git_commits.file
echo --------------------------------
rm -rf git_commits.file
cd ..
fi
done
bash sh stdout
I have a bash script used to write all the committers' details to a file for each repository on a list.
If I perform the steps manually, the file is written and there's content. However, if I do this using the bash script, the written file is empty (without content).
Can you help me find what is wrong?
Thanks in advance.
Below, you can find my script.
#!/usr/bin/env sh
if [ $# -lt 1 ];then
echo "usage: $0 <repos_list>"
exit 1
fi
repos_list=$1
cat $repos_list | while read -r line || [[ -n "$line" ]];
do
# Control will enter here if $line exists.
if [ -d "$line" ]; then
cd $line
git shortlog -sne > git_commits.file 2>&1
echo --------------------------------
echo "#Commits and committers for $line"
echo --------------------------------
cat git_commits.file
echo --------------------------------
rm -rf git_commits.file
cd ..
fi
done
bash sh stdout
bash sh stdout
asked Jan 3 at 12:05


Bruno FernandesBruno Fernandes
821110
821110
First, note that the or part of the while condition will only be executed if theread
sets an exit code, i.e. if $repos_list is exhausted. In this case,$line
still holds the content of the last input line, but you have processed it already. Aside from this, I guess that what is stored in$line
is not a directory; you could verify it by adding anelse
part, where you print out:echo Not a directory: $line
.
– user1934428
Jan 3 at 14:05
2
An aside -Don'tcat $repos_list
. Trydone < $repos_list
.
– Paul Hodges
Jan 3 at 14:21
1
@user1934428 The|| [[ -n "$line" ]]
bit is for the case where the input file doesn't have a newline on the last line.
– Benjamin W.
Jan 3 at 14:29
1
You have ash
shebang line, but you use bashisms such as[[ ]]
. Use a bash shebang line instead, or use[ ... ]
instead.
– Benjamin W.
Jan 3 at 14:30
1
Which file do you expect to be written? The only file I see is deleted in every loop.
– Benjamin W.
Jan 3 at 14:31
|
show 2 more comments
First, note that the or part of the while condition will only be executed if theread
sets an exit code, i.e. if $repos_list is exhausted. In this case,$line
still holds the content of the last input line, but you have processed it already. Aside from this, I guess that what is stored in$line
is not a directory; you could verify it by adding anelse
part, where you print out:echo Not a directory: $line
.
– user1934428
Jan 3 at 14:05
2
An aside -Don'tcat $repos_list
. Trydone < $repos_list
.
– Paul Hodges
Jan 3 at 14:21
1
@user1934428 The|| [[ -n "$line" ]]
bit is for the case where the input file doesn't have a newline on the last line.
– Benjamin W.
Jan 3 at 14:29
1
You have ash
shebang line, but you use bashisms such as[[ ]]
. Use a bash shebang line instead, or use[ ... ]
instead.
– Benjamin W.
Jan 3 at 14:30
1
Which file do you expect to be written? The only file I see is deleted in every loop.
– Benjamin W.
Jan 3 at 14:31
First, note that the or part of the while condition will only be executed if the
read
sets an exit code, i.e. if $repos_list is exhausted. In this case, $line
still holds the content of the last input line, but you have processed it already. Aside from this, I guess that what is stored in $line
is not a directory; you could verify it by adding an else
part, where you print out: echo Not a directory: $line
.– user1934428
Jan 3 at 14:05
First, note that the or part of the while condition will only be executed if the
read
sets an exit code, i.e. if $repos_list is exhausted. In this case, $line
still holds the content of the last input line, but you have processed it already. Aside from this, I guess that what is stored in $line
is not a directory; you could verify it by adding an else
part, where you print out: echo Not a directory: $line
.– user1934428
Jan 3 at 14:05
2
2
An aside -Don't
cat $repos_list
. Try done < $repos_list
.– Paul Hodges
Jan 3 at 14:21
An aside -Don't
cat $repos_list
. Try done < $repos_list
.– Paul Hodges
Jan 3 at 14:21
1
1
@user1934428 The
|| [[ -n "$line" ]]
bit is for the case where the input file doesn't have a newline on the last line.– Benjamin W.
Jan 3 at 14:29
@user1934428 The
|| [[ -n "$line" ]]
bit is for the case where the input file doesn't have a newline on the last line.– Benjamin W.
Jan 3 at 14:29
1
1
You have a
sh
shebang line, but you use bashisms such as [[ ]]
. Use a bash shebang line instead, or use [ ... ]
instead.– Benjamin W.
Jan 3 at 14:30
You have a
sh
shebang line, but you use bashisms such as [[ ]]
. Use a bash shebang line instead, or use [ ... ]
instead.– Benjamin W.
Jan 3 at 14:30
1
1
Which file do you expect to be written? The only file I see is deleted in every loop.
– Benjamin W.
Jan 3 at 14:31
Which file do you expect to be written? The only file I see is deleted in every loop.
– Benjamin W.
Jan 3 at 14:31
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f54021972%2fwrite-output-of-command-in-bash-script-returns-an-empty-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f54021972%2fwrite-output-of-command-in-bash-script-returns-an-empty-file%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
First, note that the or part of the while condition will only be executed if the
read
sets an exit code, i.e. if $repos_list is exhausted. In this case,$line
still holds the content of the last input line, but you have processed it already. Aside from this, I guess that what is stored in$line
is not a directory; you could verify it by adding anelse
part, where you print out:echo Not a directory: $line
.– user1934428
Jan 3 at 14:05
2
An aside -Don't
cat $repos_list
. Trydone < $repos_list
.– Paul Hodges
Jan 3 at 14:21
1
@user1934428 The
|| [[ -n "$line" ]]
bit is for the case where the input file doesn't have a newline on the last line.– Benjamin W.
Jan 3 at 14:29
1
You have a
sh
shebang line, but you use bashisms such as[[ ]]
. Use a bash shebang line instead, or use[ ... ]
instead.– Benjamin W.
Jan 3 at 14:30
1
Which file do you expect to be written? The only file I see is deleted in every loop.
– Benjamin W.
Jan 3 at 14:31