Condition checking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Condition checking
# 8  
Old 06-18-2002
Kevin Pryke
yes, it's worked, but -x option not within the standard grep, it must set path to /usr/xpg4/bin on my system...solaris
and the data file must be write item name line by line...

How about SED, can u make it?
# 9  
Old 06-19-2002
They may be a very simple way of doing it in sed but I don't use sed really, anyone else?

Also I don't use solaris (I'm on sco using a bourne shell) but I imagine there is an equivalent option to the -x.

if you type

man grep

it will give you a list of options, look for one that says something like

-x Displays only exact matches of an entire line.



I'm not quite sure what you mean by "the data file must be write line by line". If it's the output of the echo that you want to write to a file, do this

echo $name >> outputfile.dat

which will append the $name to a file outputfile.dat. However you must remember to initialise the file each time you run the script so at the start put

> outputfile.dat

which will empty it.


Post back with any further problems.
# 10  
Old 06-19-2002
I agree with Perderabo.

A case statement will be the best. Because you will only have to write a short code statement for each one and any thing that doesn't match will kick out to the main menu.

Case is superior especially when you need to do a great deal of validation or have many different tasks to choose from.

I would also use a flag to turn on when any one of the parameters are met so that any subsequent answers for that part of the case will be ignored and kick to the menu again. If it matches your user shouldn't need to answer it again.

Case is very easy to format, don't hesitate to use it.

case $VAR in

VAR1 ) Do some statement ;;

VAR2 ) Do different statement ;;

VAR3 ) Do another different statement;;

* ) Do default statement or return to menu and exit from CASE ;;

esac








Smilie
# 11  
Old 06-20-2002
HI Kelam_Magnus
i don't think so, the wildcard can be represent all combination which not match the string, but how about the rest, if you must excatly match word by word in string? assume, match any 1000 numbers in 1-100000, how to write the code, 1 by 1?

To: Kevin Pryke

I mean that the file would be like this format:

...
word1
word2
word3
word4
...


Yes, it will not function or prompt error if you append a word beside the word1, you be told" word 1 not found"


...
word1 word100
word2
word3
word4
...
# 12  
Old 06-20-2002
The wildcard in the case statement will only catch $VAR when it does not match as the ;; at the end makes it skip the rest of the checks when it hits a match.

It seems to me you'll either have to type all your codes into a case statement or into a file - it's up to you really.



My way of doing it using a grep was just a suggestion of another way, I thought you might already have a file in the format of

word 1
word 2
word 3
word 4
etc.

if you have a file

word 1 word 2
word 3
word 4

I know it won't work, you may be able to manipulate if somehow with sed/awk but I'm not sure.

I'm off work till Monday now, the World Cup is calling me.
# 13  
Old 06-20-2002
Case is superior, even though coding may be long

trynew,

Using Case may mean you will have to write long code. If you code checking is as extensive as you suggest, I can't think of another way to do it. A for loop or a while loop will have to contain many if statements. Case seems to me to be the best way to go.

Here is my further explanation...

For case to work any input must be 1 variable string at a time. if you have 1000 variables, you will need 1000 groups of lines in your case statement.

If you are executing a loop to read in data, any data on one line of your input must match a particular case element. Otherwise it will fall out of the case with the *) statement.

If your input file has more than one column, I suggest this. Of course if you have a delimeter, put a -F? after the awk.

cat filename | awk '{ print $1 } >> newfile.out
cat filename | awk '{ print $2 } >> newfile.out
cat filename | awk '{ print $3 } >> newfile.out
cat filename | awk '{ print $4 } >> newfile.out

And so on, until you have cleared all the columns down to only one column. You will need this in for the case to work properly, if I read your data correctly.

You need to change:

word1 word2 word3
word4 word5 word6

Into:

word1
word4
word2
word5
word3
word6.....


This will allow the looping statement to feed into the case statement.

Hope this helps... Please get back with any other info.



Smilie

Last edited by Kelam_Magnus; 06-20-2002 at 12:29 PM..
# 14  
Old 06-20-2002
Thanks, Kelam_Magnus and Kevin Pryke

Got to be relax for today, watch world cup... England vs Brazil

I bet england will win!!! Beckham..owen..oh yes!!!goal................
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Condition checking issue while if

hi, i am using a simple condition end_ct=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID << EOF select description from bravo_statistics where trunc(time_stamp)=trunc(sysdate)-1 and description='END CAT'; EOF` echo $end_ct; echo... (30 Replies)
Discussion started by: lovelysethii
30 Replies

2. Shell Programming and Scripting

Checking for null condition in a UNIX variable

i have this code for i in `cat sql_output.txt` do -- some script commands done sql_output.txt has 1 column with employee_ids If the sql_output.txt is null then the do loop should not execute. How can i implement this. for i in `cat sql_output.txt` If i is null or empty then ... (5 Replies)
Discussion started by: rafa_fed2
5 Replies

3. Shell Programming and Scripting

Condition checking in UNIX

i have a script where i have to find the age of a file, if then echo "dnb file is present for the monthly load" >> $RUNLOG dnb="1" else echo "dnb file has not arrived yet" > $ERRLOG dnb="0" fi i know the file is available so... (3 Replies)
Discussion started by: lovelysethii
3 Replies

4. Shell Programming and Scripting

Checking file existence along with condition

Hi am trying to write a script which find the existence of a file from a find command output and perform a task if the file exists. Help me out with the correct syntax . Am trying with the following one but unable to get the output. if then <some tasks> else echo "file not exists" fi (5 Replies)
Discussion started by: rogerben
5 Replies

5. Shell Programming and Scripting

Multiple condition checking in bash

Hi All, I am trying to check if two variables have value assigned to it. i am doing it like if ] then echo "Please specify either single hostname or host file for the report" usage exit fi But its not working for it.Even i specify values for both variables it dont go... (6 Replies)
Discussion started by: kailash19
6 Replies

6. Shell Programming and Scripting

create separate file after checking condition..

Problem : I want to create a separate file for country list if condition is true. Please help. ***************************************************** Input file: SV-INCR-139302-365540488-201104090934.sqllog SV-INCR-1082-552793184-201104040805.sqllog SV-INCR-1077-855045741-201104040805.sqllog... (4 Replies)
Discussion started by: humaemo
4 Replies

7. Shell Programming and Scripting

Cutting specific line of a file by checking condition

testfile.csv 0","1125209",,"689202CBx18888",,"49",,,"NONMC",,,,,"01112010",,,,,,,"MTM- "1","",,"689202ABx19005",,"49",,,"NONMC",,,,,"01072010",,,,,,,"MTM- testfile.csv looks like above format if the second column is null then get 23rd column and store in a different varible .. add all the... (1 Reply)
Discussion started by: mgant
1 Replies

8. Shell Programming and Scripting

Unzip file By checking condition.

Hi.. Gurus I Have a list of .zip files in a directory. I want to check whether each .zip file having some particular file or not (say .jsp) if it's having .Jsp file then create a directory as per the .zip file and extract the content to that directory except the .jsp file, If .zip not having... (3 Replies)
Discussion started by: posix
3 Replies

9. Shell Programming and Scripting

sed solution for condition checking

Hi all , Recently i came across this in FAQ's. I have a file cat rem.txt sreedhar 20 sreedhar 10 sreedhar 15 sreedhar 18 sreedhar 16 sreedhar 30 I have to replace sreedhar with "Sridhar" if the second parameter is > 18. I need to do it in "sed" only. I am wondering how this... (4 Replies)
Discussion started by: panyam
4 Replies

10. Shell Programming and Scripting

Checking condition inside the loop

Hi all, I have one clarification i am using the loop which will process for each record .suppose there is f ailure in the first record it need to send mail and process the next .my code: defcount=`cat <filename>|wc -l` while ] do if <some condiotion> then echo "mail" fi done so... (1 Reply)
Discussion started by: ithirak17
1 Replies
Login or Register to Ask a Question