![]() |
|
|
|
|
|||||||
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
Case Statement
Can anyone please tell me why this wont work! Thanks so much!
#!/bin/sh for file do case $file in *.*.*) echo Cannot have more than 1 dot exit ;; *'**'*) echo Cannot have more than 1 asterisk exit ;; *'*'*|?.) echo this is a target ;; *) echo This is a file item = file ;; esac done if [! -e "$item"]; echo The File does not exit exit fi It just says Item not found? Thanks |
| Forum Sponsor | ||
|
|
|
||||
|
Two things. You write
Code:
if [! -e "$item"]; echo The File does not exit exit fi Code:
if [ ! -f "$item" ]; then echo "$item does not exist" exit fi You've also got spaces in your variable assignment, and no $ infront of the file variable name to reference it. You are, in effect, setting the value of item to "file". Another "no, no!" Do this Code:
item=$file Elaborate more on the script. What does it do? What is it for? And give the "proper" output from the script. Cheers ZB http://www.zazzybob.com |
|
|||
|
Hi Zazzybob, thanks for the help so far! Ok to elaborate more on what I am trying to do:
Well I'm trying to create a script that looks at parameters followed by a target that that specifies either a new main part of the file name or a new extension part but not both. Rename File1 File2 File3 *.html So File1 to File3 are params and *.html is is the target. Then with my loop Im trying to check the validity of them eg less than 2 params, cant have 2 dots, more than one asterisk and doesnt exist. Then If there are no errors the files are to be renamed in another loop. But I'm really struggling with it and Its really beginning to get on my nerves as I dont know what Im doing wrong! Any Suggestions on how I should do it would be fantastic. By the way, Im doing this becuase I have loads of files that I want to change extensions on and I want to do it this way because I've been trying for ages. Again many thanks. |
|
|||
|
Can anyone please help me (again) with this statement:
*'**'*) echo Cannot have more than 1 asterisk exit ;; As you can tell its supposed to drop out if there is more than 1 asterisk, but it doesnt! Many thanks. Also can anyone tell me what the structure of these statements are eg I know that * selects the whole string and ? selects a char. But how do I check if there are less than 2 parameters in my statements or if the parameter has to have a dot within the string! Thank you all so very much (I'm beginning to tear my hair out)! |
|
||||
|
It does seem to work:
$ set a'**'b $ case $1 in *'**'*) echo true ;; esac true P.S. Why go to the bother of writing a rename script? Just do... for file in files*.txt do mv -i $file ${file%.*}.html done Last edited by Ygor; 04-01-2004 at 05:58 AM. |
|
||||
|
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 |
|
||||
|
You probably don't have your home directory in your PATH. (And good for you! I don't either.)
Try using: ./dosRename .... The leading ./ says to find it in your current directory rather than searching the directories on your PATH. |
||||
| Google UNIX.COM |
| Thread Tools | |
| Display Modes | |
|
|
|
The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
|
| 421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix curses.h cut command in unix daemon process find grep find mtime find null character in a unix file from ip can we get machine name +unix glance unix grep multiple lines grep or grep recursive how to redirect console logs in unix inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime perl array length ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script |