The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Alias, function or script (bash) to "revert" cd command? SilversleevesX UNIX for Dummies Questions & Answers 1 05-06-2009 12:30 PM
All alias in .profile lost when "script" command is called amicon007 UNIX for Advanced & Expert Users 3 12-03-2008 03:22 AM
Any idea what this counter means "tcpTimRetrans" purechgo UNIX for Advanced & Expert Users 3 11-18-2008 01:36 AM
Why generate "ash and bash" different output for same bash script? s. murat Shell Programming and Scripting 0 05-26-2008 08:19 AM
can I do something like "alias" within a script? tphyahoo Shell Programming and Scripting 1 06-06-2006 03:05 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-10-2009
SilversleevesX SilversleevesX is offline
Registered User
  
 

Join Date: May 2009
Location: Northeast USA
Posts: 48
Idea for an "informative" bash alias or script

I had the idea come into my head that it would be good to have a single command that gave file type, file size, last modification date, and owner/group information, like what one would see in a GUI "Properties" dialog, but all in a terminal window. In my opinion, these statistics about a file should be available via one command in an xterm or rxvt situation. So I started looking things up.

It turns out that the information such an alias (or script) would return comes from two Linux/Unix commands, "file" and "stat". From file, one can glean mime-type as well as or instead of file type, since most of the time when one types in "file somefile.ext, what comes back is somewhat vague. From stat, one can glean much of the other information (size, last mod date, owner, group).

Now where it falls down for me is in what I don't know about writing scripts. Particularly how to format commands in functions so they handle file name strings entered by the user in the terminal. I've a vague clue there is something on the order of a {FILE} or ${FILE} variable involved, but I can't seem to "stick" the syntax without getting an error returned like "No such file or directory" or something.

Also, if there already is a single command that 'barfs back' the info I'm looking for, I certainly would rather use it than hike further down the scripting path.

Any help would be appreciated.

BZT
  #2 (permalink)  
Old 05-10-2009
pludi's Avatar
pludi pludi is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,961
Something like this:

Code:
$ cat bla.sh
function showAll {
    file "$1"
    stat "$1"
}
$ . ./bla.sh
$ showAll bla.sh
bla.sh: ASCII text
  File: `bla.sh'
  Size: 49              Blocks: 1          IO Block: 65536  regular file
Device: 78355355h/2016760661d   Inode: 9288674231464218  Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1003/pludi)   Gid: (  513/    None)
Access: 2009-05-10 23:45:04.968750000 +0200
Modify: 2009-05-10 23:43:25.859375000 +0200
Change: 2009-05-10 23:43:25.859375000 +0200

  #3 (permalink)  
Old 05-11-2009
SilversleevesX SilversleevesX is offline
Registered User
  
 

Join Date: May 2009
Location: Northeast USA
Posts: 48
Quote:
Originally Posted by pludi View Post
Something like this:
[code]$ cat bla.sh
function showAll {
file "$1"
stat "$1"
}

$ . ./bla.sh
$ showAll bla.sh
bla.sh: ASCII text
File: `bla.sh'
<-snip->
I like it so far. Is there some way of trimming the returned data to just the elements I mentioned in the original post? This might sound like I'm in pursuit of a "one from column A, two from column B" thing, and I know from what I started with before your reply, how difficult it was to get even as far as I did. All your help (and others' who may want to pitch in at this point) is appreciated.

I put the above code in a script, and in a weak tribute to my years and years on Macs, changed the name of the function to GetInfo.

BZT
  #4 (permalink)  
Old 05-11-2009
pludi's Avatar
pludi pludi is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,961
I hate it when something catches my interest and won't go away until I can have a hack at it:
Code:
$ cat fileinfo.sh
function showAll {
    filename=$1
    echo "File: '${1}'"
    type=$( file "${filename}" | cut -d: -f2- )
    stat -c "%F~%U~%u~%G~%g~%s~%x~%y~%z" ${filename} | \
    while IFS='~' read typ user uid group gid size atime mtime ctime
    do
        echo "Type: ${type} (${typ})"
        echo "Ownership: ${user} (${uid}) ${group} (${gid})"
        echo "Size: ${size}"
        echo "Last access: ${atime}"
        echo "Last modification: ${mtime}"
        echo "Last change: ${ctime}"
    done
}
$ showAll fileinfo.sh
File: 'fileinfo.sh'
Type:  ASCII text (regular file)
Ownership: pludi (1000) users (100)
Size: 496
Last access: 2009-05-11 19:25:24.000000000 +0200
Last modification: 2009-05-11 19:23:48.000000000 +0200
Last change: 2009-05-11 19:25:14.000000000 +0200

Tested on Linux and Cygwin. Modify to your hearts desire.
  #5 (permalink)  
Old 05-11-2009
SilversleevesX SilversleevesX is offline
Registered User
  
 

Join Date: May 2009
Location: Northeast USA
Posts: 48
Looks like we've got a GetInfo!

I'll try it in a few. It looks pretty enough. Thanks for the double-barrelled road test btw.

BZT

EDIT: It's a winner, pludi. Much thanks.

Last edited by SilversleevesX; 05-12-2009 at 01:52 AM..
  #6 (permalink)  
Old 05-14-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by pludi View Post
I hate it when something catches my interest and won't go away until I can have a hack at it:
Code:
$ cat fileinfo.sh
function showAll {

The standard syntax for function declaration is:


Code:
showAll() {

Quote:

Code:
    filename=$1
    echo "File: '${1}'"
    type=$( file "${filename}" | cut -d: -f2- )

There's no need for an external command (cut):


Code:
type=$( file "${filename}" )
type=${type#*:}

Quote:

Code:
    stat -c "%F~%U~%u~%G~%g~%s~%x~%y~%z" ${filename} | \

There's no need for a backslash after a pipe. It is only necessary when the command would be complete as it stands; a command ending with a pipe (or && or ||, etc.) is not complete, therefore the backslash is unnecessary.
Quote:

Code:
    while IFS='~' read typ user uid group gid size atime mtime ctime
    do
        echo "Type: ${type} (${typ})"
        echo "Ownership: ${user} (${uid}) ${group} (${gid})"
        echo "Size: ${size}"
        echo "Last access: ${atime}"
        echo "Last modification: ${mtime}"
        echo "Last change: ${ctime}"
    done
}
$ showAll fileinfo.sh
File: 'fileinfo.sh'
Type:  ASCII text (regular file)
Ownership: pludi (1000) users (100)
Size: 496
Last access: 2009-05-11 19:25:24.000000000 +0200
Last modification: 2009-05-11 19:23:48.000000000 +0200
Last change: 2009-05-11 19:25:14.000000000 +0200

Tested on Linux and Cygwin. Modify to your hearts desire.

Next exercise: modify the function so that it works on other systems. BSD systems have a stat command that uses a different syntaxt; other systems do not have a stat command.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0