Say I have this script:
#!/bin/bash function cpp-lang { yum install "Development Tools" } function updatesys { yum -y update yum -y upgrade } whiptail --checklist "test" 5 40 5\ Update "Update the system" on \ C++ "Install C++" off 2>results while read choice do case $choice in Update )updatesys ;; C++)cpp-lang ;; *) ;; esac done < resultsWhen I run it, it exits, should I return something from the function?
Considering the script above as an example, should I run
sudoevery time I callyum installor is doingsudo ./script.shenough?
1 Answer
Basically (the second question of course regarding the way sudo should be used most effectively), it's a matter of judgement:
- In the script there are three calls to
yum. If the script tested at the beginning whether it is running asrootand sudo'd to run itself, that would make a simpler script. - On the other hand, some people might comment that running exclusively as
rootmakes it less safe to test the interactive part (in case you continued to develop and expand the script, including specifying the package names directly). My own inclination would be to provide a command-line option to let the script be tested, and just show (or log) the corresponding commands that would be run.
For instance:
- what happens if
whiptailis not installed? - if
$choiceis not a word, it might help to quote it in thecasestatement. - what happens if yum reports an error (does the user see that, or is there another call to
whiptaillater)?
sudo scriptis enough (and recommended), don't runsudo commandevery time a command needs to be run as root.