LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2013, 02:59 PM   #1
acarnera
LQ Newbie
 
Registered: May 2013
Posts: 1

Rep: Reputation: Disabled
Help with IF statement in Bash


I need help, please. I don't normally do any scripting.

The following IF statement in this script will not work. If I comment out the IF statement, the case statement works fine. Please let me know what I am doing wrong. Thanks!!


#!/bin/bash



# Network segment determined by 3rd octet of IP


myNetwork1=`system_profiler | grep 'IPv4 Addresses'`
myNetwork=`system_profiler | grep 'IPv4 Addresses' | awk '{ print $3 }' | sed 's/[.]/ /g' | awk '{ print $3 }'`


echo $myNetwork1
echo $myNetwork

if [[ "$myNetwork1" == "IPv4 Addresses: 172.31*" ]]
then

case $myNetwork in
8*)

exec lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael &
;;
11*)

exec /usr/sbin/lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec /usr/sbin/lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec /usr/sbin/lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec /usr/sbin/lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael
 &
;;
12*)

exec /usr/sbin/lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec /usr/sbin/lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec /usr/sbin/lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec /usr/sbin/lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael
 &
;;
13*)

exec /usr/sbin/lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec /usr/sbin/lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec /usr/sbin/lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec /usr/sbin/lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael
 &
;;
14*)

 exec /usr/sbin/lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec /usr/sbin/lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec /usr/sbin/lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec /usr/sbin/lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael
 &
;;
esac
fi
 
Old 05-10-2013, 05:28 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
OK a few things here:


Your IT security is awesome, you managed to get TMNT into your network and nobody messes with those guys...

Can you please also put your code in the CODE tags.

What is the result of the echo statement:

Code:
echo $myNetwork1
 
Old 05-10-2013, 05:32 PM   #3
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Also,


It's probably the expansion that is causing the problem:

Code:
if [[ echo "$myNetwork1" | grep -e "IPv4 Addresses: 172.31*" ]]
 
Old 05-10-2013, 05:37 PM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Another note,

The if statement will show the output of the grep on stdout.

It is probably also easier to do a case statement nested above the first one:

Code:
#!/bin/bash



# Network segment determined by 3rd octet of IP


myNetwork1=`system_profiler | grep 'IPv4 Addresses'`
myNetwork=`system_profiler | grep 'IPv4 Addresses' | awk '{ print $3 }' | sed 's/[.]/ /g' | awk '{ print $3 }'`


echo $myNetwork1
echo $myNetwork

case $myNetwork1 in

*172.31*) 

case $myNetwork in
8*)

exec lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael &
;;
11*)

exec /usr/sbin/lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec /usr/sbin/lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec /usr/sbin/lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec /usr/sbin/lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael
 &
;;
12*)

exec /usr/sbin/lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec /usr/sbin/lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec /usr/sbin/lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec /usr/sbin/lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael
 &
;;
13*)

exec /usr/sbin/lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec /usr/sbin/lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec /usr/sbin/lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec /usr/sbin/lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael
 &
;;
14*)

 exec /usr/sbin/lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec /usr/sbin/lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec /usr/sbin/lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec /usr/sbin/lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael
 &
;;
;;
esac
esac
 
Old 05-10-2013, 11:59 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Is it just me or is the original case statement a little pointless as it calls the exact same 'execs' irrelevant of which match is made?

Also, no need for grep / sed / awk when either sed or awk on their own can probably do what you require. As listed above, show the output of myNetwork1 and we can help reduce
the statments, plus you already have it stored in this variable so no need to recall the same commands again.
 
Old 05-12-2013, 04:44 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Ok, that's pretty ugly. Long, repeated, nearly-unreadable commands like that should generally be configured as functions, so that they don't clutter up the code. Or at least set up the various options in variables first.

And is exec really needed here? If you're forking off the commands and want to detach them from the script, follow them up with the disown builtin instead.

Code:
myNetwork1=`system_profiler | grep 'IPv4 Addresses'`
myNetwork=`system_profiler | grep 'IPv4 Addresses' | awk '{ print $3 }' | sed 's/[.]/ /g' | awk '{ print $3 }'`


$(..) is highly recommended over `..`

You're running the same command twice here. Have the second command filter the $myNetwork1 variable directly instead. You can probably replace the awk and sed with some built-in string manipulations instead, as well.

Code:
if [[ "$myNetwork1" == "IPv4 Addresses: 172.31*" ]]
then

case $myNetwork in
Again, you should use variables and string substitutions to extract only the parts you need to test first, instead of testing for long text strings. It generally pays off to pre-process your input, to help keep the code following it simple and clean and to keep the focus on what's really important.

Code:
8*)

exec lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael &
;;
As I mentioned before, moving the commands into functions, and/or storing data strings in variables, can really help. In bash you can use an array to safely store a string of command options together. Also, quote long strings rather than backslash escaping them.

Code:
lp_settings(){

    local pD_opt L_opt v_opt P_opt

    case $1 in
        don) 
            pD_opt=dal1-donatello
            L_opt='Next to IT Room'
            v_opt='lpd://172.31.8.34'
            P_opt='/Library/Printers/PPDs/Contents/Resources/RICOH Aficio MP C3501'
        ;;
        raph)
            ...<etc>
        ;;
    esac

    lpadmin_opts=( -p "$pD_opt" -D "$pD_opt" -E -L "$L_opt" -v "$v_opt" )
}

lp_settings don && lpadmin "${lpadmin_opts[@]}" &
disown

Last edited by David the H.; 05-12-2013 at 04:47 PM.
 
Old 05-12-2013, 06:06 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
I suspect that you probably meant if [[ "${myNetwork1}" =~ "IPv4 Addresses: 172.31" ]] not if [[ "$myNetwork1" == "IPv4 Addresses: 172.31*" ]]. Since the string "IPv4 Addresses: 172.31*" is a literal, the exact string match (==) is likely to always fail.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
OR statement in Bash akeenabawa Linux - Software 3 11-14-2008 09:17 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
BASH IF statement kinetik Programming 10 05-07-2006 02:48 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration