LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-16-2009, 10:15 AM   #1
fpp666
LQ Newbie
 
Registered: Jul 2009
Posts: 10

Rep: Reputation: 0
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!
 
Old 07-16-2009, 10:26 AM   #2
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
you just need to have a space before and after the [ and ] (brackets)
 
Old 07-16-2009, 10:30 AM   #3
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware
Posts: 299

Rep: Reputation: 50
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

Last edited by vonbiber; 07-16-2009 at 10:31 AM.
 
Old 07-16-2009, 10:30 AM   #4
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
also get rid of the keyword 'test'
 
Old 07-16-2009, 10:37 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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
 
Old 07-16-2009, 10:55 AM   #6
fpp666
LQ Newbie
 
Registered: Jul 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Awesome guys, you rock!!

Cheers!
 
  


Reply

Tags
shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command to test DNSmasq? arfon Linux - Server 3 09-29-2008 06:14 PM
using test command ahmedb72 Solaris / OpenSolaris 2 07-03-2007 06:15 AM
BASH - For with a if test command mago Programming 9 05-03-2007 03:15 PM
How to test whether a command timeout? DriveMeCrazy Programming 3 01-11-2007 09:21 AM
understanding read command as a test frankie_DJ Programming 11 08-08-2005 02:30 PM


All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration