sexta-feira, 18 de novembro de 2016

BASH

como usar comparação com if

Bash can't handle floats. Pipe to bc instead:

if [ $(echo " $test > $k" | bc) -eq 1 ]
The error you see though is because the test command (i.e. the [) needs spaces before and after

It is even better to use (( ... )) since you compare numbers like this:

if (( $(bc <<< "$test > $k") ))
The part in the loop should look like this:

if (( $(bc <<< "$test <= 1") ))
then
    echo "output"
elif (( $(bc <<< "$test > $k") ))
then
    echo "output$k"
fi
Relational expressions evaluate to 0, if the relation is false, and 1 if the relation is true [source]. Note however that is a behavior of GNU bc, and it is not POSIX compiant.


FONTE: http://stackoverflow.com/questions/15224581/floating-point-comparison-with-variable-in-bash


while
x=1
while [ $x -le 5 ]
do
  echo "Welcome $x times"
  x=$(( $x + 1 ))  #parenteses precisam estar juntos
done

procurando numero de elementos dentro do array
for word in ${myarr[*]}; do
  echo $word
done | grep -c "hello"
ou
grep -o hello <<< ${myarr[*]} | wc -l



declare -a myArray
myArray=( `cat aux1.txt` )
re=${myArray[0]}
tLen=${#myArray[@]}
unset myArray

Comparar numeros usa-se: eq ne lt gt
Comparar strings   usa-se:  == != < >


bc não dá conta de trabalhar com notação científica. Usar awk no lugar


GREP
-v, --invert-match        select non-matching lines

grep -v '^Nov 06' file

grep -Ev '^(ATOM|CONECT|HETATM|TER|END)' /path/to/your/file


grep -E '^(C|P)' coordenadas_finais.coor

grep -e 'C' -e 'P' coordenadas_finais.coor > b.coor
grep '^V' coordenadas_finais.coor > c.coor







Nenhum comentário: