Related case actions for Shell commands

Source: Internet
Author: User

Case 1: variables, functions, judgments, comparisons

test.sh

[JavaScript]View PlainCopy
  1. #!/bin/bash
  2. Echo ======================== This is a bash demo========================
  3. printf "%s is%d years old \ n" Zhengjinwei
  4. DECLARE count=10
  5. DECLARE fruit=apple
  6. Declare strnormal="We have $count ${fruit} ' s"
  7. Declare strspecial="We have $count ${fruit}"
  8. function Printstr ()
  9. {
  10. if [$count-gt 1]; Then
  11. echo "$strNormal"
  12. Else
  13. echo "$strSpecial"
  14. Fi
  15. }
  16. Printstr
  17. echo $ (uname)
  18. #########################################
  19. DECLARE num=1000
  20. function Printnum ()
  21. {
  22. Local num=10
    1. Let num+=10
    2. Echo $num
    3. }
    4. Echo "The global num is: $num"
    5. ECHO-E "Call Printnum Function:"
    6. Printnum
    7. o "==================this is if else fi .... Test demo====================="
    8. If [-F "test.sh"]; Then
    9. echo "named Test.sh is a file:"
    10. Else
    11. echo "named test.sh is not a file:"
    12. Fi
    13. function Checkvariablehavenum ()
    14. {
    15. if [-N '];
    16. echo "This variable has value"
    17. Else
    18. echo "This variable has not value"
    19. Fi
    20. }
    21. DECLARE g_num=10
    22. Checkvariablehavenum $g _num
    23. function Checkvariableequal ()
    24. {
      1. If ["$"-eq "$" ]; Then
      2. printf "%d = =%d" $
      3. elif [ "$"-ne "$"]; Then
      4. printf "%d! =%d" $ A
      5. elif [ "$"-ge "$"]; Then
      6. printf "%d >=%d" $
      7. elif [ "$"-gt "$"]; Then
      8. printf "%d >%d" $
      9. elif [ "$"-lt "$"]; Then
      10. printf "%d <%d" $
      11. Else
      12. printf "%d <=%d" $
      13. Fi
      14. }
      15. Checkvariableequal 2 4
      16. Checkvariableequal 4 2
      17. Checkvariableequal 2 2

    25. Case 2: String manipulation (take substring)

      [JavaScript]View PlainCopy
      1. echo "-------------------------------------"
      2. ####### #this is String opt################################
      3. #get string ' s length
      4. DECLARE strname="Zhengjinwei"
      5. DECLARE strnamelength=${#strName}
      6. printf "The length of%s is%d \ n" $strName $strNameLength
      7. function getchildstring ()
      8. {
      9. Local strresourse=$1
      10. Local nparamcount=$2
      11. Local nstartpos=$3
      12. Local nendpos=$4
      13. if [[$nParamCount-lt 3]]; Then
      14. echo "param less than 3 is err"
      15. elif [[$nParamCount-GT 4]]; Then
      16. echo "param more than 4 is err"
      17. Else
      18. if [[$nParamCount-eq 3]]; Then
      19. Let nendpos=${#StrResourse}
      20. Else
      21. Let nendpos=$4
      22. Fi
      23. Fi
      24. Local strresult=${strresourse: $nStartPos: $nEndPos}
          1. printf "The child string in%s from position%d to%d is%s: \n\n\n" $StrResourse $nStartPos $nEndPos $strResult
          2. }
          3. Getchildstring "Zhengjinwei" 4 2 6
          4. Getchildstring "Zhengjinwei" 3 2
      25. Case 3: Iterations, arrays

        [JavaScript]View PlainCopy
        1. ###########################################################
        2. <span style="FONT-SIZE:24PX;" ># This is array test demo
        3. Declare array_int= (1 2 3 4 5 6 7)
        4. printf "The int value in Array_int which index was%d is%d\n\n" 0 ${array_int["0"}
        5. echo "=============for loop=============================="
        6. DECLARE array_int_len=${#array_int [*]}
        7. For ((i=0;i<"$array _int_len"; i++))
        8. Do
        9. Echo-e ${array_int[$i]}
        10. Done
        11. echo ""
        12. echo "============for iterator loop================================"
        13. For value in ${array_int[@]}
        14. Do
        15. ECHO-E $value
        16. Done
        17. echo ""
        18. echo "==========while loop=================================="
        19. DECLARE i=0
        20. while (($i < $array _int_len))
        21. Do
        22. Echo-e ${array_int[$i]}
        23. Let I=i+1
        24. Done
        25. ########################################################</span>

        Case 4: Time action

        [JavaScript]View PlainCopy
        1. function Getnowtime ()
        2. {
        3. Local _date=$ (date +%y-%m-%d:%h-%m-%s:%a)
        4. Echo $_date
        5. }
        6. function Getcosttime ()
        7. {
        8. Local _start=$ (date +%s)
        9. Sleep 2
        10. Local _end=$ (date +%s)
        11. Local _cost=$ ((_end-_start))
        12. Echo $_cost
        13. }
        14. function Getdifftimebetweeninseconds ()
        15. {
        16. Local _start=$ (date +%s-d ' 2010-09-01 17:00:00 ')
        17. Local _end=$ (date +%s-d ' 2011-09-01 18:40:40 ')
        18. tep=$ (($_end-$_start))
        19. tep2=$ ((3600*24))
        20. result=$ (($tep/$tep 2))
        21. extra=$[$tep% $tep 2]
        22. DECLARE _hours=0
        23. DECLARE _seconds=0
        24. if [$extra-ge 3600]; Then
        25. _hours=$ (($extra/3600))
        26. _seconds=$ (($extra%3600))
        27. Else
          1. _hours=0
          2. _seconds= $extra
          3. Fi
          4. printf "%d days---%d hours----%d seconds \ n" $result $_hours $_seconds
          5. }
          6. Getnowtime
          7. Getcosttime
          8. Getdifftimebetweeninseconds

Related case actions for Shell commands

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.