nir_s
February 23, 2010, 10:41am
1
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
panyam
February 23, 2010, 11:13am
2
Something like this:
alias nirdirs="ls -ltr | grep "^d" | awk '{print \$9}'|xargs du -s"
nir_s
February 23, 2010, 3:28pm
3
Hi Panyam,
Thanks , but it still fails:
awk: syntax error near line 1
awk: illegal statement near line 1
Thanks in advance,
Nir
Scott
February 23, 2010, 3:37pm
4
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!
drl
February 23, 2010, 6:09pm
5
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
nir_s
February 24, 2010, 4:28am
6
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
drl
February 24, 2010, 10:12am
7
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
nir_s
February 24, 2010, 3:41pm
8
Hi drl,
Thanks a lot!!
Now , it's working perfect!
Best regards,
Nir