alias defining problem in .cshrc file

Hi folks,

I'm trying to define the following command as alias in .cshrc file:
ls -ltr | grep ^d | awk '{print $9}' | xargs du -hs

I defined it as the following:

alias nirdirs '`ls -ltr | grep "^d" | awk "{print \\$9}" | xargs du -hs`'

I've got the following error when I've run the alias:
dwhdev > nirdirs
awk: syntax error near line 1
awk: illegal statement near line 1
2.5M: Command not found.

How to define it correctly?

Thanks in advance,
Nir

Something like this:

 
alias nirdirs="ls -ltr | grep "^d" | awk '{print \$9}'|xargs du -s"

Hi Panyam,

Thanks , but it still fails:
awk: syntax error near line 1
awk: illegal statement near line 1

Thanks in advance,
Nir

Are you using Solaris? If so, use /usr/xpg4/bin/awk (or nawk)

Why escape the $9 as \$9? Is that a csh-ism? It shouldn't be necessary if the awk is single-quoted.

Edit: actually, with hindsight, my (first) question was dumb. Even awk on Solaris could do that!

Hi.

The comments show refinements and corrections:

#!/bin/csh

# @(#) s1       Demonstrate alias.

echo
setenv LC_ALL C ; setenv LANG C
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version "=o" csh
echo

# alias nirdirs '`ls -ltr | grep "^d" | awk "{print \\$9}" | xargs du -hs`'
# alias nirdirs '`ls -ltr | grep "^d" | nawk "{print \\$9}" | xargs du -hs`'
# alias nirdirs '`ls -ltr | grep "^d" | nawk "{print \$9}" | xargs du -hs`'
# alias nirdirs '`ls -ltr | grep "^d" | nawk "{print $9}" | xargs du -hs`'
alias nirdirs 'ls -ltr | grep "^d" | nawk "{print $9}" | xargs du -hs'

alias

nirdirs

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility version)
OS, ker|rel, machine: SunOS, 5.10, i86pc
csh Aug 8 2006 (SunOS 5.10)

nirdirs ls -ltr | grep "^d" | nawk "{print $9}" | xargs du -hs
   3K   .

Good luck ... cheers, drl

Hi drl,

Thanks!

When I'm running this script I'm getting the following output:

 dwhdev > ./check_alias.csh

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility version)

   0K   drwxr-xr-x
   0K   2
   0K   oracle
   0K   dba
   0K   8192
   0K   May
   0K   6
   0K   2009
 376K   check_version
   0K   drwxr-xr-x
   0K   2
   0K   oracle
   0K   dba
   0K   8192
   0K   Jan
   0K   17
   0K   19:50
 608K   logs

The requested output should be:

dwhdev > ls -ltr | grep "^d" | awk '{print $9}' | xargs du -hs
 376K   check_version
 608K   logs

Thanks in advance,
Nir

Hi.

I think csh is causing trouble when processing "$9". This variation seemed to work for me:

alias nirdirs 'ls -ltr | grep "^d" | tr -s " " | cut -d " " -f9 | xargs du -hs'

producing in my test directory that contains a directory "d1":

   7K   d1

Now you have an idea why people generally avoid scripting with the csh family, and use members of the Bourne shell family: sh, bash, ksh, zsh.

Best wishes ... cheers, drl

Hi drl,

Thanks a lot!!
Now , it's working perfect!

Best regards,
Nir