problem in a shell based project

i have a bank project in shell script.it has a module called deposit.when i am entering an amount and commit a mistake like entering char instead of numbers and so on and repeating it, suppose 3 times,then it is rejected by amount validation code. every thing is fine upto these steps. but after 3 times if i give the correct amount then this new amount is added three times to the account rather than just once.
i dont know why
please help..

first getting old value of amount by grep command
and then adding new amount to it

i am replacing the old amount with new one using "sed" command
sed s/old/new details > file
mv file details

what to do?

wilshire soft
hyderabad
india

break the loop when correct? Show us the loop, or are the three tries in a line? Use a loop.

1 Like

thanx for the quick reply
may be you are right ? i will check it

Before making the old -> new, put a conditional check for the input with a counter.

  1. use something like below for the validation
    echo "asbc123" | tr -dc '[:alpha:]' #if something return its invalid

  2. for each try increament the counter
    check if the step1 not returning anything & step2<=3
    proceed
    else
    do nothing

1 Like

this is very funny situation
i tried the code on my laptop at home and its working fine
i dont know what goes wrong on the server with the code in office

tomorrow i will check it again in the office
for the while i have this code
################################
in deposit module
############
amtvalidation ()

{
len=`echo "$amt"|wc -c`
for((i=1;i<len;i++))
do
c=`echo "$amt"|cut -c$i`
case $c in
[0-9])continue;;
*)echo "invalid .enter:"
read amt
amtvalidation "$amt";;
esac
done
let r=amt%100
if [ $r -ne 0 ]
then
echo "enter amount in 100: "
read amt
amtvalidation "$amt"
fi
}

clear

echo "enter accno:"
read accno

echo "enter the amount:"
read amt
amtvalidation "$amt"

old=`grep -w "$accno" info|cut -d ":" -f7`

clear
sleep 1
echo "pre amt is $old "
let new=old+amt
sed "/$accno/s/$old/$new/" info > file
mv file info

echo
sleep 1
echo "added"
echo

n=`grep -w "$accno" info|cut -d ":" -f7`
sleep 1
echo "new amt is $n:"
echo

######################
in info file the red number is f7(field 7)
################
dha:jhjh:hgj@jhj.com:dha:8989898989:hjj:3800:123021

Do you have a

#!/path/to/interpreter [one-option-like-f-in-sed-or-awk ]

line at the top of the script and made it executable to the executing id?

Are the interpreters the same version?

1 Like

hiiiiii friend
i am very much new to shell script
i dont know that much of technicalities
please mention what should be added at top of script

---------- Post updated at 10:11 PM ---------- Previous update was at 10:09 PM ----------

the code i have pasted is working fine on my laptop but the same code gets wrong on my office server

For instance, if it is a ksh script and a path to ksh is /usr/bin/ksh, since ksh will gladly run a script as

ksh script_name

unlike sed where you say

sed -f script_name

so you need no argument, the first line should be:

#!/usr/bin/ksh

Sometimes this is called the magic. For our memory, we call this "pound-bang"! It is documented in man 2 exec:
Man Page for exec (OpenSolaris Section 2) - The UNIX and Linux Forums
It also guides the file command to identify it as a ksh script. As the man page warns, you might end up under some shell (sh?) as stdin without it. In UNIX, interpreted files with right permissions

chmod u+x script_name

are just as executable as binaries to the common loader, exec().

The only thing where scripts are second class is if you would set setuid or setgrpid bits with chmod and have scripts that change user or group; it is too insecure, so when loading setuid/setgrpid, the dynamic library path is unset by exec as it changes user/group, and withou it the ksh cannot run. A C program compiled with -R can have hard coded dynamic library paths and so be setuid/setgrpid. Many commands you use to see OS things or affect other processes are setuid to a super-user:

$ ls -ld /bin/*|sort
-r-sr-x---   1 root       suallow      24576 Aug  6  1998 /bin/su
-r-sr-xr-x   1 lp         bin          16384 Oct 27  1997 /bin/slp
-r-sr-xr-x   1 root       bin          20480 Jan 21  2004 /bin/newgrp
-r-sr-xr-x   1 root       bin          24576 Jan 10  2001 /bin/bdf
-r-sr-xr-x   1 root       bin          24576 Nov  7  2001 /bin/rexec
-r-sr-xr-x   1 root       bin          28672 Mar  5  2004 /bin/nfsstat
-r-sr-xr-x   1 root       bin          32768 Jun 18  2002 /bin/crontab
-r-sr-xr-x   1 root       bin          32768 Nov  7  2001 /bin/remsh
-r-sr-xr-x   1 root       bin          45056 Nov  7  2001 /bin/rlogin
-r-sr-xr-x   1 root       bin          49152 Nov  6  1997 /bin/chkey
-r-sr-xr-x   1 root       bin          49152 Nov  7  2001 /bin/rcp
-r-sr-xr-x   1 root       bin          53248 Aug 14  2002 /bin/login
-r-sr-xr-x   1 root       bin          53248 Jun 18  2002 /bin/at
-r-sr-xr-x   1 root       bin          65536 Oct 20  1999 /bin/mediainit
-r-sr-xr-x   1 root       bin          73728 Feb 25  2003 /bin/df
-r-sr-xr-x   1 root       bin          73728 Nov  7  2001 /bin/rdist
-r-sr-xr-x   5 root       bin          49152 May 23  2001 /bin/chfn
-r-sr-xr-x   5 root       bin          49152 May 23  2001 /bin/chsh
-r-sr-xr-x   5 root       bin          49152 May 23  2001 /bin/nispasswd
-r-sr-xr-x   5 root       bin          49152 May 23  2001 /bin/passwd
-r-sr-xr-x   5 root       bin          49152 May 23  2001 /bin/yppasswd
1 Like

thanx for all your support

i corrected that problem.....
this problem was due to the placing of balance updation code inside the amtvalidation function ....

i kept this code out side the function and problem was corrected

the code that i have pasted in this thread was running fine because it has the updation code separate from validation function .....
but in my office server it was inside the validation function

once again
thanx a lot

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.