Shell script instance

Source: Internet
Author: User

Shell script instance 1. Switch the working directory to the directory given by the location parameter, and send hello to each file or subdirectory in these directories in sequence. The number of various files and subdirectories under these directories is counted and displayed. 01 #! /Bin/sh02 03 # Switch the working directory to the directory specified by the location parameter 04 # Send hello to each file or subdirectory in these directories in sequence 05 # count the number of various files and subdirectories in these directories, 06 07if [$ #-lt 1] 08then09 echo "parameter's number error! "; 10 exit 1; 11fi; 12 13 while [$ #-gt 0] 14do15 if [-f $1] 16 then17 echo" $1 is not a dir! "; 18 else 19 cd $120 21 bfiles = 0; # block Device Files 22 cfiles = 0; # character device files 23 pfiles = 0; # Pipe files 24 lfiles = 0; # Link file/directory 25 sfiles = 0; # sock file 26 files = 0; # common file 27 dirs = 0; # directory file 28 total = 0; # total files 29 for var in 'ls $ 1' 30 do31 total = 'expr $ total + 1'; 32 echo "hello $ var "; 33 if [-B $ var] 34 then35 bfiles = 'expr $ bfiles + 1'; 36 elif [-c $ var] 37 then38 cfiles = 'expr $ cfiles + 1 '; 39 elif [-S $ var] 40 then41 sfiles = 'expr $ sfiles + 1'; 42 elif [-p $ var] 43 then44 pfiles = 'expr $ pfiles + 1 '; 45 elif [-h $ var] 46 then47 lfiles = 'expr $ lfiles + 1'; 48 elif [-f $ var] 49 then50 files = 'expr $ files + 1 '; 51 elif [-d $ var] 52 then53 dirs = 'expr $ dirs + 1'; 54 fi; 55 done56 57 echo "the number of bfiles is $ bfiles "; 58 echo "the number of cfiles is $ cfiles"; 59 echo "the number of lfiles is $ lfiles"; 60 echo "the number of sfiles is $ sfiles "; 61 echo "the number of pfiles is $ pfiles"; 62 echo "the number of files is $ files"; 63 echo "the number of dirs is $ dirs "; 64 echo "total file number is $ total"; 65 fi; 66 echo "************************************" 67 shift; 68 done

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.