Run a function in a Shell Script as a different user [duplicate]












0
















This question already has an answer here:




  • Executing a Bash Script Function with Sudo

    5 answers



  • Forward function and variables into sudo su - <user> <<EOF

    3 answers



  • “command not found” when sudo'ing function from ~/.zshrc

    3 answers




I have a .sh script which contains a set of functions. I have made the script executable (chmod 777).



If I want to run a function as my current user I simply do:



. /path/to/script/functions.sh && fc_number1 "${item1}" "${item2}" &


This works fine. However I have another function, that needs to be run by a different user with different privileges.



I have tried:



sudo -unewuser . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}"
sudo -unewuser -c "bash -c fc_number2 ${item1} ${item2} ${item3}"


Neither of which works. I simply get sudo: .: command not found or bash: export: fc_number2: not a function, or some other error.



Is there something else I need to do to make this work. Everything I read basically comes back to one of the two options I have tried above.










share|improve this question













marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 18 at 13:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You could use a wrapper script that contains . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}" and run it as sudo -unewuser wrapper_script.sh. If necessary you could also create a wrapper script that takes command line arguments and passes them to the function call.

    – Bodo
    Feb 18 at 13:41











  • It is better to not give scripts a .sh ending. And if you give a mode of 777 then you give everyone on the system, permission to write/change the script.

    – ctrl-alt-delor
    Feb 18 at 13:53











  • the 777 was purely to try and get it working on my test environment. It just means there is one less thing that may be preventing it working. Once I have it working it would then refine it back down to the bare minimum privileges needed to make it work.

    – IGGt
    Feb 18 at 13:58
















0
















This question already has an answer here:




  • Executing a Bash Script Function with Sudo

    5 answers



  • Forward function and variables into sudo su - <user> <<EOF

    3 answers



  • “command not found” when sudo'ing function from ~/.zshrc

    3 answers




I have a .sh script which contains a set of functions. I have made the script executable (chmod 777).



If I want to run a function as my current user I simply do:



. /path/to/script/functions.sh && fc_number1 "${item1}" "${item2}" &


This works fine. However I have another function, that needs to be run by a different user with different privileges.



I have tried:



sudo -unewuser . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}"
sudo -unewuser -c "bash -c fc_number2 ${item1} ${item2} ${item3}"


Neither of which works. I simply get sudo: .: command not found or bash: export: fc_number2: not a function, or some other error.



Is there something else I need to do to make this work. Everything I read basically comes back to one of the two options I have tried above.










share|improve this question













marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 18 at 13:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You could use a wrapper script that contains . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}" and run it as sudo -unewuser wrapper_script.sh. If necessary you could also create a wrapper script that takes command line arguments and passes them to the function call.

    – Bodo
    Feb 18 at 13:41











  • It is better to not give scripts a .sh ending. And if you give a mode of 777 then you give everyone on the system, permission to write/change the script.

    – ctrl-alt-delor
    Feb 18 at 13:53











  • the 777 was purely to try and get it working on my test environment. It just means there is one less thing that may be preventing it working. Once I have it working it would then refine it back down to the bare minimum privileges needed to make it work.

    – IGGt
    Feb 18 at 13:58














0












0








0









This question already has an answer here:




  • Executing a Bash Script Function with Sudo

    5 answers



  • Forward function and variables into sudo su - <user> <<EOF

    3 answers



  • “command not found” when sudo'ing function from ~/.zshrc

    3 answers




I have a .sh script which contains a set of functions. I have made the script executable (chmod 777).



If I want to run a function as my current user I simply do:



. /path/to/script/functions.sh && fc_number1 "${item1}" "${item2}" &


This works fine. However I have another function, that needs to be run by a different user with different privileges.



I have tried:



sudo -unewuser . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}"
sudo -unewuser -c "bash -c fc_number2 ${item1} ${item2} ${item3}"


Neither of which works. I simply get sudo: .: command not found or bash: export: fc_number2: not a function, or some other error.



