Fedora Linux Support Community & Resources Center
  #1  
Old 18th July 2012, 11:18 AM
glennzo Online
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Age: 57
Posts: 14,804
linuxfirefox
More bash scripting help

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.

PHP Code:
#!/bin/bash
# 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."
   
exit 1
fi

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
else
   if [[ 
$chassis_type == "Satellite A205" ]] ; then
      
echo $chassis_type $laptop
   
else
      if [[ 
$chassis_type == "P4i65G" ]] ; then
         
echo $chassis_type $centos
else
         if [[ 
$chassis_type == "P4V8X-MX" ]] ; then
            
echo $chassis_type $server
         fi
      fi
   fi
fi
}
get_chassis

test_type
=$(dmidecode -s system-product-name)
case 
$test_type in

"N61PC-M2S")
   echo 
$test_type $phenom
   
;;

"Satellite A205")
   echo 
$test_type $laptop
   
;;

"P4i65G")
   echo 
$test_type $centos
   
;;
"P4V8X-MX")
   echo 
$test_type $server
   
;;

esac 
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?

Disclaimer: 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" ]]
__________________
Glenn
The Bassinator

[SIGPIC][/SIGPIC]
Laptop: Just a couple of old single core units
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / Kingston HyperX 3K SSD 240GB SATA 3.0 / 1TB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
  #2  
Old 18th July 2012, 11:45 AM
flyingfsck Offline
Registered User
 
Join Date: Aug 2010
Location: Al Ain, UAE
Posts: 1,927
linuxfirefox
Re: More bash scripting help

Uhmmm, why not just use the hostname?
Reply With Quote
  #3  
Old 18th July 2012, 12:14 PM
glennzo Online
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Age: 57
Posts: 14,804
linuxfirefox
Re: More bash scripting help

That would work too but wouldn't it amount to the same thing I'm doing with dmidecode?
__________________
Glenn
The Bassinator

[SIGPIC][/SIGPIC]
Laptop: Just a couple of old single core units
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / Kingston HyperX 3K SSD 240GB SATA 3.0 / 1TB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
  #4  
Old 18th July 2012, 01:02 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,877
linuxfirefox
Re: More bash scripting help

You can consider using an associative array - something like

Code:
#!/bin/sh

declare -A tbl
tbl['N61PC-M2S']='This is the Phenom Box'
tbl['Satellite A205']='This is the server'
tbl['P4i65G']='This is the CentOS Box'
tbl['P4V8X-MX']='This is the laptop'

echo ${tbl['Satellite A205']}
where my subscript in the echo is replaced with the dmidecode command.
Reply With Quote
  #5  
Old 18th July 2012, 01:19 PM
Skull One Offline
Registered User
 
Join Date: Jun 2010
Location: Lost...
Posts: 1,164
linuxredhatmozilla
Re: More bash scripting help

Using parameter expansion:
Code:
#!/bin/sh

N61P='This is the Phenom Box'
Sate='This is the server'
P4i6='This is the CentOS Box'
P4V8='This is the laptop'

type=$(dmidecode -s system-product-name)
echo $type $!${type:0:4}
Could you tell if it works? I have not tested it.
__________________
:confused:
Reply With Quote
  #6  
Old 18th July 2012, 01:46 PM
stevea Online
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,985
linuxfirefox
Re: More bash scripting help

Nah associative array, as JP suggests are the right thing .... but we can tighten that up a little

$ declare -A tbl=(["4313CTO"]="my Laptop" ["H57M-USB3"]="My Server")
$ echo ${tbl["$(sudo dmidecode -s system-product-name)"]}
My Server
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #7  
Old 18th July 2012, 02:54 PM
glennzo Online
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Age: 57
Posts: 14,804
windows_xp_2003firefox
Re: More bash scripting help

Thanks for the replies guys. I've tried all three suggestions. Here's the code with edits:

