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 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! |
you just need to have a space before and after the [ and ] (brackets)
|
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 |
also get rid of the keyword 'test'
|
Hello fpp666 :)
You can use Code:
if test -e $dir_new$name.CP; then Code:
if [ -e $dir_new$name.CP ]; then Code:
if [[ -e $dir_new$name.CP ]]; then 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 Best Charles |
Awesome guys, you rock!!
Cheers! |
All times are GMT -5. The time now is 09:55 PM. |