Solved

setting variable in C shell

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sed -- search an replace multiple pattern 11 76
Unix Question 19 50
How to Insert a File Using Text Editor 9 89
awk sed variable in file 3 38
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…
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…
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…
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…

810 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