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.datcd c:/tesi/libsvm
rm image-vector.dat
rm image-vectorSVM.datcd c:/tesi
cp $* c:/tesi/or/annotator/annotatecd 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.datcd 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
What is this? It looks like a bad mix of Bash and Windows.
Is it Cygwin? MS SFU? MKS Toolkit?
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
suppression is same as for microsoft i think:
rm whateverfile 2>nuli'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}
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.
@Razor,
yeah you're right, my output went to file "nul" and i forgot to check for it. \dev\null is the correct target.
@ Razor2.3 I've wrote it's the MinGW/MinSYS shell installed in a Windows XP environment
@nail Thank you very much for your answer, now everything works fine.
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.
Naruladib: MinSYS shell installed in a Windows XP environment
I believe MSYS uses /dev/nullEDIT:
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.
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"
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.
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.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |