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:
(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:
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"):
(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:
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)