LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script not working in RedHat Linux. (https://www.linuxquestions.org/questions/programming-9/script-not-working-in-redhat-linux-568748/)

srinivasocp 07-12-2007 09:28 AM

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...

MensaWater 07-12-2007 09:36 AM

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.

chrism01 07-13-2007 12:59 AM

He'll need quotes if fname has spaces in it ...
I'm pretty sure that spaces around [ ] and [[ ]] are in fact reqd.

MensaWater 07-13-2007 10:05 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.

chrism01 07-15-2007 08:57 PM

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.