coanor

时间总在流逝,思绪过处,留下只言片语。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
# these shell code used to test file attributes
# block device
device0="/dev/sda2"
if [ -b "$device0" ]
then
        echo "$device0 is a block device."
fi

# character device
device1="/dev/ttyS1"    # PCMCIA modem card
if [ -c "$device1" ]
then
        echo "$device is a character device."
fi

# pipe
function show_input_type()
{
        [ -p /dev/fd/0 ] && echo PIPE || echo STDIN
}

show_input_type "Input"         # STDIN
echo "Input" | show_input_type  # pipe

# block device
device0="/dev/sda2"
if [ -b "$device0" ]
then
        echo "$device0 is a block device."
fi

# character device
device1="/dev/ttyS1"    # PCMCIA modem card
if [ -c "$device1" ]
then
        echo "$device is a character device."
fi

# pipe
function show_input_type()
{
        [ -p /dev/fd/0 ] && echo PIPE || echo STDIN
}

show_input_type "Input"         # STDIN
echo "Input" | show_input_type  # pipe
 1 #!/bin/bash
 2 # Find dead symlinks and output them quoted,
 3 # so they can be fed to xargs and dealt with.
 4 #
 5 #   e.g. sh broken-link.sh /somedir /someotherdir | xargs rm
 6 #
 7 # This, however, is better method:
 8 
 9 # find "somedir" -type l -print0 |\
10 #         xargs -r0 file |\
11 #         grep "broken symbolic" |\
12 #         sed -e 's/^\|: *broken symbolic.*$/"/g'
13   
14 # But above code would not be PURE Bash
15 
16 # Caution: beware the /proc file system and any circular links!
17 
18 # If no args are passed to the script set directories-to-search
19 # to current directory. Otherwise set the directory-to-search to
20 # the args passed.
21 
22 [ $# -eq 0 ] && directorys=`pwd` || directorys=$@
23 
24 linkchk() {
25         for element in $1/*; do
26                 [ -h "$element" -a ! -e "$element" ] && echo \"$element\"
27                 [ -d "$element" ] && linkchk $element
28         # Of course, '-h' test for symbolic link, '-d' for directory.
29         done
30 }
31 
32 for directory in $directorys; do
33         if [ -d $directory ]
34         then linkchk $directory
35         else
36                 echo "$directory is not a directory"
37                 echo "Usage: $0 dir1 dir2 ..."
38         fi
39 done
40 
41 exit $?
posted on 2012-09-18 01:41  coanor  阅读(165)  评论(0编辑  收藏  举报