Solved

setting variable in C shell

Posted on 2000-03-09
4
205 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now