Shell Script Instance

Source: Internet
Author: User
Tags case statement gz file

1. Analog Linnux Login Shell

#/bin/bashecho-n "Login:" Read nameecho-n "password:" Read passwdif [$name = "CHT"-a $passwd = "abc"];thenecho "The Hos T and password is right! " else echo "Input is error!" Fi

2. Compare two number sizes

#/bin/bashecho "Please enter the number" read aread bif Test $a-eq $bthen echo "The" "No.2" elif test $a-gt $bthen echo " The > no.2 "Else echo" the very same < No.2 "fi

3. Find out if the file exists under the/root/directory

#/bin/bashecho "Enter a file name:" Read AIF test  -e/root/$athen echo "The file is exist!" else echo "The file is not exist!" Fi

Use of 4.for loops

#/bin/bashclearfor num in 1 2 3 4 5 6 7 8 9 10do    echo ' $num ' done

5. Command line Input

#/bin/bashecho "Please enter a User:" Read ab=$ (whoami) if test $a = $bthen echo "The user is running." else echo "The user is not running." Fi

6. Delete the file size 0 in the current directory

#/bin/bashfor filename in ' ls ' does    if test-d $filename then    b=0    Else          a=$ (ls-l $filename | awk ' {print $ } ')            if test $a-eq 0 then             rm $filename             fi        fi     done

7. If there is a file under/export/um_lpp_source, change its file system size to 3G

#/bin/bashwhile line= ' Ls/export/um_lpp_source ' do        if test $line = "then  echo" NULL "             sleep 1    else echo $line                chfs-a size=3g/export/um_lpp_source                 exit 0        Fidone

  

8. Test the IP address

#/bin/bashfor i in  1 2 3 4 5 6 7 8 9do    echo "The number of $i computer is"    ping-c 1 192.168.0. $idone

9. If the size of the Test.log is greater than 0, then the *.tar.gz file in the/opt directory

#/bin/sha=2while name= "Test.log" Do        sleep 1        b=$ (ls-l $name | awk ' {print $} ')        if test $b-ge $a        #then Ech O "OK" then    ' cp/opt/*.tar.gz. '        exit 0        Fidone

10. Print the contents of the read, prepare for the following example

#/bin/bashwhile Read Namedoecho $namedone

11. Read content from 0.sh and print

#/bin/bashwhile Read Linedo    echo $linedone < 0.sh

12. Read the contents of the A.C and add 1 operations

#/bin/bashtest-e a.cwhile Read Linedo    a=$ (($line + 1)) Done < A.cecho $a

13. Ordinary parameterless function

#/bin/bashp () {    echo "Hello"}p

14. Passing parameters to a function

#/bin/bashp_num () {    num=$1    echo $num}for n in [email protected]do    p_num $ndone

15. Create a Folder

#/bin/bashwhile:d o    echo "Please input file ' s name:"    read a    if test-e/root/$a    then         echo "The file is Existing please input new file name: "    else        mkdir $a        echo" Aye sussesful! "        Break    Fidone

16. Get the native IP address

#/bin/bashifconfig | grep "inet Addr:" | awk ' {print $} ' | Sed ' s/addr://g '

17. Find the largest file

#/bin/basha=0for  name in *.*do     b=$ (ls-l $name | awk ' {print $} ')    if test $b-ge $a then    a= $b         Namemax = $name     Fidoneecho "The max file is $namemax"

18. Locate the IP user in the current segment and redirect to the Ip.txt file

#/bin/basha=1while:d o    a=$ (($a + 1))    if test $a-GT 255 then break    Else        echo $ (ping-c 1 192.168.0. $a | grep "TTL" | awk ' {print $4} ' | Sed ' s/://g ')        ip=$ (ping-c 1 192.168.0. $a | grep "TTL" | awk ' {print $4} ' | sed ' s/://g ')        echo $ip >> ip.tx T    Fidone

19. Print the current user

#/bin/bashecho "Current User is:" Echo $ (PS | grep "$$" | awk ' {print $} ')

20.case Statement Exercise

#!/bin/bashclearecho "Enter a number from 1 to 5: ' Read Numcase $num in    1 ' echo" you enter 1 "    ;;    2) echo "You enter 2"    ;;    3) echo "You enter 3"    ;;    4) echo "You enter 4"    ;;    5) echo "You enter 5"    ;;    *) echo "error"    ;; Esac

21.yes/no return to different structures

#!/bin/bashclearecho "Enter [y/n]:" Read acase $a in    y| y| yes| YES) echo "You enter $a"    ;;    n| N|no|no) echo "You enter $a"    ;;    *) echo "error"    ;; Esac

22. Killing process

#/bin/bashpid= ' Ps-ef | grep ' process-related content ' | Grep-v ' grep ' | awk ' {print $} ' if [-N ' $pid ']; Then        kill-9 $PIDFI

23. Use of built-in commands

#/bin/bash    Clear        echo "Hello, $USER"        echo               echo "Today's date id ' Date '"        echo        echo "The USER is: "        who        Echo        echo" This is ' uname-s ' "        echo        echo" That's all folks! "

24. Print user without password

#/bin/bashecho "No Password User is:" Echo $ (Cat/etc/shadow | grep "!!" | awk ' BEGIN {fs= ': '}{print $} ')

25. Check if the port number is started

#!/bin/bashn=1echo "Check XXX service ..." While Truedo        if test $n-gt and then                echo "XXX service failed to start" break        fi                       Sleep 5        n=$ (($n + 1))        port= ' netstat-antp | grep "0.0.0.0:8080" '        if [${#port}-gt 3]; then                Echo "XXX Service has been started" break                ;        Fidone

Original link: http://blog.163.com/hbu_lijian/blog/static/1261291532011113035622113/

  

  

  

  

  

  

  

 

  

  

  

Shell Script Instance

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.