How to find min of two variables in linux
ksh, linux, shell
Solution
There is no function, but you can create it:
(( $a <= $b )) && echo "$a" || echo "$b"
The `condition && action1 || action2` does evaluate the condition. If it is true, then perform `action1`; otherwise, perform `action2`.
To store the result into a variable, do:
min=$( (( $a <= $b )) && echo "$a" || echo "$b" )
Update
It seems that the `$( (( )) )` syntax is giving problems. Hence, let's replace it to:
[ $a -le $b ] && echo "$a" || echo "$b"
or assigning value:
min=$([ $a -le $b ] && echo "$a" || echo "$b")
Sample
$ a=3
$ b=4
$ [ $a -le $b ] && echo "$a" || echo "$b"
3
$ b=1
$ [ $a -le $b ] && echo "$a" || echo "$b"
1
$ b=3
$ [ $a -le $b ] && echo "$a" || echo "$b"
3
Problem
i have two variables $a and $b is there shell script command to assign the value of minimum variable to $c i.e ``` $c = min($a,$b) ``` and some command that can work across all platforms hp-ux,aix,linux_x_64 thanks in advance EDIT: default shell is ksh and below the script im trying to run ``` rm abc.log near_dr=`sqlplus -s tcs384160/tcs#1234 <<\EOF set pagesize 0 feedback off verify off heading off echo off select max(sequence#) from v$archived_log where applied='YES' and thread#=1 and dest_id=2; exit; EOF` DR=`sqlplus -s tcs384160/tcs#1234 <<\EOF set pagesize 0 feedback off verify off heading off echo off select max(sequence#) from v$archived_log where applied='YES' and thread#=1 and dest_id=3; exit; EOF` safe_var=$([ $near_dr -le $DR ] && echo "$near_dr" || echo "$DR") echo $safe_var; ulimit=`expr $safe_var - 30`; llimit=`expr $ulimit - 1000`; echo $llimit; echo $ulimit; i=$llimit while [ $i -le $ulimit ]; do ls evdprd_1_${i}_*.arc>>abc.log; let i=i+1; done; recover -s ttlhydnwr -c tphtpsd2<<EOF >> abc.log ls -1 *.arc exit EOF sed -e 's/[\t ]//g;/^$/d' abc.log > abc1.log awk '++seen[$0] == 2' abc1.log > actual.log ``` please ignore the sqlplus parts as those are working fine the only problem is the $safe_var needing the minimum of the two values