Solved

setting variable in C shell

Posted on 2000-03-09
4
223 Views
Last Modified: 2010-04-21
Here is part of my script I am having problems with.
I want to find the directory size but I want an error redirected to /dev/null.  How do I do this?  

Just fill in what #3 should be to give me the output I want.

Thanks


#!/bin/csh -f
cd /PMC/CENTRAL_LIBRARY/

#1
set dir_size=`du -s | cut -f1 -d"."`
echo "Dir size is: $dir_size"

#outputs
./.netscape/cache: Permission denied
./.netscape/archive: Permission denied
Dir size is: 7343104


#2
set dir_size=`(du -s | cut -f1 -d"." > /dev/tty)>&/dev/null`
echo "Dir size is: $dir_size"

#outputs
7343104
Dir size is:


#3
set dir_size=
echo "Dir size is: $dir_size"

#output I want is:
Dir size is: 7343104
0
Comment
Question by:onestar
4 Comments
 
LVL 15

Expert Comment

by:samri
ID: 2601588
onestar,

  Your problem is that you didn't have enoght rights to browse (read) into .netscape/cache directory.  Check the ownership of the directory (and any subdirectories below it).

  Well maybe it's not the whole point.  I believe you are just interesteed in getting only the output.  Only line #3 right?.

Line 1: /.netscape/cache: Permission denied
Line 2: /.netscape/archive: Permission denied
Line 3: Dir size is: 7343104

Okay, your problem is, when you are in an interactive shell, your STDIN (Standard input) is you keyboard (or what you type), your STDOUT is your display, and your STDERR, is also you display.  To get rid of the first 2 lines (#1, and #2), you need to do some redirection in you command. ( which you already work on it, but unsuccessful).  I'm really naive in that also.  But you can try some trick.

try:

set dir_size=`du -s | cut -f1 -d"." > /tmp/mysize.tmp`
set mysize=`cat /tmp/mysize.tmp`
echo "Dir Size is : $mysize"


give it a shot.

samri
0
 
LVL 1

Author Comment

by:onestar
ID: 2601674
That is just the same as my first attempt.  When I run the script it will still output the Permission denied statement on my screen which I don't want.
0
 
LVL 20

Expert Comment

by:tfewster
ID: 2601829
set dir_size=`du -s | cut -f1 -d"."` 2>/dev/null

- I don't think you use "=" with the "set" command, but try it with & without;
- "2" in this context is STDERR;
So, error messages get binned & the variable is initialised to the output.

IMHO, #1 is the most useful as redirecting STDERR disguises the truth about the size of the directory

In #2, you appear to force STDOUT to the screen instead of "capturing" it in dir_size and throw away everything else
0
 
LVL 5

Accepted Solution

by:
paulqna earned 50 total points
ID: 2606182
Lousy but working !! ;)

#3
temp=`du -s | cut -f1 -d " " 2>/dev/null`
set $temp
dir_size=$1
echo "Dir size is: $dir_size"
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question