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?
Asked
Active
Viewed 1,310 times
2
Rui F Ribeiro
- 55,929
- 26
- 146
- 227
Lynob
- 4,054
- 12
- 44
- 73
-
Please don't ask multiple questions in a single post. Yes, [running `sudo script` is enough](http://askubuntu.com/a/425990/85695) (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](http://unix.stackexchange.com/questions/182039/resizing-whiptail-to-full-terminal-screen). 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
1 Answers
1
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)?
Thomas Dickey
- 75,040
- 9
- 171
- 268