ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#usage ss23
echo "enter any filename \c"
read fname
if[!-z "$fname"] then
if [-r $fname -a -w $fname -a -x $fname]
then
echo you have read,wirte and execute permisions to $fname
else
echo read,write and execute permisions denied
fi
else
echo improper filename
fi
when i run this script and give the filename 'test' it is showing the following error.
Code:
s23:line 3: if[!-z test]: command not found
s23:line 4: if [-r test -a -w $fname -a -x test] : command not found
s23:line 5:syntax error near unexpected token 'then'
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.
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.
Last edited by MensaWater; 07-17-2007 at 09:02 AM.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.