![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sh -help with case statement (should be simple) | kuliksco | Shell Programming and Scripting | 1 | 11-19-2007 06:04 PM |
| case statement | bkan77 | Shell Programming and Scripting | 5 | 09-11-2007 02:54 PM |
| Case statement problem | gzs553 | UNIX for Advanced & Expert Users | 6 | 11-14-2006 12:24 PM |
| turning case into a if statement | brentdeback | Shell Programming and Scripting | 2 | 12-02-2005 08:12 PM |
| case statement | Bab00shka | Shell Programming and Scripting | 1 | 07-15-2002 02:31 AM |
|
|
LinkBack | Thread Tools | Display Modes |
| Forum Sponsor | ||
|
|
|
||||
|
Zeta_Acosta, please don't start a 2nd thread like that. I have merged your threads.
I believe that Ygor has a typo. That mv command probably should be: mv -i $file ${file%.*}.html However, by starting a new thread, you concealed from Ygor that you're determined to use an antique shell. That won't work with the old Bourne shell. |
|
|||
|
Ok I know that Im beginning to get on peoples nerves and I apologise for that! But can I ask one more question in relation to this script (please dont shout about it being a sh, Im sorry)
Why is this not working correctly now: input: dosRename Mike *.mike #!/bin/sh for file do case $file in *'*'*|?.) if [ $file = *.[a-z] ] then type=extension elseif [$file = [a-z].*] type=main fi echo $type ;; *) echo $file is a File ;; esac done output: $ dosRename Mike *.mike Mike is a File BLANKLINE: Thank you all so much for the patience! |
|
||||
|
I don't think that we're shouting. But you are continually tripping over the shortcomings of sh and then inquiring about it. We don't meant to get on your nerves.
Whether or not something like: if [ $file = *.[a-z] ] will even work at all depends on the contents of the current directory. I had to copy the script to an otherwise empty directory to ensure that the shell would not match "*.[a-z]". Once I did that that, the test command will see the string *.[a-z]. But the test command does not do pattern matching. So to trigger the "if" statement, copy your script to an otherwise empty directory and then use: dosRename Mike '*.[a-z]' You see that is the only string that will match the *.[a-z] in the if statement. I guess that I'm not supposed to mention that this would be a snap with ksh, so I won't. With sh, the only way to match a string against a pattern is with the case statement. Or you can do stuff like using sed to delete the pattern and see if anything is left in the string. The case statement idea is the better solution since it is a builtin command. |
|
|||
|
Perderabo you are the Don of Unix! Thanks so much for everything! I have found the following solution to my problem though and you might be pleased!
#!/bin/sh //I take it this is why it wont work? # # Usage - Display error message and exit # Usage() { [ $# -ne 0 ] && echo "\n$*\n" >&2 cat <<-EOD_USAGE >&2 Usage: $0 file... target file... List of files to rename target Target name EOD_USAGE exit 1 } # # Get and verify arguments # # Argument count [ $# -lt 2 ] && Usage "Missing arguments." # Files to rename while [ $# -gt 1 ] do file=$1 case "$file" in *.*.*|*\**) Usage "Invalid file name : $file" ;; esac [ -e "$file" ] || Usage "File not found : $file" file_list="$file_list $file" shift done # Target name target=$1 case "$target" in *.*.*|*\**\**) Usage "Invalid target name : $file" ;; esac target_nam=`echo "$target" | cut -d. -f1` target_ext=`echo "$target" | cut -d. -f2` # # Rename loop # for file in $file_list do # Actual file name and extension file_nam=`echo "$file" | cut -d. -f1` file_ext=`echo "$file" | cut -d. -f2` # New file name and extension if [ "$target_nam" = '*' ] then new_nam="$file_nam" else new_nam="$target_nam" fi if [ "$target_ext" = '*' ] then new_ext="$file_ext" else new_ext="$target_ext" fi [ -n "$file_ext" ] && file_ext=".$file_ext" [ -n "$new_ext" ] && new_ext=".$new_ext" new="${new_nam}${new_ext}" # Rename echo "Rename file $file to $new ..." if [ -e "$new" ] then echo "Not renamed, target file already exists : $new" >&2 else mv "$file" "$new" fi done but when I come to use run it, its say dosRename not found in home directory, is this because its a sh instead of it being a ksh? Or is it something else? Many many many many thanks |
|||
| Google UNIX.COM |