Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

327
Vistas
How to find min of two variables in linux

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

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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
over 4 years ago · Santiago Trujillo Denunciar

0

You could do in this way too:

x=1
y=2
echo $(($x<$y?$x:$y))
1

To store value in other variable:

z=$(($x<$y?$x:$y))   #Min
z=$(($x<=$y?$x:$y))  #Min and equal
z=$(($x>$y?$x:$y))   #Greater
z=$(($x>=$y?$x:$y))  #Greater and equal
over 4 years ago · Santiago Trujillo Denunciar

0

In ksh, this works fine

$ near_dr=5 DR=10                        
$ save_var=$(( near_dr < DR ? near_dr : DR )) ; echo $save_var
5
$ near_dr=15 DR=10                                            
$ save_var=$(( near_dr < DR ? near_dr : DR )) ; echo $save_var
10
$ near_dr=15 DR=15                                            
$ save_var=$(( near_dr < DR ? near_dr : DR )) ; echo $save_var
15

So you can write a function:

min() {
    echo $(( $1 < $2 ? $1 : $2 ))
}

safe_var=$(min $near_dr $DR)

This is the same as @Liaraz's answer, except "Variables can be referenced by name within an arithmetic expression without using the parameter expansion syntax." (ksh man page, section Arithmetic evaluation)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda