While loop only if a condition is met


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users While loop only if a condition is met
# 1  
Old 05-09-2011
While loop only if a condition is met

All,

I wrote the following section of code (which logically in PHP would of worked):

Code:
tmpPATH=${1}
tmpTAG=${2}
 
if [ "${tmpPATH}" == "" -a "${tmpPATH}" == "" ]
then
    while read tmpTAG tmpPATH
    do
fi 
 
echo $tmpTAG 
echo $tmpPATH 
 
if [ "${tmpPATH}" == "" ]
then
        done < ./config.cfg 
fi

When run I get the following error:

Code:
tester[5]: 0403-057 Syntax error at line 8 : `fi' is not expected.

Now I know I can write it like this (so this is not a request for another solution, but a more can the above be achieved in terms of syntax code flow - injecting the while loop) my current solution:

Code:
main_func(){
    echo $tmpTAG
    echo $tmpPATH
}
 
tmpPATH=${1}
tmpTAG=${2}
 
if [ "${tmpPATH}" == "" -a "${tmpPATH}" == "" ]
then
    while read tmpTAG tmpPATH
    do
        main_func
    done < ./config.cfg
else
    main_func
fi

# 2  
Old 05-09-2011
in main_func() you use variables that have not been declared.
The 2 lines should be put before the main_func
Code:
tmpPATH=${1}
tmpTAG=${2}

By the way instead of reading the default values from ./config.cfg to setup tmpPATH and tmpPATH you may setup this like

read default values from confi file

Code:
<./config.cfg read dT dP

Use the default setting notation :

Code:
tmpPATH=${1:-$dP}
tmpTAG=${2:-$dT}

so that if $1 and/or $2 are given, their value will be used , but if empty then the default values will be used.

example :
Code:
# cat k.cfg
defaultTag defaultPATH

Code:
# cat k

<k.cfg read dT dP

P=${1:-$dP}
T=${2:-$dT}

echo $P $T

Code:
# ksh k
defaultPATH defaultTag
# ksh k arg1 arg2
arg1 arg2
# ksh k arg1
arg1 defaultTag

# 3  
Old 05-09-2011
thanks for the reply.

Code:
tmpPATH=${1}
tmpTAG=${2}

Don't have to be before the main function, the main function can get called with either the file or passed variables.

The problem I had with the script is I want an easy way to say, if values are passed i.e.:

Code:
./tester -d /tmp/PATH -t CAD

Then just send those and don't read the ./config.cfg file. If they are empty then read the file and loop (as would be mutiple iterations in there).

So the second solution I have works (and is what I am using). What I wondered, can you have the while loop enabled depending on a condition, and if that condition is not met then just run the code which would of been in while loop.

I know I have acieved this in the second example, but from PHP I would of been able to solve it using the first method. Shell seems to require loops / cases to have to be comepleted and not cross into other if / while loops (if that makes any sense)
# 4  
Old 05-09-2011
refer to this (here below an extract)
Code:
while getopts ":ab:c" opt
do 
    case $opt in
        a ) process option -a ;;
        b ) process option -b $OPTARG is the option's argument ;;
        c ) process option -c ;;
        \? ) print 'usage: bob [-a] [-b barg] [-c] args ...' exit 1 ;;
    esac
done
shift $(($OPTIND - 1))
normal processing of arguments ...

# 5  
Old 05-09-2011
My script already utilises getops, the -d and -t can be passed or not as per my example. That allows a user to specify a certain path or use a config file if there are mutiple paths (this is a backup script). Then the final option is -b for backup, or -r for restore. Then based on the options in the file or what has been passed (-d and -t) determines the directories and the tag for the engine. The main function for backup and restore then loops through accordingly..

Like I said I have a solution just wondered if there was a way to "inject" the while loop as per the first example - like you can in other languages.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk - print when condition is met

I have a file.txt containing the following: Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT Length=100 Score E Sequences producing significant alignments: (Bits) Value ... (2 Replies)
Discussion started by: tons92
2 Replies

2. Shell Programming and Scripting

Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low, otherwise print Not low in $(NF + 1). I am not sure how to correctly add this. Thank you :). if(low <= $2 && $2 <=... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Need help on how to append on the filename when condition met.

Hi All, Seeking for your assistance on how to append the specific string when $3 condion met. ex. file1.txt ar0050046b16,5,888,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55 expected output:... (5 Replies)
Discussion started by: znesotomayor
5 Replies

4. Shell Programming and Scripting

Getting the records once condition met

Hi All, Seeking for your assistance to get the records once the $2 met the condition. Ex. file 1.txt 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 123232,11-Aug-2015 08:33:37 PM,4234324,1321432,34364 Output: 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 What i did... (5 Replies)
Discussion started by: znesotomayor
5 Replies

5. Shell Programming and Scripting

Print specific field when condition met

Hi All, Seeking for your assistance to print all the specific field when the condition met. Ex: file1.txt 1|203|3|31243|5341|6452|623|22|00|01 3|45345|123214|6534|3423|6565|643|343|232|10 if field 1 = 1 and field 3 = 3 and field 5 = 5341 and field 6 = 6452 it will print from $1 to $10.... (2 Replies)
Discussion started by: znesotomayor
2 Replies

6. Shell Programming and Scripting

Awk. Abort script if condition was met.

I want to abort script if input variable matched first field in any line of a file. #!/bin/sh read INPUTVAR1 awk "{if(\$INPUTVAR1 == $1) x = 1} END {if(x==1) print \"I want to abort script here\"; else print \"OK\"}" /etc/some.conf I tried "exit" and system("exit") but no luck. (1 Reply)
Discussion started by: urello
1 Replies

7. Shell Programming and Scripting

Delete if condition met in a column

i have a table like this: id, senderNumber, blacklist ----------------------------- 1 0835636326 Y 2 0373562343 Y 3 0273646833 Y and I want to delete automatically if a new inserted row on another table consist anything on senderNumber column above using a BASH Script I... (9 Replies)
Discussion started by: jazzyzha
9 Replies

8. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies

9. Shell Programming and Scripting

How to break a loop if condition is met

I am having trouble figuring this code I want to grep a text from a file and if it match certain text it break out of the loop or it should continue searching for the text Here is what I have written but it isn't working while true f=`grep 'END OF STATUS REPORT' filename` do if ... (9 Replies)
Discussion started by: Issemael
9 Replies

10. Shell Programming and Scripting

Send email to user when condition met

Hi all, I plan to write a shell script to inform users on their task when certain condition met. example: If a then email user on action a else email user on action b. I'm pretty new in scripting, appreciate any suggestions. Thanks. (4 Replies)
Discussion started by: *Jess*
4 Replies
Login or Register to Ask a Question