"if" statement based off "grep"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "if" statement based off "grep"
# 8  
Old 06-06-2012
How to do this depends on what your version of 'free' outputs, since it's not the same everywhere. Can you show it?
# 9  
Old 06-06-2012
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!

Moderator's Comments:
Mod Comment Moved to Shell Scripting board because not a Programming question


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

Last edited by methyl; 06-06-2012 at 04:13 PM..
# 10  
Old 06-06-2012
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
"
# 11  
Old 06-06-2012
Quote:
The Operating system is my company's own OS.
Does it respond to:
Code:
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.

Last edited by methyl; 06-06-2012 at 05:56 PM.. Reason: Found a Linux command called "free". Try to be unambiguous.
# 12  
Old 06-07-2012
@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:

Code:
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:

Code:
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"):

Code:
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:

Code:
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:

Code:
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

Last edited by bakunin; 06-07-2012 at 09:38 AM..
# 13  
Old 06-08-2012
I sincerely doubt they have their own private OS. They may have modified it from something else but it must be something.
Quote:
Originally Posted by Amzerik
The output of "free" is like
"
MemTotal: XXXXX KB
MemFree: XXXXX KB
Buffer: XXX KB
"
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.
# 14  
Old 06-11-2012
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.
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