while [[ $# -gt 0 ]] stays in the loop

Hi,

I have(ksh):

...
while [[ $# -gt 0 ]]
do
fullPath=$(grep -s '/' $1 | wc -l)
echo $fullPath
if [[ $fullPath -gt 0 ]]; then
fi
echo "Wrong, full path"
shift
done
...

I tried to do:

script.ksh name

and also

script.ksh /home/name

But it doesn't reply $fullPath, nor "Wrong, full path", it just hangs in the loop. What am I missing?

Hi

There is nothing inside the if condition:

if [[ $fullPath -gt 0 ]]; then
fi

Guru.

I don't know much of ksh, but I guess if...then...fi shouldn't be empty

while (( $# ))
do fullPath=$(grep -sc '/' $1)
   if (( $fullPath ))
   then echo $fullPath
   else echo "Wrong, full path"
   fi
   shift
done

I changed to this:

...
while [[ $# -gt 0 ]]
do
fullPath=$(grep -sc '/' $1)
echo $fullPath
if [[ $fullPath -gt 0 ]]; then
echo "Wrong, full path"
fi
shift
done
...

But it doesn't help. I guess something wrong with the line:

 fullPath=$(grep -s '/' $1 | wc -l) 

Are you sure you are having the file "name" and/or "/home/name" ?
better remove the -s option just for testing so-that you can see what is going wrong there.

Just guessing, but it looks like you're trying to determine if $1 is a full (absolute) path (and if so, echo "Wrong, full path"), or a relative path (and if so, echo 1 according to your code?)

try this:

while [[ $# -gt 0 ]]; do
fullPath=$(echo $1 | grep -sc '^/')
echo $fullPath
if [[ $fullPath -gt 0 ]]; then
echo "Wrong, full path"
fi
shift
done

result:

./script.ksh blah
0
./script.ksh /blah
1
Wrong, full path
./script.ksh blah/foo
0

If you want it to reject anything with a slash (/), as opposed to just starting with slash (ie: blah/foo in last example), then change the line to:

fullPath=$(echo $1 | grep -sc '/')
1 Like