I have a shell program that calls another shell program
the following code works
. chkTimeFormat.sh "10/9/12 17:51:19:783."|read c
but when I am passing the the time in a variable like in the code below, the shell chkTimeFormat.sh is not returning proper value
time="10/9/12 17:51:19:783."
. chkTimeFormat.sh $time|read c
Please help
pamu
2
Try using double quotes..
. chkTimeFormat.sh "$time"|read c
Thanks but ,
. chkTimeFormat.sh "$time"|read c
it did not work.
Any more ideas...
RudiC
4
What does not work? Pls be more specific, post output and/or error samples, variable contents, etc.
pamu
5
As Rudic said Please provide some details..
Please look how double quotes makes difference here..
$ cat test2.sh
echo $1
$ . test2.sh "10/9/12 17:51:19:783."
10/9/12 17:51:19:783.
$ time="10/9/12 17:51:19:783."
$ . test2.sh $time
10/9/12
$ . test2.sh "$time"
10/9/12 17:51:19:783.
Hope this helps
pamu
This the code for chkTimeFormat.sh is as follows
time=$1
var=$(echo "$time"|awk '{for(i=1;i<=NF;i++) if ( $i ~ /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/){print i}} ')
if [ $var -ge 0 ]
then
echo 1
else
echo 0
fi
I called this script as follows from a different script n the following way
time="10/9/12 17:51:19:783."
. chkTimeFormat.sh $time|read c
echo $c
and
time="10/9/12 17:51:19:783."
. chkTimeFormat.sh "$time"|read c
echo $c
I was expecting and output
1
but I am getting
0
Please suggest
c=$(. chkTimeFormat.sh "$time")
echo $c
RudiC
8
Pls execute setting -vx and post log.
Using the variable name as time was causing the problem.
Changed it to t and the the variable in quote fixed the problem.
Thanks you all folks
pamu
10
Try using double square brackets..
for blank values of var you may need double square braces.
time=$1
var=$(echo "$time"|awk '{for(i=1;i<=NF;i++) if ( $i ~ /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/){print i}} ')
if [[ $var -ge 0 ]]
then
echo 1
else
echo 0
fi
1 Like