script not working in RedHat Linux.
Hi All,
The following script checks whether the filename is correct then the script proceeds to check whether you have read,write and execute permisiions to the file and displays an approriate message. Code:
#ss23 Code:
s23:line 3: if[!-z test]: command not found i have seen this program in a shell scripting book and tried it but it has shown the above said error. '-z ' returbs true if the lenght of the string is zero. what would be the equivalent command in Redhat linux. I am using Redhat llinux 9 version. Please advice. Thanks Srini... |
Try some spaces to be sure it understands what you mean.
e.g. Change "if[!-z "$fname"] then" to: if [ ! -z "$fname" ] then and change "if [-r $fname -a -w $fname -a -x $fname]" to: if [ -r $fname -a -w $fname -a -x $fname ] spaces are cheap and help make your code more legible. They also help to demarcate what you are doing "if[!-z" appears as if it were the name of a command - the spaces let it know that the if is the command (builtin), the [ is the start of the test, the ! is the negation and the "-z" is the test condition start. Also you don't need the quotes around $fname. The appropriate way to set off a shell variable is ${fname} if you need to add something to it but you don't even need that since you're not adding anything to the end of the variable. |
He'll need quotes if fname has spaces in it ...
I'm pretty sure that spaces around [ ] and [[ ]] are in fact reqd. |
Quotes around [] (test) are not required so I'm not sure why you said they are.
As to the other - I didn't say he couldn't use the quotes - I said they were unnecessary. You are correct that in many (sometimes bizarre) scenarios extra quoting and/or escaping is necessary but that is no reason to clutter up all your code with them - especially as one of the most common issue seen in new scripts is unmatched quotes. I once needed a fairly bizarre syntax in a command where I had to quote something then escape the quotes then quote the escapes due to the pipeline I was using. However, that didn't make me want to do it every time. One could write: "This" "sentence" "is" "grammatically" "correct". You'd understand what it meant but would think the author a moron to have typed it that way. |
Actually, I said 'spaces' around [ ] , [[ ]]
Re quotes: given that OP appears to be a newbie and he didn't provide any sample filenames, I thought it was advisable to point out that he would need quotes IF fname has spaces in it. We've both seen plenty of posts here that turn out to be that issue. I agree that un-necessary quotes should, in general, be avoided. :) |
All times are GMT -5. The time now is 08:52 AM. |