• Say I have this script:

    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 
          case $choice in
            Update )updatesys
             done < results

    When I run it, it exits, should I return something from the function?

  • Considering the script above as an example, should I run sudo every time I call yum install or is doing sudo ./script.shenough?

  • Please don't ask multiple questions in a single post. Yes, running sudo script is enough (and recommended), don't run sudo command every time a command needs to be run as root. – terdon Jan 30 '15 at 14:35
  • @terdon okay, the main question is the second one, should I ask another question for the first one? - thanks for answering the last one – Lynob Jan 30 '15 at 14:52
  • Yes, please ask the first one separately. The last was the only one I could answer :) – terdon Jan 30 '15 at 15:16
  • @terdon done that. Really? I thought my question is silly, I mean all I'm trying to do is to create a script that will let me install bunch of software that I usually Install on all my vps, so a nice whiptail screen and I select what I want. If you can't answer then my chances of getting an answer are slim :) usually when no one answers my linux questions I pray for Zeus of Kernels to let Tendron see it in order to solve the problem :P – Lynob Jan 30 '15 at 15:29
  • Ha! Thanks, but I've never used whiptail so I have no idea. – terdon Jan 30 '15 at 15:42

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 as root and sudo'd to run itself, that would make a simpler script.
  • On the other hand, some people might comment that running exclusively as root makes 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 whiptail is not installed?
  • if $choice is not a word, it might help to quote it in the case statement.
  • what happens if yum reports an error (does the user see that, or is there another call to whiptail later)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.