|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Need help with parameter pattern matching
I have an assignment in for the day after tommorrow. I've been working on it solidly but i'm new to unix and am finding it difficult. Do you think you could tell me how i would write the equivalent of the third to last line
case $# in 0) echo no parameters;; 1) echo only one parameter. echo put commands to be executed here.;; *) echo more than one parameter. echo enter code here.;; esac in a if statement. Need to write "if $file like * " but cant seem to make it work. Any help would be extremely appreciated. Thanks guys, Katie |
|
#2
|
|||
|
|||
|
high katie,
don't quite understand the question. Can you expand on it a bit and post your attempts (code)? Can name your shell/os? -Steven Am I jumping the gun Baldrick, or are the words "I have a cunning plan." marching with ill deserved confidence in the direction of this conversation? –Edmund Black Adder Last edited by stevengs : September 6th, 2004 at 05:38 PM. |
|
#3
|
|||
|
|||
|
Re: expand
Message Deleted
|
|
#4
|
|||
|
|||
|
Can I be totally honest. I have been given lots of assignments to do over the summer as my brother was lost in a traffic accident.
I have tried my hardest to complete them and left this one til last and now i don't have enough time. The code shown is my friends and I need to change it so it cant be recognised. I feel this is my only option. Are they any ways to change the structure to make it look different while still doing the same thing. Heres my assign spec Specification ~~~~~~~~~~~~~ Write a Bourne shell script called dosRename to implement an MS-DOS style rename. The scripts parameters are file names followed by a target that specifies either a new main part of the filename or a new extension part of the filename but not both. Targets consist of three parts: a dot, an asterisk and a string of filename characters. The dot is the middle one of the three. The command should check the validity of all the parameters before renaming any files. If errors are found, the script should output an error message and execute an `exit n'. The required error messages and the values of `n' are as follows: 1 Usage: dosRename file [ ... ] target 2 dosRename: <parameter>: Can't have files after target 3 dosRename: <parameter>: Can't have two dots 4 dosRename: <parameter>: Can't have more than one asterisk 5 dosRename: <parameter>: Doesn't exist 6 dosRename: <parameter>: Must have a dot 7 dosRename: No target Notes: 1 The first error message is given if the script is called with less than two parameters. 2 <parameter> is a placeholder for the actual parameter. If there are no errors in the parameters, the files are renamed with the `mv' command. There is no need to check for existing files being deleted; Unix is humble - it assumes the user knows best. For the purposes of this assignment, there is no need to check for files that can't be `mv'ed. |
|
#5
|
|||
|
|||
|
katie, it's not easy to automatize what you want, i wrote
3 month long a c-prog for this. there are a lot of checks, parameter, fileperms, filetype, extensions, extension-syntax, filename globbing, is new name unique and and and.... a quick and very+very+very+very+very+very+very dirty way be save: don't 'mv' but 'cp' and check status, it's slow and saver. Code:
#!/usr/bin/sh
[ -d ${SAVE:=savemyoldfiles} ] || mkdir $SAVE || exit 1 # error
for file in `ls *old`
do
[ -f $file ] || continue # not a plain file
[ -s $file ] && continue # empty file
cp $file $SAVE >/dev/null 2>&1 || continue #error
FILE=`echo $file|sed 's/old$/new'`
cp $file $FILE >/dev/null 2>&1 || continue #error
rm $file
done
nota: [ xxx] && abc || ABC is a shorthand of: if [ xxx ] ; then abc ; else ABC; fi and is the brother of 'c': aaa = (xxx) ? abc : ABC; just a style question. remember the if-[elif-]else is maybe far the most dangerous construct (in every languages) if possible try something else: case, switch, for, do, while... and if you use if, have a look a) VAR=no; if[ xxx ] ; then VAR=yes; fi b) VAR=no; [ xxx ] && VAR=yes c) [ xxx ] && VAR=yes || VAR=no d) if[ xxx ] ; then VAR=yes; else VAR=no; fi all 4.expls are equivalent! ![]() Last edited by guggach : September 6th, 2004 at 07:53 PM. Reason: typo |
|
#6
|
|||
|
|||
|
Thanx babe, your an absolute star. Take care
luv Katie x |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Please can a really nice person help me. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|