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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while [[ $# -gt 0 ]] stays in the loop
# 1  
Old 07-06-2010
while [[ $# -gt 0 ]] stays in the loop

Hi,

I have(ksh):

Code:
...
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:

Code:
script.ksh name

and also
Code:
script.ksh /home/name

But it doesn't reply $fullPath, nor "Wrong, full path", it just hangs in the loop. What am I missing?
# 2  
Old 07-06-2010
Hi

There is nothing inside the if condition:
Code:
if [[ $fullPath -gt 0 ]]; then
fi

Guru.
# 3  
Old 07-06-2010
I don't know much of ksh, but I guess if...then...fi shouldn't be empty
Code:
while (( $# ))
do fullPath=$(grep -sc '/' $1)
   if (( $fullPath ))
   then echo $fullPath
   else echo "Wrong, full path"
   fi
   shift
done

# 4  
Old 07-06-2010
I changed to this:
Code:
...
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:
Code:
 fullPath=$(grep -s '/' $1 | wc -l)


Last edited by chish; 07-06-2010 at 02:30 PM..
# 5  
Old 07-06-2010
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.

Last edited by clx; 07-07-2010 at 03:10 AM.. Reason: typo
# 6  
Old 07-07-2010
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:
Code:
while [[ $# -gt 0 ]]; do
fullPath=$(echo $1 | grep -sc '^/')
echo $fullPath
if [[ $fullPath -gt 0 ]]; then
echo "Wrong, full path"
fi
shift
done

result:
Code:
./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:
Code:
fullPath=$(echo $1 | grep -sc '/')

This User Gave Thanks to ec01 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wait and continue if directory stays same size

hi all, i want to make a bash script so it can monitor directory sizes ie if it stays the same size (for a certain time) i want it to run the script but if there still copying and the directory increases in size do not run the script i have heard "inotifywait" can do this, but theres... (7 Replies)
Discussion started by: robertkwild
7 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Shell Programming and Scripting

$1 stays set after sourcing shell script.

ok, so I have a shell script that can be called using the first argument ($1) or not. This argument is a word (Tim for example) and not an actual flag (-x for example). If I call the script with an argument and call the same script without one, it believes that I provided an argument. Note here... (2 Replies)
Discussion started by: mrwatkin
2 Replies

6. Shell Programming and Scripting

Shell script: If a file stays in a particular directory more than 30 min send an email

Hi , I am new to shell scripting. i have a requirement say i will receive a file in a directory say /xyz.if that file stays in that directory more than 30 min i need to get a mail to my outlook.this should run for every 20 min in crontab. can anyone help me? (8 Replies)
Discussion started by: muraliinfy04
8 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies
Login or Register to Ask a Question