The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
remsh inside of while loop joettacm UNIX for Advanced & Expert Users 1 12-07-2007 08:54 AM
rsh script with inside a for loop trekianov Shell Programming and Scripting 4 01-20-2006 05:54 AM
Repetitive Tasks JairGuerra UNIX for Dummies Questions & Answers 3 11-18-2005 08:36 AM
input inside while read loop jhansrod Shell Programming and Scripting 3 08-13-2005 07:46 AM
read inside a while loop dta4316 UNIX for Dummies Questions & Answers 3 05-21-2005 07:53 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-09-2006
Registered User
 

Join Date: Mar 2006
Posts: 3
Repetitive Tasks: using if..then inside a loop



I thought I was getting pretty good with Bash but this problem is stumping me. Any suggestions would be greatly appreciated. I've written a firewall script that uses a lot of functions. The variables are read into the script from another .conf file earlier on in the code. Most of these variables are either set as "ON", "OFF", or as an IP address. To date, I've written out a conditional statement for each service. Here is an example of two of them:


if [ $FTP = "ON" ]; then
FTP_WAN
else
if [ "$FTP" != "OFF" ]; then
FTP_PORT_FORWARDING
fi
fi

if [ $SSH = "ON" ]; then
SSH_WAN
else
if [ "$SSH" != "OFF" ]; then
SSH_PORT_FORWARDING
fi
fi


I'd like to figure out a way to make one conditional statement inside a loop where the $SSH or $FTP variables are replaced for each variable corresponding to a service.

I'd thought something like:


$SERVICES=SSH,FTP

for VAR in $SERVICES; do

if [ $VAR = "ON" ]; then
$VAR_WAN
else
if [ "$VAR" != "OFF" ]; then
$VAR_PORT_FORWARDING
fi
fi

done


But no variation I've tried seems to work. I want it to
input the variable, like SSH, and then read the value
of SSH inside the conditional statement. Am I missing
some brackets somewhere or anything else?

Thanks.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-09-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
shell != perl
Code:
SERVICES='SSH FTP'
Code:
#!/bin/ksh

a='ssh ftp'

for i in ${a}
do
   eval echo \${i}_wan
done

Last edited by vgersh99; 03-09-2006 at 04:34 PM.
Reply With Quote
  #3 (permalink)  
Old 03-13-2006
Registered User
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
How about:

Code:
# Test values
ssh=on
ftp=off
# End test values

typeset -u v               ;# output in upper case

a='ssh ftp'

for i in ${a}
do
  eval v=\${$i}            ;# v becomes value of variable _called_ $i
  if [ $v = 'ON' ]; then
    v=${i}_WAN
  else
    v=${i}_PORT_FORWARDING
  fi

  print $v                 ;# do something with the output
done
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0