?
Solved

setting variable in C shell

Posted on 2000-03-09
4
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 21

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

719 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