"if" statement based off "grep"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "if" statement based off "grep"
# 1  
Old 06-04-2012
"if" statement based off "grep"

Hello,

I am somewhat new to Linux/Unix. I am currently working on a shell script that is suppose to cat a file, grep the same file for a certain line, if that line is found save the file in a different location, else remove the file.

This is a rough example of what I want.

Code:
$Dating = False

date > DatingTest | grep "May" DatingTest

#Something changing the Dating variable to True based off the grep

if ($Dating = True)
DatingTest > SuccessDate

else

rm DatingTest

any help would be grateful.

Thank you

Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 06-04-2012 at 11:51 AM.. Reason: code tags
# 2  
Old 06-04-2012
Code:
grep -q May /path/to/somefilename  # silently check for the word May
if [ $? - eq 0 ] ; then                     # it has the word May in the file
   mv /path/to/somefilename   /new/path/to/somefilename
else
   rm /path/to/somefilename            #delete the file
fi

I have a feeling that you should tell us what you want to do not how you decided to do it. For example, how did you come up with the search word: May? Will you need to run the ocde sometime later and have Jun as the search word?
# 3  
Old 06-04-2012
Thank you

Thank you very much for your help.

The code I added was only a simple example of what I wanted. I was just using "May" as an example because it may be an output of "date", but it may not. My real code would not be using the "date" command.

Thank you once more for your help.

---------- Post updated at 04:00 PM ---------- Previous update was at 11:03 AM ----------

So my code should look something like this?

"
Code:
#Creating variable
Dating = False

#Taking output of Date and putting it into DatingTest
date > DatingTest                      

#Using "grep" to search for the word "May" within DatingTest
grep -q "May" DatingTest             

#If "May" was found then move DatingTest over to usb
if [ $Dating - eq 0 ] ; then           
   mv DatingTest /mnt/usb

#Else delete DatingTest file
else
   rm DatingTest

"

Last edited by Scrutinizer; 06-05-2012 at 04:51 AM.. Reason: code tags
# 4  
Old 06-05-2012
Quote:
Originally Posted by Amzerik
if [ $Dating - eq 0 ] ; then
From jim_mcnamara's post, it's [ $? -eq 0 ] not [ $Dating -eq 0 ]

You don't actually need "Dating" variables (pun intended Smilie)
# 5  
Old 06-05-2012
Ok thanks that fix my script.
# 6  
Old 06-05-2012
That's better done as
Code:
if grep -q "May" DatingTest
then
        echo "Matched"
else
        echo "Didn't match"
fi

You seldom need to check the value of $? directly.
# 7  
Old 06-06-2012
Hello again,

I was thinking since we were already talking about "if" how would i go about setting up an if statement with free that would allow me to do an if statement based off how much free memory is remaining.

something like

"
free > Memory.txt

if (FeeMem <= 1000)
then
ps > psinfo.txt
else
rm Memory.txt

exit
"

but i do not want to have to save "free" output into a file just to find the free memory information on it to place it into my "if" statement. Everything must be done without pipes.

any advice would be grateful.

Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

8. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question