Чтение онлайн

ЖАНРЫ

Искусство программирования на языке сценариев командной оболочки

Купер Мендель

Шрифт:

local -a -i INDEX=( 0 0 ) # Local of index to return

local -i Lidx Lcnt

local -i if=0 of=0 # Default to variable names

case "$#" in # Simplistic option testing

0) return 1 ;;

1) return 1 ;;

2) : ;; # Poor man's continue

3) case "$1" in

– if) if=1 ;;

– of) of=1 ;;

* ) return 1 ;;

esac ; shift ;;

4) if=1 ; of=1 ; shift ; shift ;;

*) return 1

esac

# Make local copy of list

case "$if" in

0) eval LIST=\( \"\$\{$1\[@\]\}\" \) ;;

1) LIST=( $(cat $1) ) ;;

esac

# Grok (grope?) the array

Lcnt=${#LIST[@]}

Lidx=0

until (( Lidx >= Lcnt ))

do

if IsNumber ${LIST[$Lidx]}

then

local -i inode name

local ft

inode=Lidx

local m=${LIST[$Lidx+2]} # Hard Links field

ft=${LIST[$Lidx+1]:0:1} # Fast-Stat

case $ft in

b) ((Lidx+=12)) ;; # Block device

c) ((Lidx+=12)) ;; # Character device

*) ((Lidx+=11)) ;; # Anything else

esac

name=Lidx

case $ft in

– ) ((Lidx+=1)) ;; # The easy one

b) ((Lidx+=1)) ;; # Block device

c) ((Lidx+=1)) ;; # Character device

d) ((Lidx+=1)) ;; # The other easy one

l) ((Lidx+=3)) ;; # At LEAST two more fields

# A little more elegance here would handle pipes,

#+ sockets, deleted files - later.

*) until IsNumber ${LIST[$Lidx]} || ((Lidx >= Lcnt))

do

((Lidx+=1))

done

;; # Not required

esac

INDEX[${#INDEX[*]}]=$inode

INDEX[${#INDEX[*]}]=$name

INDEX[0]=${INDEX[0]}+1 # One more "line" found

# echo "Line: ${INDEX[0]} Type: $ft Links: $m Inode: \

# ${LIST[$inode]} Name: ${LIST[$name]}"

else

((Lidx+=1))

fi

done

case "$of" in

0) eval $2=\( \"\$\{INDEX\[@\]\}\" \) ;;

1) echo "${INDEX[@]}" > "$2" ;;

esac

return 0 # What could go wrong?

}

# # # # # Content Identify File # # # # #

#

# DigestFile Input-Array-Name Digest-Array-Name

# or

# DigestFile -if Input-FileName Digest-Array-Name

# # # # #

# Here document used as a comment block.

: <<DigestFilesDoc

The key (no pun intended) to a Unified Content File System (UCFS)

is to distinguish the files in the system based on their content.

Distinguishing files by their name is just, so, 20th Century.

The content is distinguished by computing a checksum of that content.

This version uses the md5sum program to generate a 128 bit checksum

representative of the file's contents.

There is a chance that two files having different content might

generate the same checksum using md5sum (or any checksum). Should

that become a problem, then the use of md5sum can be replace by a

cyrptographic signature. But until then...

The md5sum program is documented as outputting three fields (and it

does), but when read it appears as two fields (array elements). This

is caused by the lack of whitespace between the second and third field.

So this function gropes the md5sum output and returns:

[0] 32 character checksum in hexidecimal (UCFS filename)

[1] Single character: ' ' text file, '*' binary file

[2] Filesystem (20th Century Style) name

Note: That name may be the character '-' indicating STDIN read.

DigestFilesDoc

DigestFile

{

local if=0 # Default, variable name

local -a T1 T2

case "$#" in

3) case "$1" in

– if) if=1 ; shift ;;

* ) return 1 ;;

esac ;;

2) : ;; # Poor man's "continue"

*) return 1 ;;

esac

case $if in

0) eval T1=\( \"\$\{$1\[@\]\}\" \)

T2=( $(echo ${T1[@]} | md5sum -) )

;;

1) T2=( $(md5sum $1) )

;;

esac

case ${#T2[@]} in

0) return 1 ;;

1) return 1 ;;

2) case ${T2[1]:0:1} in # SanScrit-2.0.5

\*) T2[${#T2[@]}]=${T2[1]:1}

T2[1]=\*

;;

*) T2[${#T2[@]}]=${T2[1]}

T2[1]=" "

;;

esac

;;

3) : ;; # Assume it worked

*) return 1 ;;

esac

local -i len=${#T2[0]}

if [ $len -ne 32 ] ; then return 1 ; fi

eval $2=\( \"\$\{T2\[@\]\}\" \)

}

# # # # # Locate File # # # # #

#

# LocateFile [-l] FileName Location-Array-Name

# or

# LocateFile [-l] -of FileName Location-Array-FileName

# # # # #

# A file location is Filesystem-id and inode-number

# Here document used as a comment block.

: <<StatFieldsDoc

Based on stat, version 2.2

stat -t and stat -lt fields

[0] name

[1] Total size

File - number of bytes

Symbolic link - string length of pathname

[2] Number of (512 byte) blocks allocated

[3] File type and Access rights (hex)

[4] User ID of owner

[5] Group ID of owner

[6] Device number

[7] Inode number

[8] Number of hard links

[9] Device type (if inode device) Major

[10] Device type (if inode device) Minor

[11] Time of last access

May be disabled in 'mount' with noatime

atime of files changed by exec, read, pipe, utime, mknod (mmap?)

Поделиться с друзьями: