Help with test command

Plese help me on the below query.

for j in *.20071231* .ctl .dat
do
(
if [ -f "$j" && "$j" -ne "C*20071231
.log" ] then
cp "$base/*.
" "$base1"
fi
)
done

My requirement is for all files that has extension *.20071231* *.ctl *.dat
should be copied to another folder.
But those with caaa.20071231.log should'nt be copied.

Assuming $base1 is the destination directory:

ls *.ctl *.dat | while read file
do
  cp "$file" "$base1"
done

Regards

You cannot use && inside a test expression.

-ne compares integers, not strings.

You cannot compare a pattern with a test expression.

if [ -f "$j" ]; then
  case $j in
     C*20071231*.log) ;;
     *) cp "$base/*.*" "$base1" ;;
  esac
fi

Why would you use a loop for that?

cp *.ctl *.dat "$base1"

(And that loop would fail if any matching filenames had leading whitespace.)

Thanks cfajohnson and Franklin52

Thanks for the response.

Jhon:
could u please explain me the code which u gave.

if [ -f "$j" ]; then
case $j in
C*20071231*.log) ;; // I am confuesed here as there is no opening brace here ) // What is the purpose of this brace here cp "$base/*." "$base1" ;;
esac
fi

Please help me to understand this.

Thank you

The following is the basic format of the case statement:

 case test-string in                        
              pattern-1 ) commands-1 ;;              
              pattern-2 ) commands-2 ;;              
              pattern-3 ) commands-3 ;;              
              .                                      
              .                                      
              .                                      
              *)          commands   ;;              
         esac                                        

So, you are correct in seeing only a ) and wondering where the matching ( character is. It simply is not needed.
If a match on pattern-1, then commands-1 is executed, and so on. The *) means that if none of the above were matched, do commands.

Please put code inside

 tags.



if [ -f "$j" ]; then
  case $j in
     C*20071231*.log) ;; // I am confuesed here as there is no opening brace here

[/quote]

[indent]
There is no closing brace, either; there is a closing parenthesis. The opening parenthesis is optional.

Read your shell's man page for the case statement.

Thanks joeyg,

Now i'm pretty clear.:b:

Thanks to all who have helped me in this thread.:slight_smile: