Shell Programming Examples

Source: Internet
Author: User

organized a few shell programming examples, for beginners! 1. InThe home directory creates 100 directories and the directory name is a1......a100. Instance 1 for((i=1; i<= -; i++)) Do    mkdir "/home/a"$i DoneExample 2, you can also write a bit more advanced for((i=1; i<= -; i++)) Do    if[!-E"/home/a"$i] Then            mkdir "/home/a"$iElse        Echo "/home/a"$i"exist!"    fi   Done2. Write a script that automatically packages all files less than 5KB in the home directory into XX.Tar. Gz. (Hint: with LS,grep, find and other commands, files generally refer to ordinary files) find implementationFind./-type F-size +5120c |Xargs Tar-CZVF xx.Tar. GZls, grep implementsls-L |grep^- |awk '{if ($5<5120) print $9}'|Xargs Tar-czfTar. GZ3. Write a program that takes the first column of/et/passwd out, and each column is a line of string "the1Account is "root" to show #!/bin/Bashi=1 whileRead Line Doname=`Echo$line |Cut-D":"-F1 'Echo " the"$i" Account is"$name i=$ ((i+1)) Done</etc/passwdOne command is enough to take the first line.Cat/etc/passwd|Cut-D":"-F14. Write a program whose role is to look at/root/test/firstlogical the name exists, and if it does not exist, create a file. Use touch to create, leave after creation is complete, and if present, determine if the name is a file, and if it is deleted, create a new directory. The file name is Loglical, then left, and if present, and the name is directory, delete the directory. #!/bin/Bashif[-E"/root/work/logical" ] Then    if[-F"/root/work/logical" ]     Then         RM "/root/work/logical"         mkdir "/root/work/logical"     elif[-D"/root/work/logical"  ]     Then            RM-rf"/root/work/logical"    fi  Else    Touch "/root/work/logical" fi    5. Write a shell script that reads 10 numbers from the keyboard, showing the maximum and minimum values. Comparison of the difficulty integers and the distinction of string comparators min=0Max=0; var=0 for((i=0;i<Ten; i++)) Doread Varif[$i = =0 ]      Thenmin=$var; Max=$var; Else        if[$min-GT $var] Thenmin=$var; fi          if[$max-LT $var] Thenmax=$var; fi      fi;  DoneEcho$maxEcho$min6. Write a script that prints any number of multiplication tables. Print If you enter 31*1=1  2*1=2 2*2=4  3*1=3 3*2=6 3*3=9Difficulty:Echonon-newline output var=0read var; for((i=1; i<=var;i++)) Do     for((j=1; j<=i;j++))     Do          Echo-ne"$i * $j ="$ ((i*j))" "     Done    Echo-ne"\ n" Done                                                                                                          7write a script, enter your own birthday time (YYYYMMDD), and calculate how many days and hours are your birthdays. The operation of time is usually achieved by time stamp.

Shell Programming Examples

Related Article

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.