View Full Version : help debuging shell scripts !
eagle_y2j
28-10-2007, 07:10 AM
Help me out debug following shell scripts I hav just started learning bash but I need bug free code for my assignments hope you ppl will help me promptly :-)
snipped
Ans
echo “Enter the date of birth”
read a
while [ $a - ne o]
do
r = ‘ expr $a %10’
s = ‘ expr $s + $r’
a = ‘ expr $a /a’
done
p = ‘ expr $s %10’
s = ‘ expr $s /10’
p = ‘ expr$p + $s’
echo “ Lucky no = $p”
Q:2 snipped
Ans:
For ciphering
$VI text.sh
clear
echo “enter the text or content that we want to cipher”
read.ch
len=’expr’ & Ch : K/C – C
For len in [a – z] & $.[A - Z]
Do
Echo “*”
Done
Deciphering the text document.
Echo “Enter the ciphering text document to which we want to decipher”
Read.str
*= [a – z]
* = [a – z]
l = ‘expr’. $ .str: wc –c;
foe lin *
Q3:snipped
Answ :--- can't figure out can someone help
mehulved
28-10-2007, 07:41 AM
I don't know much about shell scripting, but for one you can enabled xtrace in bash
set -x
This is quite helpful in debugging the script. Not sure if you know about it already.
ninad_mhatre85
28-10-2007, 02:47 PM
Help me out debug following shell scripts I hav just started learning bash but I need bug free code for my assignments hope you ppl will help me promptly :-)
to debug shell script run shell script with sh -x for eg . if ur script name is temp.sh then to debug run it as
sh -x temp.sh
Q1: shell program to calculate the lucky number when a date of birth is entered by the users.
Ans
echo Enter the date of birth
read a
while [ $a - ne o]
do
r = expr $a %10
s = expr $s + $r
a = expr $a /a
done
p = expr $s %10
s = expr $s /10
p = expr$p + $s
echo Lucky no = $p
spaces are very important in shell scripts dont give spaces ...as well as ur using backtick(`) to assign some value to p variable....u can give some idea how this code will work ? like sample input output that would be helpful ..
p = expr $s %10
this should be
p=`expr $s % 10`
a = expr $a /a'
i didnt get what are you trying to do ??
while [ $a - ne o]
here ur tying to compare no with character ...or that is 0(zero) if its zero
then it should br
while [ $a -ne 0 ]
and for question no 3 i am not getting what do u exactly want ? anyway u can use find command to that job ... or give sample input and output to proceed further ...
btw which shell are u using ?? and which os ??
praka123
28-10-2007, 03:11 PM
^it is bash shell programming on Linux or Unix.coding is similar to C i suppose.
ninad_mhatre85
28-10-2007, 11:48 PM
@praka123
its also possible he might be doing it on unix emulation packages such as cygwin or SFU from microsoft
eagle_y2j
29-10-2007, 01:11 PM
@ all thankx for ur valuable commnents ...I am using Suse 10.3 to create and test shells ,I am really having no idea about scripting as its my first week with shells and for my collage assignments I am trying to solve given problems but can any of u write a script for my last questions its urgent
praka123
29-10-2007, 03:26 PM
@praka123
its also possible he might be doing it on unix emulation packages such as cygwin or SFU from microsoft OK.thanks i never remembered that.
@eagle:que3:something with "rm -rf" for deleting files and dirs with out prompting? then wildcard(*,^,?) usage.sorry i dont studied scripting much.
ninad_mhatre85
29-10-2007, 06:31 PM
@ all thankx for ur valuable commnents ...I am using Suse 10.3 to create and test shells ,I am really having no idea about scripting as its my first week with shells and for my collage assignments I am trying to solve given problems but can any of u write a script for my last questions its urgent
i didnt get ur 3rd question properly please provide me some sample input output ?
if u want online tutorial try
steve-parker.org/sh/sh.shtml
mediator
29-10-2007, 08:30 PM
Here's ur first part...corrected! Commenting is done with "#"!!
echo "Enter the date of birth"
read a
while [ $a -ne 0 ]
do
r=`expr $a % 10`
s=`expr $s + $r`
a=`expr $a / 2`
done
p=`expr $s % 10`
s=`expr $s / 10`
p=`expr $p + $s`
echo “Lucky no = $p”
It seems u just copy n pasted from some site and therefore had differences in the text format.
Here is what u posted! LOOK CAREFULLY n analyze the corrected one n the flawed one....
echo “Enter the date of birth”
read a
while [ $a - ne o] # "o" = part of alphabet not digit
do
r = ‘ expr $a %10’
#spaces are essential "10" shud be after "%" with a
# "space". Also there shud be no gap between
# variable,"=" and expression or else shell will
# treat it as a command!
s = ‘ expr $s + $r’
#the expression containing expr shud be enclosed
#in `` i.e key before 1 on keyboard!!
a = ‘ expr $a /a’ #again spaces shud be there!
done
p = ‘ expr $s %10’ #spaces!
s = ‘ expr $s /10’ #spaces!
p = ‘ expr$p + $s’ #no gap bet expr and $p
echo “ Lucky no = $p”
Also there were some error handling issues! But thats not a part of it!! So learn from here (http://www.freeos.com/guides/lsst/), analyze and debug ur rest of the scripts!! Its easy!!
eagle_y2j
30-10-2007, 09:37 PM
i didnt get ur 3rd question properly please provide me some sample input output ?
if u want online tutorial try
steve-parker.org/sh/sh.shtml
@ all re-thankx i am learning unix these days but i think for writing scripts on my own will take some more weeks for me ..... and am really very thankful for ur valuable helping hands .. as u ppl hav helped a lot to complete my semester assignments .
ninad_mhatre85 here is the sample algorithm for my 3rd question hope this will help u in helping me out .
1. Display current dir path.
2. Get starting letter and length of file name to be deleated from user as input .
3. Store and process input
4.delete matching file names .
5. end
ninad_mhatre85
30-10-2007, 11:19 PM
here is ur script i tried it on some files it works ...reply back if there are any issues
# script to delete files in current directory.
# input file name and length of file name
echo " you are currently in \"$PWD\" directory "
echo " enter the starting letter of file "
read name
echo " enter the length "
read length
echo "delete files with \"$name\" and filename length = $length "
for i in `ls -1 $name*`
do
j=`expr length $i`
if [ $j -eq $length ] ;then
echo " delete file $i " # rather than echoing them use rm -i command
fi
done
make sure when u save file its in unix format .
make sure that u use rm -i command to delete files so that it will ask before deleting ....dont use rm -rf
eagle_y2j
01-11-2007, 01:02 PM
@ ninad_mhatre85 thankx for script it really works for me.
now I am just left with an offtopic question i.e.
Q:- Write an algorithm to convert binary into its octal equivalent ?
I jotted down following am I going right in following steps ?
Ans:-Step1: Take an input of binary number.
Step2: now for converting binary no. into we start loop for process p=1, dec=0,oct=0
Step3: loop start from no. and go at the 0.
Step4: now find the remand by mod no by 10.
R=’expr $no%10’
Dec=’expr # dec/10’
P=’expr $ \*2’
Bin=’expr $bin/10’
Step 5: now check the remainder when remainder will be less then 10 that will be stored as it is. When no, is false when no=0 then repeat step 3 & 4 untiule step 5 in not false
Step6; when step 5 is falxe that means binary no. is 0 then convert it into octal no.
Step7: now start loop for octal conversion that is
While [$dec ! =0]
Do
R=’expr $dec % 8’
Oct= ‘expr $oct +$r\*p’
P=’expr $p \* 10’
Dec=’expr $dec /8’
Done
Step8: now check dec when no. when dec no. is not equal to 0 then step 5 until loop is false that means dec is 0
Step9: when dec is equal to 0 then print the value of octal
Step 10: stop run.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.