Repetitive Tasks: using if..then inside a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Repetitive Tasks: using if..then inside a loop
# 1  
Old 03-09-2006
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  
Old 03-09-2006
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  
Old 03-13-2006
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Improving repetitive tasks in function

stop_service () { sudo systemctl is-active --quiet video.service && sudo systemctl stop video.service && sudo rm /etc/systemd/system/video.service && echo stop video sudo systemctl is-active --quiet audio.service && sudo systemctl stop audio.service && sudo rm /etc/systemd/system/audio.service... (4 Replies)
Discussion started by: aristosv
4 Replies

2. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

3. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

4. Shell Programming and Scripting

Doing multiple tasks in a loop.

I am in the process of updating a folder of hundreds of recipe html files. I've already managed to modify a number of things in each file but I have run into something that's beyond my ability. I have a text file that I need to insert the contents into the html at a specific point. It creates... (0 Replies)
Discussion started by: Trapper
0 Replies

5. Shell Programming and Scripting

Background tasks in a loop (bash)

I am trying to use a loop to start tasks 0-3, running 0,1,2 in the background with &. FOLDSET=( 0 1 2 3 ) for FOLDSET in ${FOLDSET} do if ; then BACKGRD="&" else BACKGRD="" fi # start task $FOLDSET task1 -nogui -ni -p $PROJ \ epochs=$EPOS ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Using 'su' inside a loop

Hi, I am using su within a for loop. As you might expect, it prompts for password during each loop execution. Here is my piece of code: for i in $LIST do if then DATABASE=`echo $i | awk -F "|" '{ print $1 }'` USER_ID=`echo $i | awk -F "|" '{ print $2 }'` su - apstage -c... (1 Reply)
Discussion started by: sugan
1 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. UNIX for Dummies Questions & Answers

Repetitive Tasks

Could someone tell me how I can simplify the script that follows!!! I know that there must be a way how to grep Average from sar01.................. sar02 ....................... sar03....................... sar04... (3 Replies)
Discussion started by: JairGuerra
3 Replies
Login or Register to Ask a Question