Something I don't understand although I've been browsing the typical bash scripting web sites. The following script simply tells me which computer it is being run on. There are 4 possibilities, the server, the phenom box, the centos box or the laptop. It works as expected. In the script there are two methods for achieving the desired result. I do believe that there are better ways to accomplish the task in a script but I don't know what these methods are referred to, arrays, loops, brace expansion, what? Seems that my code could be considered a bit redundant and could be shorter.
# Test if desktop or laptop
# Text strings
phenom="This is the Phenom Box"
server="This is the server"
centos="This is the CentOS Box"
laptop="This is the laptop"
if [[ $EUID -ne 0 ]] ; then # Need root permissions
echo "Please execute this script with root permissions."
function get_chassis() # Determine if system is desktop or laptop
chassis_type=$(dmidecode -s system-product-name)
if [[ $chassis_type == "N61PC-M2S" ]] ; then
echo $chassis_type $phenom
if [[ $chassis_type == "Satellite A205" ]] ; then
echo $chassis_type $laptop
if [[ $chassis_type == "P4i65G" ]] ; then
echo $chassis_type $centos
if [[ $chassis_type == "P4V8X-MX" ]] ; then
echo $chassis_type $server
test_type=$(dmidecode -s system-product-name)
case $test_type in
echo $test_type $phenom
echo $test_type $laptop
echo $test_type $centos
echo $test_type $server
So you can see that I've used if-then-else excessivly and I've also tried a case statement. I think it can be done with less code and in a way that makes more sense. Can anyone offer some help or a rewrite of this code?
This is not a homework project. It's been almost 40 years since I've had to do homework
Edit: As an afterthought, I don't really want someone to rewrite the code for me. I just need to be pointed in the right diretion. "Glenn, you need to use <what?>"
[[ "N61PC-M2S", "Satellite A205", "P4i65G", "P4V8X-MX" ]]