LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Test command question (https://www.linuxquestions.org/questions/programming-9/test-command-question-740549/)

fpp666 07-16-2009 09:15 AM

Test command question
 
Hi all!

I'm new to shell scripting, and I need to do a diff between two dirs. One of them (dir_old) contains many files, and the other (dir_new) contains just a set of 8 files, all starting with MC and extension .CP .

The problem I have is that dir_old contains 12 files that start with MC and with no extension, so I need to check if the files in dir_new exist before using the diff.

This is what I have so far:

Code:

cd $dir_old
for name in MC*
    do
        if [test -e  $dir_new$name.CP]; then
            diff -s "$name" "$dir_new$name.CP" >> logfile
        fi
    done

All I get from this script is this list of errors:

testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found
testWhs[16]: [test: not found

I'm using ksh and according to the man cmd, the command exists, and I think I'm using it right, so I'm lost :-(

Thanks for the help!

rn_ 07-16-2009 09:26 AM

you just need to have a space before and after the [ and ] (brackets)

vonbiber 07-16-2009 09:30 AM

cd $dir_old
for name in MC*
do
if [ -e $dir_new/$name.CP ]; then
diff -s "$name" "$dir_new/$name.CP" >> logfile
fi
done


In other words:
1. remove the 'test' from inside the brackets
and there must be a space after '[' and
a space before ']'
2. if dir_new doesn't end with a '/' you must add it
e.g., if dir_new=/home/someuser/new then : $dir_new/$name.CP
but, if dir_new=/home/someuser/new/ then : $dir_new$name.CP

if [test -e $dir_new$name.CP]; then

rn_ 07-16-2009 09:30 AM

also get rid of the keyword 'test'

catkin 07-16-2009 09:37 AM

Hello fpp666 :)

You can use
Code:

if test -e  $dir_new$name.CP; then
or
Code:

if [ -e  $dir_new$name.CP ]; then
or
Code:

if [[ -e  $dir_new$name.CP ]]; then
[[ <stuff> ]] is the GT racing souped up version of [ <stuff> ] and has less gotchas so is preferred. See http://tldp.org/LDP/abs/html/testconstructs.html for more info.

When there's a single command to run after the test you can use the more compact (but less legible) form
Code:

[[ -e  $dir_new$name.CP ]] && diff -s "$name" "$dir_new$name.CP" >> logfile
where && is the logical AND operator; what comes after it is only evaluated if the [[ <stuff> ]] part evaluates to true.

Best

Charles

fpp666 07-16-2009 09:55 AM

Awesome guys, you rock!!

Cheers!


All times are GMT -5. The time now is 09:55 PM.