If - stop in a shell script

January 20, 2010 at 09:07:56
Specs: Windows XP - MinGW/MinSYS Shell
I've wrote this shell script (see below).

I've got two questions:
1) I want to insert an if block at the beginning of the script: if the script is invoked with no argument it should exit to shell at line 4, else it can continue.
2) how can I hide on screen if the remove command return an error (because the first time the script run the files aren't there)?

#!/bin/sh

ECHO "Extracts CM features from images, then vectorize and normalize them"
ECHO "USAGE: Extractor image_name"
ECHO.

ECHO "Removing previous files"
rm image-vector.dat
rm image-vectorSVM.dat

cd c:/tesi/libsvm
rm image-vector.dat
rm image-vectorSVM.dat

cd c:/tesi
cp $* c:/tesi/or/annotator/annotate

cd c:/tesi/or/annotator/annotate
ECHO.
ECHO "Color Moments extraction"
ECHO.
ECHO "600" > image-vector.dat
annotate -e 'YUV-moments(hblocks=10,vblocks=10)' $* -d >> image-vector.dat
rm $*
cp image-vector.dat c:/tesi/libsvm
cp image-vector.dat c:/tesi/
rm image-vector.dat

cd c:/tesi/LibSVM
ECHO.
ECHO "Image-vector normalizing..."
ECHO "."
ECHO "."
ECHO "."
perl normalizerSVM.pl
rm image-vector.dat
cp image-vectorSVM.dat c:/tesi
rm image-vectorSVM.dat
ECHO.
ECHO "Image-vector ready!"

exit 0


See More: If - stop in a shell script

Report •


#1
January 20, 2010 at 14:00:56
What is this? It looks like a bad mix of Bash and Windows.
Is it Cygwin? MS SFU? MKS Toolkit?

Report •

#2
January 20, 2010 at 15:04:52
I am assuming your script is a variety of Unix:

1) To check for an undefined arg 1, Insert this code:

if [ -z $1 ]
then
   echo "arg 1 undefined"
   exit 1
fi

2) Try using rm's -f switch to suppress error messages:

rm -f file



Report •

#3
January 20, 2010 at 15:06:12
suppression is same as for microsoft i think:
rm whateverfile 2>nul

i'm still working on (re)learning sh/bash, so i haven't done the IF, but this will exit the script if parameter 1 is null:
{$1:? goodbye}


Report •

Related Solutions

#4
January 20, 2010 at 15:11:18
suppression is same as for microsoft i think:
rm whateverfile 2>nul

It depends on what "Unix-like" environment you're using. Some you can redirect to NUL, others map null to /dev/null.

Report •

#5
January 20, 2010 at 15:36:22
@Razor,
yeah you're right, my output went to file "nul" and i forgot to check for it. \dev\null is the correct target.

Report •

#6
January 20, 2010 at 16:00:22
@ Razor2.3

I've wrote it's the MinGW/MinSYS shell installed in a Windows XP environment


Report •

#7
January 20, 2010 at 16:04:39
@nail

Thank you very much for your answer, now everything works fine.


Report •

#8
January 20, 2010 at 16:32:13
what is the difference between

rm whatever 2> \dev\null (or /dev/null)

versus

rm -f whatever

Both will do the job, but if the file is write protected - meaning that user can not delete it - the rm -r command will display the write protected error. Redirecting to the null device throws away all the error messages.

It's a person choice, but I would like to know my script can not delete a file.


Report •

#9
January 20, 2010 at 16:32:51
Naruladib: MinSYS shell installed in a Windows XP environment
I believe MSYS uses /dev/null

EDIT:
nails if the file is write protected
Then the rest of the script will bomb soon after.
But since we're dealing with Windows and ACL's, I suppose he could not have delete access, but have write access.


Report •

#10
January 21, 2010 at 07:29:33
Nails, the "-r" may be for recursive, which you need if deleting directory structures. I may be wrong here, but without "-r" he would only behold files in the current directory, and ignore all directories. Often, that is OK. But if you want to remove a directory (as opposed as to deleting a file), you need to specify an extra parameter. And if I'm correct, it's "-r"

Report •

#11
January 21, 2010 at 19:48:36
tvc:

I am sorry, but that's a typo on my part. I meant to say:

rm -f

I apologize again. You are correct about -r being for a recursive remove.

I am correcting the above so as not to confuse anyone else.


Report •

#12
January 22, 2010 at 02:35:04
No prob,
But Naruladib, why even bothering running unix like code ... I'm not seeing any special command, you might as well use DOS code to write a script with only ECHO and COPY commands, no ?! You may be just making it hard for yourself and anybody else. Just an idea.


Report •


Ask Question