PHP Code:
#!/bin/bash
# jpollard
declare -A tbl
tbl
['N61PC-M2S']='This is the Phenom Box'
tbl['Satellite A205']='This is the server'
tbl['P4i65G']='This is the CentOS Box'
tbl['P4V8X-MX']='This is the laptop'

chassis_type=$(dmidecode -s system-product-name ${tlb})
echo 
$chassis_type

# Skull One
N61P='This is the Phenom Box'
Sate='This is the server'
P4i6='This is the CentOS Box'
P4V8='This is the laptop'

type=$(dmidecode -s system-product-name)
echo 
$type $!${type:0:4}

# stevea
declare -A tbl=(["N61PC-M2S"]="Phenom" ["Satellite A205"]="Laptop" ["P4i65G"]="CentOS" ["P4V8X-MX"]="Server")
echo ${
tbl["$(sudo dmidecode -s system-product-name)"]} 
Here are the results:
jpollard --- > P4V8X-MX
Skull One --> P4V8X-MX P4V8
stevea --> Server

This stuff works, or at the very least is close, so after I get home from work I'll be off to study the code(s) so that I will understand what they do. Thank you so much.

For all the documentation that is available it certainly can be tough to filter down to exactly what you are looking for.
__________________
Glenn
The Bassinator

[SIGPIC][/SIGPIC]
Laptop: Just a couple of old single core units
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / Kingston HyperX 3K SSD 240GB SATA 3.0 / 1TB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
  #8  
Old 18th July 2012, 03:04 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,877
linuxfirefox
Re: More bash scripting help

Using a number of different scripting languages helps.

bash has adopted several features of other languages, but not provided a good tutorial or documentation on them that is easy to find and use.

I knew hash tables were taken, but understanding hash tables only came from using awk and perl (and some LISP where they were first exploited).

But reading the bash manpage was not very clear.

for things I put into scripts (other than the simplest) I still use Perl.
Reply With Quote
  #9  
Old 18th July 2012, 03:39 PM
Skull One Offline
Registered User
 
Join Date: Jun 2010
Location: Lost...
Posts: 1,164
linuxredhatmozilla
Re: More bash scripting help

Damn! It does not expand!
Humm... how was is already? I did it once, but cannot remember... :'(

Edit:
You can still use the following:
Code:
#!/bin/sh

N61P='This is the Phenom Box'
Sate='This is the server'
P4i6='This is the CentOS Box'
P4V8='This is the laptop'

type=$(dmidecode -s system-product-name)
mach=${type:0:4}
echo $type $mach
But I am sure there is a more compact syntax, damn!
__________________
:confused:

Last edited by Skull One; 18th July 2012 at 05:00 PM. Reason: Addon
Reply With Quote
  #10  
Old 19th July 2012, 01:33 AM
glennzo Online
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Age: 57
Posts: 14,804
linuxfirefox
Re: More bash scripting help

Seems the simplest form is:
PHP Code:
type=$(dmidecode -s system-product-name)
echo 
$type 
Working on the laptop the result:
Satellite A205

Works as a one-liner too type=$(dmidecode -s system-product-name) ; echo $type

Of course, I'm still working on developing some sort of understanding of the code

Thank you guys for all your help.
__________________
Glenn
The Bassinator

[SIGPIC][/SIGPIC]
Laptop: Just a couple of old single core units
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / Kingston HyperX 3K SSD 240GB SATA 3.0 / 1TB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
Reply

Tags
bash, scripting

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting help plazman30 Using Fedora 2 4th February 2010 07:34 PM
Bash Scripting leah44 Using Fedora 4 23rd October 2009 04:40 AM
Bash Scripting help North323 Using Fedora 4 16th October 2007 10:25 PM
cron scripting/bash scripting coolbeansdude51 Using Fedora 7 16th January 2007 04:13 AM
bash scripting Wiles Using Fedora 5 27th December 2006 12:12 AM


Current GMT-time: 01:01 (Sunday, 23-11-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin Copyright 2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Judita Travel Photos - Tooele Instagram Photos - Berlin Schoeneberg Travel Photos on Instagram