Remove sudo from one command in a large Bash script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a large bash script that is supposed to install all dependencies for an application and then build the application itself.
I run the script with sudo
because most of the commands require it, e. g.:
apt update
apt install -y libunwind8
apt install -y curl
...
Then it gets to building the application:
npm install
npm run build
When the script gets to these two commands it fails, giving me the following error:
/home/crispjam/.npm/_cacache/tmp/git-clone-98eb9fb8/.git: Permission denied
I did some research on this and found out that when interacting with a git repository you shouldn't use sudo
.
I tried running the npm install
outside of the script and it did, indeed, work, suggesting that sudo
makes that command fail inside the script.
I've considered adding sudo
to all the commands inside instead of running the script with sudo
but I've read here that for the most part it is not considered good practice.
In the top answer I read that you can drop the sudo
privileges from a single command by prepending sudo -u username
to it.
Is this good practice?
And how can I make this dynamic so that instead of using my username
the script uses the name of the user running the script?
bash sudo
add a comment |
I have a large bash script that is supposed to install all dependencies for an application and then build the application itself.
I run the script with sudo
because most of the commands require it, e. g.:
apt update
apt install -y libunwind8
apt install -y curl
...
Then it gets to building the application:
npm install
npm run build
When the script gets to these two commands it fails, giving me the following error:
/home/crispjam/.npm/_cacache/tmp/git-clone-98eb9fb8/.git: Permission denied
I did some research on this and found out that when interacting with a git repository you shouldn't use sudo
.
I tried running the npm install
outside of the script and it did, indeed, work, suggesting that sudo
makes that command fail inside the script.
I've considered adding sudo
to all the commands inside instead of running the script with sudo
but I've read here that for the most part it is not considered good practice.
In the top answer I read that you can drop the sudo
privileges from a single command by prepending sudo -u username
to it.
Is this good practice?
And how can I make this dynamic so that instead of using my username
the script uses the name of the user running the script?
bash sudo
Related: Ignore sudo in bash script - if the script is being run viasudo
, then it should be able to access the invoking user's uersname as$SUDO_USER
– steeldriver
Mar 29 at 12:49
add a comment |
I have a large bash script that is supposed to install all dependencies for an application and then build the application itself.
I run the script with sudo
because most of the commands require it, e. g.:
apt update
apt install -y libunwind8
apt install -y curl
...
Then it gets to building the application:
npm install
npm run build
When the script gets to these two commands it fails, giving me the following error:
/home/crispjam/.npm/_cacache/tmp/git-clone-98eb9fb8/.git: Permission denied
I did some research on this and found out that when interacting with a git repository you shouldn't use sudo
.
I tried running the npm install
outside of the script and it did, indeed, work, suggesting that sudo
makes that command fail inside the script.
I've considered adding sudo
to all the commands inside instead of running the script with sudo
but I've read here that for the most part it is not considered good practice.
In the top answer I read that you can drop the sudo
privileges from a single command by prepending sudo -u username
to it.
Is this good practice?
And how can I make this dynamic so that instead of using my username
the script uses the name of the user running the script?
bash sudo
I have a large bash script that is supposed to install all dependencies for an application and then build the application itself.
I run the script with sudo
because most of the commands require it, e. g.:
apt update
apt install -y libunwind8
apt install -y curl
...
Then it gets to building the application:
npm install
npm run build
When the script gets to these two commands it fails, giving me the following error:
/home/crispjam/.npm/_cacache/tmp/git-clone-98eb9fb8/.git: Permission denied
I did some research on this and found out that when interacting with a git repository you shouldn't use sudo
.
I tried running the npm install
outside of the script and it did, indeed, work, suggesting that sudo
makes that command fail inside the script.
I've considered adding sudo
to all the commands inside instead of running the script with sudo
but I've read here that for the most part it is not considered good practice.
In the top answer I read that you can drop the sudo
privileges from a single command by prepending sudo -u username
to it.
Is this good practice?
And how can I make this dynamic so that instead of using my username
the script uses the name of the user running the script?
bash sudo
bash sudo
asked Mar 29 at 11:06
CrispJamCrispJam
53
53
Related: Ignore sudo in bash script - if the script is being run viasudo
, then it should be able to access the invoking user's uersname as$SUDO_USER
– steeldriver
Mar 29 at 12:49
add a comment |
Related: Ignore sudo in bash script - if the script is being run viasudo
, then it should be able to access the invoking user's uersname as$SUDO_USER
– steeldriver
Mar 29 at 12:49
Related: Ignore sudo in bash script - if the script is being run via
sudo
, then it should be able to access the invoking user's uersname as $SUDO_USER
– steeldriver
Mar 29 at 12:49
Related: Ignore sudo in bash script - if the script is being run via
sudo
, then it should be able to access the invoking user's uersname as $SUDO_USER
– steeldriver
Mar 29 at 12:49
add a comment |
2 Answers
2
active
oldest
votes
Have you considered changing the folder permissions?
You can check it by right-clicking properties on the folder in your file explorer.
add a comment |
One of the answers here suggested using the command logname
, which prints the user's login name.
To use it inside the script I simply modify the lines
npm install
npm run build
to:
sudo -u $(logname) npm install
sudo -u $(logname) npm run build
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
});
}
});
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%2f1129655%2fremove-sudo-from-one-command-in-a-large-bash-script%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
Have you considered changing the folder permissions?
You can check it by right-clicking properties on the folder in your file explorer.
add a comment |
Have you considered changing the folder permissions?
You can check it by right-clicking properties on the folder in your file explorer.
add a comment |
Have you considered changing the folder permissions?
You can check it by right-clicking properties on the folder in your file explorer.
Have you considered changing the folder permissions?
You can check it by right-clicking properties on the folder in your file explorer.
answered Mar 29 at 11:19
PilouPilou
42
42
add a comment |
add a comment |
One of the answers here suggested using the command logname
, which prints the user's login name.
To use it inside the script I simply modify the lines
npm install
npm run build
to:
sudo -u $(logname) npm install
sudo -u $(logname) npm run build
add a comment |
One of the answers here suggested using the command logname
, which prints the user's login name.
To use it inside the script I simply modify the lines
npm install
npm run build
to:
sudo -u $(logname) npm install
sudo -u $(logname) npm run build
add a comment |
One of the answers here suggested using the command logname
, which prints the user's login name.
To use it inside the script I simply modify the lines
npm install
npm run build
to:
sudo -u $(logname) npm install
sudo -u $(logname) npm run build
One of the answers here suggested using the command logname
, which prints the user's login name.
To use it inside the script I simply modify the lines
npm install
npm run build
to:
sudo -u $(logname) npm install
sudo -u $(logname) npm run build
answered Mar 29 at 12:28
CrispJamCrispJam
53
53
add a comment |
add a comment |
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%2f1129655%2fremove-sudo-from-one-command-in-a-large-bash-script%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
Related: Ignore sudo in bash script - if the script is being run via
sudo
, then it should be able to access the invoking user's uersname as$SUDO_USER
– steeldriver
Mar 29 at 12:49