Sponsored Content
Top Forums Shell Programming and Scripting Idea for an "informative" bash alias or script Post 302316334 by cfajohnson on Thursday 14th of May 2009 07:51:30 PM
Old 05-14-2009
Quote:
Originally Posted by pludi
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.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Why is it Bad Idea to insert "." (Dot) to PATH ?

I was told that it's a Bad Idea (especially for root ) to Add To the Variable $PATH in unix the ":." (dot), In order to execute programs in my current directory without typing ./program For example: PATH=$PATH:$HOME/bin:. Does someone know why is it a Bad Idea? (2 Replies)
Discussion started by: amitbern
2 Replies

2. Shell Programming and Scripting

can I do something like "alias" within a script?

Within a script, I want to switch perl executable. I can do this within .bashrc with the alias feature, but the following didn't work when I tried it within a bash script. alias perl='perlroot/bin/perl' which perl # want this to be above perl, but it's still /usr/bin/perl. Can someone... (1 Reply)
Discussion started by: tphyahoo
1 Replies

3. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies

4. UNIX for Advanced & Expert Users

All alias in .profile lost when "script" command is called

Hi, I was trying to call "script <an ip add>" command from .profile file to log everything whenever anyone logs in to this user. I did the following at the end of .profile. 1) Extracted the IP address who logged in 2) Called script < ip add> . The problem I am facing is all, aliases etc. written... (3 Replies)
Discussion started by: amicon007
3 Replies

5. UNIX for Dummies Questions & Answers

Alias, function or script (bash) to "revert" cd command?

In all of my brief and superficial experience with Unix or Linux, the one curious and consistent thing has been that 'cd ./' (back up one directory level) has done absolutely nothing in any of them. Now I understand that, at least for bash, 'cd ./' appears to have been substituted by 'cd ..' Am... (1 Reply)
Discussion started by: SilversleevesX
1 Replies

6. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

7. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

10. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies
All times are GMT -4. The time now is 09:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy