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 and shell scripting languages here.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-09-2006
garak garak is offline
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.
  #2 (permalink)  
Old 03-09-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,128
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 08:34 PM..
  #3 (permalink)  
Old 03-13-2006
thestevew thestevew is offline
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0