"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.

$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

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?

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?

"

#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

"

From jim_mcnamara's post, it's [ $? -eq 0 ] not [ $Dating -eq 0 ]

You don't actually need "Dating" variables (pun intended :D)

Ok thanks that fix my script.

That's better done as

if grep -q "May" DatingTest
then
        echo "Matched"
else
        echo "Didn't match"
fi

You seldom need to check the value of $? directly.

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

How to do this depends on what your version of 'free' outputs, since it's not the same everywhere. Can you show it?

There is much variation in the ps command and commands to find out free memory vary. Please tell the forum some detail about your computer environment:
What Operating System and version are you running?
What Shell do you use?

Edit: @Corona688 - great minds think alike!

When you answer Corona688's question, please show the command typed and the matching output. If you don't know the command, please ask.

The Operating system is my company's own OS. The output of "free" is like
"
MemTotal: XXXXX KB
MemFree: XXXXX KB
Buffer: XXX KB
"

I already know how to deal with the "ps" command, but my issue is getting the MemFree from the free command and creating an if statement based off the MemFree without having the do something like
"
free > Memory.txt
grep MemFree Memory.txt
if ( #some command that checks MemFree, and if MemFree is less then or equal to 1000 KB)
then
ps > psLog.txt #i know this part works just fine
else
"

Does it respond to:

uname -a   # Blotting anything confidential with X's

If so, please post what Operating System and version you are running. If not, please be reminded that this is the unix and Linux forum.

Please post the free command with any parameters and the output in code tags so we can see the exact format complete with every space character and the exact case of any alphabetic characters. I bet that the command does not output double-quote characters.

@Amzerik:

You might think that you have asked a very specific question and all you wanted is a specific answer. Now my colleagues are questioning your motives. Please, let me explain.

We oftenly have the case that question "how can i achieve X" is asked because what is really needed is Y but the thread opener is under the (false) impression that X is necessary to achieve Y. It may be that in fact X is a suboptimal way to achieve Y and as soon as we recognize that we try to help the thread opener achieve his goal instead of answering his question. Therefore all the questions about your OS, the version, your true goal, etc.

Regarding your initial question: "if" takes a list of commands as an argument and branches depending on its exit code. The following will work:

if true ; then
    echo "tested to TRUE"
else
     echo "tested to FALSE"
fi

(Of course, the else-part will never be executed because "true" always returns 0, whereas "false" always returns 1. Replace "true" with "false" to see this effect. "true" and "false" are both commands built for exactly this purpose.)

In shell programming, logical TRUE and FALSE are handled like in C: 0 is TRUE, everything else is FALSE. Now "grep -q" returns "0" when it finds the search pattern in its input and "1" if not. Therefore we could write:

if grep -q "foo" /path/to/file/containing-foo ; then
    ...

and the if-branch would be executed if the file contains "foo" while the else-part will be executed of the file doesn't contain "foo".

Now, how does that fit in with what we usually see as if-statements?

Originally, there was a command "test": it was fed an expression which evaluated to TRUE or FALSE and returned an exit status depending on this. See the man page for "test" for details. The following should be obvious to you now ("-ge" means "greater or equal"):

if test 1 -ge 0 ; then
     echo "yes, it worked"
else
     echo "something went wrong"
fi

(By the way, this is why it is a really BAD idea to name a program "test". It will collide with the original "test" from the system, which is still there.)

Now, to let shell code look more like traditional programming languages someone came up with the clever idea to make "[" a link to "test". "test" could now be invoked by "[" too and the following could be written:

if [ 1 -ge 0 ; then
     echo "yes, it worked"
else
     echo "something went wrong"
fi

but that still looked counter-intuitive, because the opening "[" missed a counterpart. Therefore "test" itself was modified to have "]" as its (last) argument (well, actually only its "["-variant was changed that way). Now the necessary line looked like what we are used to:

if [ 1 -ge 0 ] ; then
     echo "yes, it worked"
else
     echo "something went wrong"
fi

Notice, this is the reason, why "[" has to be surrounded by spaces. if [ $int1 -ge $int2 ]; will work, whereas if [$int1 -ge $int2]; won't. It is for the same reason that ls -l works but ls-l doesn't.

I hope this helps.

bakunin

I sincerely doubt they have their own private OS. They may have modified it from something else but it must be something.

That looks a lot like 'cat /proc/meminfo' from linux, actually.

Relying on 'MemFree' in Linux is a bad idea because it's frequently "wrong". You might see "MemFree: 10 KB" and panic, even though it's got "Cached: 1000000 KB" -- meaning, a gig of memory on-tap that the kernel will happily convert into free at need. I've seen sysadmins rebooting their systems daily whenever 'free' creeps low, even though nothing was wrong -- a mostly-idle system will tend to convert 'free' into 'cached' over time, since memory that sits around doing nothing at all is useless.

Without knowing which system you have, we can't understand the meaning of what your 'free' command prints, and can't offer advice that will work the way you want it to.

Thanks for all your help everyone, but i found out a way to do it myself.

@Corona688

Many companies build their own OS to allow more flexibility with memory. If a company builds a product that has low memory having bash or some other OS with many commands would use up too much of the memory due to many unnecessary commands. The best thing would be to build their own OS with their own commands. Doing so would allow them to be more efficient with their memory usage.