alias defining problem in .cshrc file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

alias defining problem in .cshrc file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-23-2010
nir_s nir_s is offline
Registered User
 
Join Date: Jun 2004
Last Activity: 2 April 2012, 3:20 AM EDT
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
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:

Code:
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
Sponsored Links
    #2  
Old 02-23-2010
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 3 April 2014, 10:13 AM EDT
Posts: 1,148
Thanks: 20
Thanked 104 Times in 99 Posts
Something like this:


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

Sponsored Links
    #3  
Old 02-23-2010
nir_s nir_s is offline
Registered User
 
Join Date: Jun 2004
Last Activity: 2 April 2012, 3:20 AM EDT
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Panyam,

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

Thanks in advance,
Nir
    #4  
Old 02-23-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 April 2014, 2:43 AM EDT
Location: in der Schweiz
Posts: 8,322
Thanks: 313
Thanked 1,013 Times in 886 Posts
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!

Last edited by Scott; 02-23-2010 at 05:52 PM..
Sponsored Links
    #5  
Old 02-23-2010
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 22 April 2014, 8:36 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,634
Thanks: 24
Thanked 179 Times in 163 Posts
Hi.

The comments show refinements and corrections:

Code:
#!/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:

Code:
$ ./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
Sponsored Links
    #6  
Old 02-24-2010
nir_s nir_s is offline
Registered User
 
Join Date: Jun 2004
Last Activity: 2 April 2012, 3:20 AM EDT
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
Hi drl,

Thanks!

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

Code:
 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:

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

Thanks in advance,
Nir
Sponsored Links
    #7  
Old 02-24-2010
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 22 April 2014, 8:36 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,634
Thanks: 24
Thanked 179 Times in 163 Posts
Hi.

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

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

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

Code:
   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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with my .cshrc perl0101 Shell Programming and Scripting 1 02-02-2010 05:38 PM
.cshrc file zainster Virtualization and Cloud Computing 3 07-15-2009 10:13 AM
Problem defining remote printers on solaris 10 bonovox Solaris 2 10-16-2008 09:53 AM
word too long..problem while sourcing .cshrc mobydick UNIX for Dummies Questions & Answers 1 05-08-2008 09:11 PM
Sorting/Filed Defining/Output problem TiredOrangeCat Shell Programming and Scripting 1 02-11-2007 11:41 PM



All times are GMT -4. The time now is 08:08 AM.