Is there something else I need to do to make this work. Everything I read basically comes back to one of the two options I have tried above.










share|improve this question















This question already has an answer here:




  • Executing a Bash Script Function with Sudo

    5 answers



  • Forward function and variables into sudo su - <user> <<EOF

    3 answers



  • “command not found” when sudo'ing function from ~/.zshrc

    3 answers




I have a .sh script which contains a set of functions. I have made the script executable (chmod 777).



If I want to run a function as my current user I simply do:



. /path/to/script/functions.sh && fc_number1 "${item1}" "${item2}" &


This works fine. However I have another function, that needs to be run by a different user with different privileges.



I have tried:



sudo -unewuser . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}"
sudo -unewuser -c "bash -c fc_number2 ${item1} ${item2} ${item3}"


Neither of which works. I simply get sudo: .: command not found or bash: export: fc_number2: not a function, or some other error.



Is there something else I need to do to make this work. Everything I read basically comes back to one of the two options I have tried above.





This question already has an answer here:




  • Executing a Bash Script Function with Sudo

    5 answers



  • Forward function and variables into sudo su - <user> <<EOF

    3 answers



  • “command not found” when sudo'ing function from ~/.zshrc

    3 answers








bash function






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 18 at 13:35









IGGtIGGt

68541527




68541527




marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 18 at 13:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 18 at 13:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You could use a wrapper script that contains . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}" and run it as sudo -unewuser wrapper_script.sh. If necessary you could also create a wrapper script that takes command line arguments and passes them to the function call.

    – Bodo
    Feb 18 at 13:41











  • It is better to not give scripts a .sh ending. And if you give a mode of 777 then you give everyone on the system, permission to write/change the script.

    – ctrl-alt-delor
    Feb 18 at 13:53











  • the 777 was purely to try and get it working on my test environment. It just means there is one less thing that may be preventing it working. Once I have it working it would then refine it back down to the bare minimum privileges needed to make it work.

    – IGGt
    Feb 18 at 13:58



















  • You could use a wrapper script that contains . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}" and run it as sudo -unewuser wrapper_script.sh. If necessary you could also create a wrapper script that takes command line arguments and passes them to the function call.

    – Bodo
    Feb 18 at 13:41











  • It is better to not give scripts a .sh ending. And if you give a mode of 777 then you give everyone on the system, permission to write/change the script.

    – ctrl-alt-delor
    Feb 18 at 13:53











  • the 777 was purely to try and get it working on my test environment. It just means there is one less thing that may be preventing it working. Once I have it working it would then refine it back down to the bare minimum privileges needed to make it work.

    – IGGt
    Feb 18 at 13:58

















You could use a wrapper script that contains . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}" and run it as sudo -unewuser wrapper_script.sh. If necessary you could also create a wrapper script that takes command line arguments and passes them to the function call.

– Bodo
Feb 18 at 13:41





You could use a wrapper script that contains . /path/to/script/functions.sh && fc_number2 "${item1}" "${item2}" "${item3}" and run it as sudo -unewuser wrapper_script.sh. If necessary you could also create a wrapper script that takes command line arguments and passes them to the function call.

– Bodo
Feb 18 at 13:41













It is better to not give scripts a .sh ending. And if you give a mode of 777 then you give everyone on the system, permission to write/change the script.

– ctrl-alt-delor
Feb 18 at 13:53





It is better to not give scripts a .sh ending. And if you give a mode of 777 then you give everyone on the system, permission to write/change the script.

– ctrl-alt-delor
Feb 18 at 13:53













the 777 was purely to try and get it working on my test environment. It just means there is one less thing that may be preventing it working. Once I have it working it would then refine it back down to the bare minimum privileges needed to make it work.

– IGGt
Feb 18 at 13:58





the 777 was purely to try and get it working on my test environment. It just means there is one less thing that may be preventing it working. Once I have it working it would then refine it back down to the bare minimum privileges needed to make it work.

– IGGt
Feb 18 at 13:58










0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