Bash: Execute a Function as Sudo within a Script: Difference between revisions

From WikiMLT
m (Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:Linux Server)
mNo edit summary
Line 14: Line 14:
* [https://unix.stackexchange.com/a/269080/201297 Executing a Bash Script Function with Sudo]
* [https://unix.stackexchange.com/a/269080/201297 Executing a Bash Script Function with Sudo]
* [https://stackoverflow.com/q/9448920/6543935 How can I execute a bash function using sudo?]
* [https://stackoverflow.com/q/9448920/6543935 How can I execute a bash function using sudo?]
<noinclude>
 
[[Category:Ubuntu Scripts and Commands|2]]
</noinclude>
<noinclude>
<noinclude>
<div id='devStage'>
<div id='devStage'>

Revision as of 12:03, 3 August 2022

Prob­a­bly the sim­plest way is the fol­low:

#!/bin/bash

# Define the function
main() { ... }

sudo -b bash -c "$(declare -f main); main"

Ref­er­ences: