LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CSH Permission Denied When Accessing Local File (https://www.linuxquestions.org/questions/programming-9/csh-permission-denied-when-accessing-local-file-867590/)

wafflesausage 03-09-2011 08:49 PM

CSH Permission Denied When Accessing Local File
 
I've just recently started learning shell scripting and I've been working on a basic csh script, but I've been having a few problems. Here's the script
Code:

#!/bin/csh
echo Enter a file name
$< = FILE                                      ##Name of file
echo enter a size (in kilobytes) to monitor
SIZETOMON = $<                                  ##Size value that's input by user
du -k $FILE = $SIZE                            ##Size of the file that the user wishes to monitor
while (1)
if $SIZE > $SIZETOMON then
echo ALERT: File size is greater than $SIZETOMON
end

And here's the output
Code:

/home/lucer/foo.txt: Permission denied.
Badly placed ()'s.

I'm not really sure what the issue is with the "badly placed ()'s" or why it won't let me access files that I can access with the same shell when it's not in a script. Anyone have any ideas?

Edit:Fixed glaring punctuation error

corp769 03-09-2011 09:15 PM

Try enclosing the echo statements in quotation marks:
Code:

#!/bin/csh
echo "Enter a file name"
$< = FILE
echo "enter a size (in kilobytes) to monitor"
SIZETOMON = $<
du -k $FILE = $SIZE
while (1)
if $SIZE > $SIZETOMON then
echo "ALERT: File size is greater than $SIZETOMON"
end


wafflesausage 03-09-2011 09:30 PM

Quote:

Originally Posted by corp769 (Post 4284957)
Try enclosing the echo statements in quotation marks:
Code:

#!/bin/csh
echo "Enter a file name"
$< = FILE
echo "enter a size (in kilobytes) to monitor"
SIZETOMON = $<
du -k $FILE = $SIZE
while (1)
if $SIZE > $SIZETOMON then
echo "ALERT: File size is greater than $SIZETOMON"
end


I just solved the parentheses error by removing the parentheses around "in kilobytes". It seemed to be superficial, and I could probably find a way to esape it, but that's not my actual concern. The permission error is still there.

corp769 03-09-2011 10:02 PM

Are you running the script as your normal user? And is the foo.txt already there? If so, what are the permissions of the file?

wafflesausage 03-09-2011 10:10 PM

Quote:

Originally Posted by corp769 (Post 4285003)
Are you running the script as your normal user? And is the foo.txt already there? If so, what are the permissions of the file?

Yes, I tried it before with sudo and it didn't work. I just now I tried it with the permissions of 770 and it worked without root privileges, but now it says
Code:

Enter a file name
/home/lucer/foo.txt
/home/lucer/foo.txt: 1: SDFDFA: not found
enter a size in kilobytes to monitor
89
89: Command not found.
FILE: Undefined variable.

"SDFDFA" is the contents of the file, but I need $FILE to be the input from stdin.

corp769 03-09-2011 10:23 PM

I think you need to have it like so:
Code:

set file = $<

wafflesausage 03-09-2011 11:08 PM

Quote:

Originally Posted by corp769 (Post 4285016)
I think you need to have it like so:
Code:

set file = $<

Yes, thanks a lot. Now all I have to do is find a way to chop off the non-numerical data from du and get around the "variable must begin with number" error.

corp769 03-09-2011 11:42 PM

What do you mean? Show me your code so far.

wafflesausage 03-10-2011 12:27 AM

Quote:

Originally Posted by corp769 (Post 4285051)
What do you mean? Show me your code so far.

Code:

#!/bin/csh
echo Enter a file name
set file aFILE = $<                                            ##Name of file
echo enter a size in kilobytes to monitor
set SIZE = $<                                                  ##Size value that's input by user
set SIZETOMON = du -k $aFILE | sed s/$aFILE//g                  ##Size of the file that the user wishes to monitor
while (1)
if $SIZE > $SIZETOMON then
echo ALERT: File size is greater than $SIZETOMON
end

If you want to run it for yourself, you can't specify a full path because of the way I set it set up to sed out the non-numerical characters from the output of du. I'll probably just find a workaround to that myself if it becomes to much of an inconvenience.

corp769 03-10-2011 12:36 AM

Show me the output of "du -k $aFILE" and "du -k $aFILE | sed s/$aFILE//g" if you could so I can see what you are talking about. I would rather see the output on your end. Of course replace aFILE with the actual file and so forth...

wafflesausage 03-10-2011 12:22 PM

Quote:

Originally Posted by corp769 (Post 4285083)
Show me the output of "du -k $aFILE" and "du -k $aFILE | sed s/$aFILE//g" if you could so I can see what you are talking about. I would rather see the output on your end. Of course replace aFILE with the actual file and so forth...

Actually, I switched that line so it now reads
Code:

du -k $aFILE | awk '{print $1}'
which prints only the size of the specified file in kilobytes (without the name as it did before). I still get the error that states
Code:

set: Variable name must begin with a letter.
SIZETOMON: Undefined variable.

It was my understanding that in shell scripting that one doesn't have to specify whether a variable is a string, float, integer, etc., but that doesn't seem to be the case here. And just to clarify, aFILE is a variable that is specified by the user which the script "asks for" on line 3.

corp769 03-10-2011 12:28 PM

Code:

set SIZETOMON = `du -k $aFILE | sed s/$aFILE//g`
Just noticed this - Try using backticks. Backticks will execute whatever is inside of them.

corp769 03-10-2011 12:30 PM

And I have a question about the following:
Code:

set file aFILE = $<
Shouldn't be this?
Code:

set aFILE = $<

wafflesausage 03-10-2011 12:39 PM

Quote:

Originally Posted by corp769 (Post 4285795)
Code:

set SIZETOMON = `du -k $aFILE | sed s/$aFILE//g`
Just noticed this - Try using backticks. Backticks will execute whatever is inside of them.

I tried it with the backticks, but it still says
Code:

SIZETOMON: Undefined variable
Without the backticks, it says
Code:

set: Variable must begin with a letter
SIZETOMON: Undefined variable.

Really, what I'm trying to do here is have the SIZETOMON variable have the numerical value of the output of
Code:

du -k $aFILE | awk '{print $1}'
and compare it against SIZE, which is a value input by the user.

corp769 03-10-2011 12:42 PM

Can you do what I asked before about running the "du -k $aFILE | sed s/$aFILE//g" by itself on the command line and substituting the appropriate filename?


All times are GMT -5. The time now is 05:03 PM.