Sponsored Content
Top Forums UNIX for Beginners Questions & Answers How to write a Boolean variable which succeed and failed inside the if loop in shell script ? Post 303040728 by Scrutinizer on Tuesday 5th of November 2019 04:10:01 PM
Old 11-05-2019
This will only check if one of the conditions is true, but not which one is true. If you want to find out, which of the expression is true, you will need to check them one by one..

Which means something like this:
Code:
if (( dm > 0 || cl == 100 || ad == 0 || me > 8 || cp > 0 )); then
  if (( dm > 0 )); then
    echo "dm > 0 "
  fi
  if (( cl == 100 )); then
    echo "cl == 100 "
  fi
   if (( ad == 0 )); then
    echo "ad == 100"
  fi
  if (( me > 8 )); then
    echo "me > 8 "
  fi
  if (( cp > 0 )); then
    echo "cp > 0 "
  fi
else
  echo "None of the conditions are true"
fi

--
For a shorthand way, you could try something like this:
Code:
if (( dm > 0 || cl == 100 || ad == 0 || me > 8 || cp > 0 )); then
  (( dm > 0 ))    && echo "dm > 0 "
  (( cl == 100 )) && echo "cl == 100 "
  (( ad == 0 ))   && echo "ad == 100"
  (( me > 8 ))    && echo "me > 8 "
  (( cp > 0 ))    && echo "cp > 0 "
else
  echo "None of the conditions are true"
fi


Last edited by Scrutinizer; 11-05-2019 at 05:27 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies

2. Shell Programming and Scripting

Changing value of a variable inside a shell script

I have a continous polling happening inside a shell script on AIX. This actually calls a PL/SQL block. Is there a way I can set up a variable or pass an interrupt to end the script gracefully. I cant read from the config once the job starts running. Ideally I should change value of a variable and... (1 Reply)
Discussion started by: kshyju
1 Replies

3. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

4. Shell Programming and Scripting

scope of a Variable inside shell script

hi all, i'm using the following script, Status=1 Function_do () { while read line; do if ; then #echo $line if ; then Status=0 echo " LINKINK ERROR " fi fi done < ldd.log } Function_do (4 Replies)
Discussion started by: vij_krr
4 Replies

5. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

6. Shell Programming and Scripting

help in running while loop inside a shell script

I have an input file at ./$1.txt with content of seq numbers like : 1234567890 1234589700 . . so on.. I need to take each seq nbr from the input file ,run the query below: select ackname,seqnbr from event where event_text like '%seqnbr( from the input file)' and redirect the... (11 Replies)
Discussion started by: rkrish
11 Replies

7. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

8. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

9. UNIX for Beginners Questions & Answers

Variable inside while loop got reset

hi, I am using hp unix server and not getting variable output present inside the while loop. I have tried changing the code and need to verify whether it is proper practice of code. I am expecting the output of varible RUN_FILE 3 to TRUE which i get inside the while loop. RUN_FILE 1=TRUE... (8 Replies)
Discussion started by: gowthamsoft
8 Replies

10. Shell Programming and Scripting

Shell script use two variable in for loop

I want to create a shell script to add a user and modify its comment field mentioned in a file. 1. File value:- username comment field value xyz123 xyztesting abc123 abctesting def123 deftesting 2. i am using below loop to create user... (2 Replies)
Discussion started by: Anil
2 Replies
break(1)							   User Commands							  break(1)

NAME
break, continue - shell built-in functions to escape from or advance within a controlling while, for, foreach, or until loop SYNOPSIS
sh break [n] continue [n] csh break continue ksh *break [n] *continue [n] ksh93 +break [n] +continue [n] DESCRIPTION
sh The break utility exits from the enclosing for or while loop, if any. If n is specified, break n levels. The continue utility resumes the next iteration of the enclosing for or while loop. If n is specified, resume at the n-th enclosing loop. csh The break utility resumes execution after the end of the nearest enclosing foreach or while loop. The remaining commands on the current line are executed. This allows multilevel breaks to be written as a list of break commands, all on one line. The continue utility continues execution of the next iteration of the nearest enclosing while or foreach loop. ksh The break utility exits from the enclosed for, while, until, or select loop, if any. If n is specified, then break n levels. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be exited. The continue utility resumes the next iteration of the enclosed for, while, until, or select loop. If n is specified then resume at the n- th enclosed loop. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be used. On this manual page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words that follow a command preceded by ** that are in the format of a variable assignment are expanded with the same rules as a variable assignment. This means that tilde substitution is performed after the = sign, and also that word splitting and file name generation are not performed. ksh93 break is a shell special built-in that exits the smallest enclosing for, select, while, or until loop. It also exits the nth enclosing loop if n is specified. Execution continues at the command following the loop or loops. If n is specified, it must be a positive integer >=1. If n is larger than the number of enclosing loops, the last enclosing loop is exited. continue is a shell special built-in that continues execution at the top of the smallest enclosing for, select, while, or until loop, if any; or of the top of the nth enclosing loop if n is specified. If n is specified, it must be a positive integer >=1. If n is larger than the number of enclosing loops, the last enclosing loop is used. On this manual page, ksh93(1) commands that are preceded by one or two + symbols are special built-in commands and are treated the follow- ing ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Built-in commands are not valid function names. 5. Words following a command preceded by ++ that are in the format of a variable assignment are expanded with rules as a variable assignment. This means that tilde substitution is performed after the = sign and field splitting and file name generation are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), exit(1), ksh(1), ksh93(1), sh(1), attributes(5) SunOS 5.11 8 Apr 2008 break(1)
All times are GMT -4. The time now is 01:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy