Numerology With Bash Code Example

View previous topic View next topic Go down

Numerology With Bash Code Example

Post by jamied_uk on 23rd July 2016, 13:35




Code:
#!/bin/bash
# Usage ./num.sh "test"
# (c) J~Net 2016
# jnet.forumn.biz/t1330-numerology-with-bash-code-example#1860
ptr=1
if [ -z "$1" ] ; then
  echo -n "Word or phrase: "
  read basis
else
  basis="$@"
fi
echo "$basis" > out.txt
basis="$( echo $basis | \
  tr '[A-Z]' '[a-z]' | \
  tr -Cd '[:alnum:]' )"
basislength="$( echo $basis | wc -c )"
echo "(Working with $basis which has \
  $basislength characters)"
while [ $ptr -lt $basislength ] ; do
  letter="$( echo $basis | cut -c $ptr )"
  ordvalue="$(echo $letter | \
    perl -e '$a=getc(); print ord($a)-96' )"
  echo "letter $letter has value $ordvalue" >> out.txt
  ptr="$(( $ptr + 1 ))"
done
# ā€ˇfilter out non numbers
cat out.txt | sed 's/[^0-9]*//g' > nout.txt
# Sum all numbers together
SUM=0; for i in `cat nout.txt`;
do SUM=$(($SUM + $i));
done;
echo "Your Words "
cat out.txt
echo "Adds up to a total of "
echo $SUM
rm nout.txt; rm out.txt
exit 0


Last edited by jamied_uk on 23rd July 2016, 16:18; edited 1 time in total
avatar
jamied_uk
Admin

Posts : 2195
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Numerology With Bash Code Example

Post by jamied_uk on 23rd July 2016, 16:11

Update File Link
app.box.com/s/v6xjqpnuvw2slupextct2lgzdz7azk6v
avatar
jamied_uk
Admin

Posts : 2195
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum