if condition for variable existing in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if condition for variable existing in file
# 1  
Old 02-16-2009
if condition for variable existing in file

Hi, Sorry for the dumb question but I can't seem to figure this one out. I want to write an if condition that basically checks if my variable is in a file. So far I have the following:

if [[ grep var file ]]
then
do something
else
do something else
fi

So essentially if the grep returns something then I want it to do the first condition else do something different but when I run it it keeps erroring on me
# 2  
Old 02-16-2009
your very close. you should inline the command into the condition.

# -q = quiet mode. just set return code
# -w = word match - may or may not apply to your situation
Code:
if grep -q -w var file
then
   do something
else
   do something else
fi

# 3  
Old 02-16-2009
Thanks for the quick response but do I need to have the [[ ]] around the condition? I'm getting a syntax error:

./Compare.sh: line 9: conditional binary operator expected
./Compare.sh: line 9: syntax error near `-q'
./Compare.sh: line 9: ` if [[ grep -q -w var file ]]'
# 4  
Old 02-16-2009
no. you should use exactly what I posted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell variable as awk condition

Hi, I'm trying to automate part of a script which uses awk to grab out some lines of a log file based on certain fields matching. For example, my log file looks something like the following (but 1000s of lines): 1 Tom 123 abc 345 2 Dick 345 abc 678 3 Harry 567 abc 345 4 Tom 345 cde 345... (3 Replies)
Discussion started by: chrissycc
3 Replies

2. Shell Programming and Scripting

Not able to assign 0 (zero) to the existing variable

Hi, I have a variable which read the value from the lines, and if it is empty string (5 bytes), then I have to assign this variable to zero "0", it able to detect the data is empty by showing me the message "empty", but not able to assign it as zero. WTHP=`echo "$file" | cut -c158-162` if ]... (5 Replies)
Discussion started by: newbie2011
5 Replies

3. UNIX for Dummies Questions & Answers

Variable in IF condition

In AIX, why is it variable VAR becomes true in the condition despite VAR was unassigned and not equal to 1? In Linux, it was traced as an error as VAR is not declared as variable and expecting an integer as argument. one.sh VAR=1 if ; then echo "One" fi if ; then echo "Two"... (5 Replies)
Discussion started by: budz26
5 Replies

4. Shell Programming and Scripting

Script with variable and condition

Hello newbies question... I just need a script able to launch a command when a condition is matched : #!/bin/ksh SIZ = 'cat /nurp/control.lst|wc -l' if test "$SIZ" -gt 0 then echo 1 else echo 2 fi but I receive errors messages ./t2: SIZ: not found 2 whats wrong ? (5 Replies)
Discussion started by: vdurieu
5 Replies

5. Shell Programming and Scripting

Compare the two variable with if condition

Please help me with this: I need to compare two values in if condition in shell script but its goes always to else condition: TIME_CHECK=PM TIME-CLOCK=PM if ; then echo "You have access!" else echo "ACCESS DENIED!" fi (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

6. UNIX for Dummies Questions & Answers

creating a new variable from existing data

Hello, I have the following data set: TRAIT DOSE 40 0.4 30 0.3 95 1.2 120 1.7 85 1.4 136 1.8 134 1.8 40 0.4 30 0.3 95 1.2 120 1.7 85 1.4 136 1.8 134 1.8 40 0.4 30 0.3 95 1.2 (2 Replies)
Discussion started by: wolf_blue
2 Replies

7. Shell Programming and Scripting

Change existing variable value only user enters non-empty string.

I haven't checked any installation script to see how this is done.. But I could not even do following simple task. How do I Change existing variable value only when user enteres non-empty string. ? #!/usr/bin/ksh uid="scott" # Assign new value user enters to uid, else leave it... (7 Replies)
Discussion started by: kchinnam
7 Replies

8. Shell Programming and Scripting

Bash: if condition as variable

How can I use a variable that has the conditions for the if statement stored in it? my test script condition=" || || " if "$condition" then echo "true" else echo "false" fi output $ ./test2.sh ./test2.sh: line 3: || || : command not found false (2 Replies)
Discussion started by: curlee2002
2 Replies

9. Shell Programming and Scripting

folder existing and file existing

I want to look into a folder to see if there are any folders within it. If there are, I need to check inside each folder to see if it contains a .pdf file So If /myserver/myfolder/ contains a folder AND that folder conatins a .pdf file do X Else do Z I may have multiple folders and... (4 Replies)
Discussion started by: crowman
4 Replies

10. UNIX for Dummies Questions & Answers

New Variable with Existing variable( Urgent)

hi all, i want use the variable value as a new variable name. print output of new variable. for i in COMPUTER1 COMPUTER2 do flag_name=${i}_FLAG eval ${flag_name}=123 echo $i'_FLAG' done output is COMPUTER1_FLAG COMPUTER2_FLAG i need output as 123 123 (2 Replies)
Discussion started by: arvindng
2 Replies
Login or Register to Ask a Question