Passing a variable as input to another shell

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

Try using double quotes..

. chkTimeFormat.sh "$time"|read c

Thanks but ,

. chkTimeFormat.sh "$time"|read c

it did not work.
Any more ideas...

What does not work? Pls be more specific, post output and/or error samples, variable contents, etc.

As Rudic said Please provide some details..

Please look how double quotes makes difference here.. :slight_smile:

$ 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

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

Try using double square brackets..:slight_smile:
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