if [! -e "$item"];
echo The File does not exit
exit
fi
This has two errors and should read
if [ ! -f "$item" ]; then
echo "$item does not exist"
exit
fi
Amend to that and see if that fixes it.
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
item=$file
as you can see, no spaces either side of the equals.
Elaborate more on the script. What does it do? What is it for? And give the "proper" output from the script.
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.
Rename File1 File2 File3 '*.html'
otherwise if you had "one.html" and "two.html" in your current directory, they would replace *.html on the command line.
Your error arises from your line:
item = file
This cause your script to try to run a program or script called "item" with two args, "=" and "file".
As zazzybob points out you must lose the spaces.
I assume you mean
item=$file
At that point you will be setting "item" in your loop. When you exit the loop, you then check the last value only to see if it exists.
Many shells support -e, and that may be superior to the -f that zazzybob suggests.
Is there any reason to sh? A more powerful shell like ksh or bash would make this easier.
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)!
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:
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
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?
Something is not matched. For example you might have an opening quote but not closing quote. Or you might have a "case" but no "esac".
In this particular case, I suspect that line:
EOD_USAGE
You have it indented. So it doesn't match what you told to expect as the end of the "here document". Either unindent it, or put escaped white space to match up with the <<.