LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2007, 09:28 AM   #1
srinivasocp
LQ Newbie
 
Registered: Jul 2007
Posts: 6

Rep: Reputation: 0
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
#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.

Please advice.

Thanks
Srini...
 
Old 07-12-2007, 09:36 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 07-13-2007, 12:59 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
He'll need quotes if fname has spaces in it ...
I'm pretty sure that spaces around [ ] and [[ ]] are in fact reqd.
 
Old 07-13-2007, 10:05 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 07-15-2007, 08:57 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with foreach script in redhat linux maker10 Programming 1 10-11-2006 12:11 PM
Need help with a short Linux command line script - working with split files bpmee Linux - Newbie 3 10-04-2006 02:08 AM
Linux Bash Script Not Working?? helptonewbie Programming 12 08-15-2006 12:57 PM
XWindows Not working in RedHat Linux 9.0 laarjo Linux - Hardware 7 11-17-2003 02:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration