![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|