Nested If statement within Do / Done


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Nested If statement within Do / Done
# 1  
Old 12-22-2009
Lightbulb Nested If statement within Do / Done

Hi all!
I'm really hoping you can help me out here; now i have searched and searched and have at least worked out that you can't have a nested if statement with a 'done' in it (as i have) as you're killing the parent before the child.

So here's what i have, and here's hoping someone can help me think of a work around! FYI, i'm using bash on a custom linux build, fairly standard based around red hat i believe(?)

The script is to find all attached USB devices, and copy a folders contents to it. The issue is within the variable check at the start, and the two unmounting after replication parts.

Theres a few other parts later on which i haven't posted which have a similar issue- so any ideas would be great! Smilie

Code:
# Assigning Drive variables and locating available disks.
alldrives=$(df -h | grep sda | awk '{print$1}' | cut -c1-8)
d1=$(echo $alldrives | awk '{print$1}')
d2=$(echo $alldrives | awk '{print$2}')
d3=$(echo $alldrives | awk '{print$3}')
d4=$(echo $alldrives | awk '{print$4}')
d5=$(echo $alldrives | awk '{print$5}')
d6=$(echo $alldrives | awk '{print$6}')
d7=$(echo $alldrives | awk '{print$7}')
d8=$(echo $alldrives | awk '{print$8}')
d9=$(echo $alldrives | awk '{print$9}')
d10=$(echo $alldrives | awk '{print$10}')
 
for i in $d1 $d2 $d3 $d4 $d5 $d6 $d7 $d8 $d9 $d10
do
 
if [[ -z $i ]]
then
echo "Variable blank, moving to next"
done
fi
 
#CHECK FOR MOUNTABLE DRIVE, MAKE DIR FOR IT
mkdir /mnt/clone/
if mount "$i""1" /mnt/clone/"$i""1"
then
 
 
# START REPLICATION
echo "BEGINNING RSYNC REPLICATION"
rsync -rtv --progress --modify-window=1 --exclude=LOCAL.CFG /usr/local/FOLDER1 /mnt/clone/"$i""1"/usr/local/FOLDER1
echo
 
 
# UNMOUNT AFTER REPLICATION
if umount /mnt/clone/"$i""1"
then
echo "Unmounted Successfully. Copying Complete."
 
done
else
 
 
# FORCE IF UNABLE TO UNMOUNT
echo "Forcing unmount..."
sync
sleep 15
cd /
umount -f /mnt/clone/"$i""1"
done
fi
 
fi

# 2  
Old 12-22-2009
Small wonder you're having problems with this. I can't even understand why it compiles, maybe it's a bug, but I can't image that using "done" inside if-statements to close the surrounding loop is legal.

With loops, use either "break" to break out of the loop, or "continue" to start the next iteration.

Aside from that, if the drives in $alldrives are space seperated, you can rewrite your loop to
Code:
for i in $alldrives
do
    ....

Future debugging tips:
  • set -x shows a nice trace on stderr of what is done
  • the -n switch to ksh/bash does a syntax check without running the script


---------- Post updated at 14:56 ---------- Previous update was at 14:52 ----------

Just verfied on Linux using your snipped of code:
Code:
$ bash -n test.sh
test.sh: line 20: syntax error near unexpected token `done'
test.sh: line 20: `done'
$ ksh -n test.sh
test.sh: syntax error at line 20: `done' unexpected

Replacing your "done"s with "continue"s:
Code:
$ ksh -n test.sh
$ bash -n test.sh

# 3  
Old 12-22-2009
MySQL

May i just say you are a legend!
I wasn't aware of the break/continue commands and that has sorted it straight out, replaced all dones (except for final, out of nest, one!) with continues and voila.

Thanks again for saving me hours of headache,
Darren.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Nested If

I am having a problem with a nested if. I am sure I am overlooking something. I check for the existence of $Pidfl3 and it exists, o this condition I then want to check for the existence of a next file and remove it. The first if is executed, but on the second if I get test: argument expected. My... (4 Replies)
Discussion started by: Charles Swart
4 Replies

3. Shell Programming and Scripting

Nested if else

Hi, i m trying to create script which logic is like below. if ; then x=`cat /tmp/testoutput.log | grep STOP | wc -l` y=`cat /tmp/testoutput.log | grep RUN | wc -l` if ; then echo "process stop" if ; then echo "process running " else echo "file not found" fi ----------------... (2 Replies)
Discussion started by: tapia
2 Replies

4. UNIX for Dummies Questions & Answers

Nested If in Unix

Hi!! LookVar=`find . -name "${input}" | wc -w` if then cd $input rm -f * ftp -n -i $HostName << EOF quote USER $User quote PASS $Password cd $Path SoLookVar=`find . -name "${input}" | wc -w` echo $SoLookVar if then cd $input mget ./* bye EOF chmod 775 ./* (12 Replies)
Discussion started by: Afsana
12 Replies

5. Shell Programming and Scripting

Perl nested if statement

I'm just having a bit of trouble running this code. It tells me that there's a syntax error on line 29. Any help appreciated. #!/usr/bin/perl # # Phone Book Application # %phonebook = ( "Wayne", '34687368', "Home", '378643287', "Work", '017374637', "School",... (2 Replies)
Discussion started by: cabaiste
2 Replies

6. Shell Programming and Scripting

If-statement nested in case

I'm trying to write case statements with 'if statements' embedded inside of them. I'm using the korn shell but it's not functioning. If I want to see if a string exists in a file and then perform an action, what would be the best way to do this? For file "asg51fin" to delete a line if a... (1 Reply)
Discussion started by: dazeman27
1 Replies

7. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

8. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

9. Shell Programming and Scripting

while read loop w/ a nested if statement - doesn't treat each entry individually

Hi - Trying to take a list of ldap suffixes in a file, run an ldapsearch command on them, then run a grep command to see if it's a match, if not, then flag that and send an email alert. The list file (ldaplist) would look like - *********** o=company a o=company b *********** **... (7 Replies)
Discussion started by: littlefrog
7 Replies

10. Shell Programming and Scripting

Nested Arrays

Hi, I'm trying to implement nested arrays in ksh. i've the follwing arrays SRV=\ "SRV1 "\ "SRV2 " SRV1=\ "MD11 "\ "MD12 " SRV2=\ "MD21 "\ "MD22 " MD11=\ "ABC " (5 Replies)
Discussion started by: guysporty
5 Replies
Login or Register to Ask a Question