Help with nested "if"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with nested "if"
# 1  
Old 04-08-2013
Wrench Help with nested "if"

Hi, i just wanna ask for some help with this
Code:
while true
do
  clear
  date
  echo AIX 630 AMOUNT OF FEPs
VAR=`ps -eaf | grep fep2 | grep cajaBMSz | wc -l`


{
 if [ $? -gt 10 && -le 20 ]
 then
    if [ $? -ge 21 ]
     then
        printf "\033[01;31m%s\033[00m\n" "$VAR"
     else
        printf "\033[01;33m%s\033[00m\n" "$VAR"
    fi
 else
        printf "\033[01;32m%s\033[00m\n" "$VAR"
 fi
}

done;

What i want is this.. if the value returned by
Code:
ps -eaf | grep fep2 | grep cajaBMSz | wc -l

is between the values i suggested the script must output the colors i selected... but instead i get the "mon55z[22]: test: 0403-021 A ] character is missing." message. mon55z is the name of my script BTW...

thanks in advance
# 2  
Old 04-08-2013
Take a look at
Code:
 if [ $? -gt 10 && -le 20 ]

I think
Code:
 if [ $? -gt 10 && $? -le 20 ]

However, how can something be gt 10 and lt 20
with your next command checking for it gt 21?

The && is for and, || is for or.
This User Gave Thanks to joeyg For This Post:
# 3  
Old 04-08-2013
Why are you using the special shell variable $? that holds the exit status of last command executed?

That is not correct. It will be zero if the command executed successfully otherwise non-zero.

Use the variable VAR instead:
Code:
if [ $VAR -gt 10 ] && [ $VAR -le 20 ]

This User Gave Thanks to Yoda For This Post:
# 4  
Old 04-08-2013
I agree with Yoda here. Smilie

You can also use,
Code:
if [ $VAR -gt 10 -a $VAR -le 20 ]

This User Gave Thanks to PikK45 For This Post:
# 5  
Old 04-08-2013
Hi joeyg,

thanks for the quick response and thank you for your observation, i've already modified that and what i got now is this:

Code:
{
 if [ $VAR -gt 11 ]
 then
    if [ $VAR -ge 17 ]
     then
        printf "\033[01;31m%s\033[00m\n" "$VAR"
     else
        printf "\033[01;33m%s\033[00m\n" "$VAR"
    fi
 else
        printf "\033[01;32m%s\033[00m\n" "$VAR"
 fi
}

the error is gone, and the scrip is working all right. thank you !!!

---------- Post updated at 10:07 AM ---------- Previous update was at 10:05 AM ----------

Quote:
Originally Posted by PikK45
I agree with Yoda here. Smilie

You can also use,
Code:
if [ $VAR -gt 10 -a $VAR -le 20 ]

yes, i modified that too, thanks Yoda Smilie
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

Intermittent "cp: cannot stat" error with nested loop

I have a bash script that has been running (on SUSE 9.3) dozens of times over the past couple of years without error. Recently it has been hitting intermittent “cp: cannot stat FILE: No such file or directory” errors. The script has nested loops that continuously process files in a... (2 Replies)
Discussion started by: jcart
2 Replies

5. Solaris

The slices "usr", "opt", "tmp" disappeared!!! Help please.

The system don't boot. on the screen appears following: press enter to maintenance (or type CTRL-D to continue)...I checked with format command. ... the slices "0-root","1-swap","2-backup" exist. ...the slises "3-var","6-usr" -unassigned. :( (16 Replies)
Discussion started by: wolfgang
16 Replies

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

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

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 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