You are not logged in.

#1 2011-05-13 09:23:52

nsb
Member
From: Switzerland
Registered: 2008-03-26
Posts: 57

[SOLVED] Weird behavior in BASH script for changing directory

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

#2 2011-05-13 09:59:14

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: [SOLVED] Weird behavior in BASH script for changing directory

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)

Offline

#3 2011-05-13 10:16:57

nsb
Member
From: Switzerland
Registered: 2008-03-26
Posts: 57

Re: [SOLVED] Weird behavior in BASH script for changing directory

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

#4 2011-05-14 04:28:55

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: [SOLVED] Weird behavior in BASH script for changing directory

nsb wrote:

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 wink
Use a pastebin if it's long (pastebin does syntax highlighting too, which makes it even easier to read wink)

Offline

#5 2011-05-14 05:31:54

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [SOLVED] Weird behavior in BASH script for changing directory

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

#6 2011-05-16 08:43:37

nsb
Member
From: Switzerland
Registered: 2008-03-26
Posts: 57

Re: [SOLVED] Weird behavior in BASH script for changing directory

@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

#7 2011-05-16 09:00:23

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] Weird behavior in BASH script for changing directory

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

#8 2011-05-16 09:13:16

nsb
Member
From: Switzerland
Registered: 2008-03-26
Posts: 57

Re: [SOLVED] Weird behavior in BASH script for changing directory

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

Board footer

Powered by FluxBB