Solved

setting variable in C shell

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

738 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