![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | 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. |
| Forum Sponsor | ||
|
|
|
|||
|
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
|
|||
| Google UNIX.COM |