You are not logged in.
Hello
I have strange problem: I have some direcories looking like that:
[nb430@talyn all]$ ls -l
total 88
drwxrwxr-x 10 nb430 nb430-group 4096 May 13 10:06 384in400w0
drwxrwxr-x 10 nb430 nb430-group 4096 May 13 10:00 384in400w0.0000016
drwxrwxr-x 10 nb430 nb430-group 4096 May 12 16:47 384in400w0.00016
drwxrwxr-x 10 nb430 nb430-group 4096 May 13 10:06 384in400w0.0016
drwxrwxr-x 10 nb430 nb430-group 4096 May 13 10:06 600in1000w0
drwxrwxr-x 10 nb430 nb430-group 4096 May 13 10:00 600in1000w0.0000016
drwxrwxr-x 10 nb430 nb430-group 4096 May 12 16:47 600in1000w0.00016
drwxrwxr-x 10 nb430 nb430-group 4096 May 13 10:06 600in1000w0.0016
drwxrwxr-x 10 nb430 nb430-group 4096 May 13 09:25 600in2154w0.00016
...
then I have script that should iterate through the direcories:
#! /bin/bash
base_dir=$(pwd)
declare -a boxes=("2154" "1000" "400")
declare -a particles=("600" "600" "384")
switches="0 0.0016 0.00016 0.0000016"
for switch in $switches
do
for i in {0..2}
do
box=${boxes[$i]}
particle=${particles[$i]}
#echo $particle
mydir="${particle}in${box}w${switch}"
echo "is there $mydir?"
if [[ $mydir == "384in400w0.00016" ]]; then
echo "hurrey"
fi
if [[ ! -d $mydir ]]; then
echo "no"
continue
fi
echo "yes"
cd "${mydir}/1"
# do stuff
cd $base_dir
done
and this is my unexpected output:
is there 600in2154w0?
no
is there 600in1000w0?
yes
...
is there 384in400w0?
yes
...
is there 600in2154w0.0016?
no
is there 600in1000w0.0016?
yes
...
is there 384in400w0.0016?
yes
...
is there 600in2154w0.00016?
yes
...
is there 600in1000w0.00016? #BAD
no
is there 384in400w0.00016? #BAD
hurrey
no
is there 600in2154w0.0000016?
no
is there 600in1000w0.0000016? #BAD
no
is there 384in400w0.0000016? #BAD
no
(Three points [...] indicate that it works there)
As you see I build in one test to compare at least one of the four composed directories, which are not recognised, with a string and the test works, but still it doesn't see the directory itself.
I have no clue, what the problem might be.
Regards
nsb
Edit: Inserted the part with going back into the base directory
Last edited by nsb (2011-05-16 09:14:17)
Offline
You seem to be missing a cd back to your starting directory... I'd suggest using absolute paths:
#! /bin/bash
declare -a boxes=("2154" "1000" "400")
declare -a particles=("600" "600" "384")
switches="0 0.0016 0.00016 0.0000016"
starting_dir="$PWD"
for switch in $switches
do
for i in {0..2}
do
cd $starting_dir
box=${boxes[$i]}
particle=${particles[$i]}
#echo $particle
mydir="$starting_dir/${particle}in${box}w${switch}"
echo "is there $mydir?"
if [[ $mydir == "384in400w0.00016" ]]; then
echo "hurrey"
fi
if [[ ! -d $mydir ]]; then
echo "no"
continue
fi
echo "yes"
cd "${mydir}/1"
# do stuff
done
Last edited by fukawi2 (2011-05-13 09:59:52)
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
Sorry, I cut out this part of the code, so it won't be too long. So actually I do that, almost in the same way as you propose.
If I replace the array
declare -a boxes=("2154" "1000" "400")
declare -a particles=("600" "600" "384")
with
declare -a boxes=("1000" "400")
declare -a particles=("600" "384")
Then it works except of course for the directory, which is then left out. I can't see, why this should matter.
Last edited by nsb (2011-05-13 11:07:48)
Offline
Sorry, I cut out this part of the code, so it won't be too long.
Post the full script... It's impossible to properly debug something without having the whole story
Use a pastebin if it's long (pastebin does syntax highlighting too, which makes it even easier to read )
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
I'm not sure what you are trying to accomplish. If you just want to cd to some subdirectories and "do stuff", you could try this:
#!/bin/bash
for d in *
do
if [[ -d "$d" && "$d" =~ (384|600)in(400|1000|2154).* ]]
then
(cd "$d/1" && {
do_stuff
})
fi
done
Last edited by rockin turtle (2011-05-14 05:32:38)
Offline
@rockin turtle I use these arrays because in an other script I use the same piece of code to create all the directories and start my simulations in them. It would be a bit silly, if I would always have to write it in two ways.
I have now pasted my out put on pastebin: the script and the output. I think I have rearranged a bit the code, since now a different set of directories is recognized or not.
regards
nsb
Offline
I'm guessing there's no config.last in 600in464w0.00016.
When you get a negative result to that test, you continue, and you're now in the subdir. Your cd back to base directory is in the outer loop, which you've just continued.
I would either move the cd to base dir to the start of the loop, or not cd into the subdir until after the config.last test.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Yes you are right. Not all the data were there and indeed that is why test failed. Now everything is there and it works. I will adjust my script, so it will handle this problems.
Thank you.
nsb
Offline