shell_example01

2018-11-27  本文已影响2人  Kate_Blog

demo

    #!/bin/bash 
    echo "scripting is fun!" 
    --------------------------    
    chmod 755 script.sh 
    ./script.sh

not just shell script

    #!/usr/bin/python 
    print "this is a python script" 
    ------------------------------
    chmod 755 hi.py 
    ./hi.py

Variables

Variable Usage

    #!/bin/bash 
    MY_SHELL="bash" 
    echo "I like the $MY_SHELL shell."
    #!/bin/bash 
    MY_SHELL="bash"
    echo "I like the ${MY_SHELL} shell."
    #!/bin/bash
    MY_SHELL="bash"
    echo "I am ${MY_SHELL}ing on my keyboard."
    
    OUTPUT:
    I am bashing on my keyboard.
    #!/bin/bash
    MY_SHELL="bash"
    echo "I am $MY_SHELLing on my keyboard."
    
    OUTPUT:
    I am  on my keyboard.
    #!/bin/bash
    SERVER_NAME=$(hostname)
    echo "you are running this script on ${SERVER_NAME}."
    
    OUTPUT:
    you are running this script on linuxsvr.
    #!/bin/bash
    SERVER_NAME=`hostname`
    echo "you are running this script on ${SERVER_NAME}."
    
    OUTPUT:
    you are running this script on linuxsvr.

Variable Names(等号前后不能有空格)

Tests:success:0 failed:1

File operators(tests)

    -d FILE     ture if file is directory.
    -e FILE     true if file exists.   
    -f FILE     true if file exists and is a regular file
    -r FILE     true if file is readable by you
    -s FILE     true if file exists and is not empty.
    -w FILE     true if file is writable to you.
    -x FILE     true is file is executable by you.

String operators(tests)

    -z STRING   true if string is empty.
    -n STRING   true if string is not empty.
    STRING1=STRING2     true is the strings are equal.
    STRING1!=STRING2    true is the strings are not equal.

Arithmetic operators(tests)

    arg1 -eq arg2
    arg1 -ne arg2
    
    arg1 -lt arg2
    arg1 -le arg2
    
    arg1 -gt arg2
    arg1 -ge arg2

Making Decisions - The if statement

    if [ condiction-is-true ]
    then
        command 1
        command 2
        command N
    fi
    #!/bin/bash
    MY_SHELL="bash"
    if [ "$MY_SHELL" = "bash" ]
    then
        echo "you seen to like the bash shell."
    fi
    
    OUTPUT:
    you seen to like the bash shell.

if/else

    if [ condition-is-true ]
    then
        command N
    else
        command N
    fi
    #!/bin/bash
    MY_SHELL="csh"
    if [ "$MY_SHELL" = "bash" ]
    then
        echo "you seem to like bash shell."
    else
        echo "you don't seem to like bash shell."
    fi

if/elif/else

    if [ condiction-is-true ]
    then
        command N
    elif [ condition-is-true ]
    then
        command N
    else 
        command N
    fi  
    #!/bin/bash
    MY_SHELL="csh"
    if [ "$MY_SHELL" = "bash" ]
    then
        echo "you seem to like bash shell."
    elif [ "$MY_SHELL" = "csh" ]
    then
        echo "you don't seem to like csh shell."
    else
        echo "you don't seem to like csh or bash shells."
    fi

For loop

    for VARIABLE_NAME in ITEM1 ITEM_N
    do
        command 1
        command 2
        command N
    done    
    #!/bin/bash
    for COLOR in red green blue
    do
        echo "COLOR: $COLOR"
    done
   OUTPUT:
   COLOR: red
   COLOR: green
   COLOR: blue
    #!/bin/bash
    COLORS="red green blue"
    
    for COLOR in $COLORS
    do
        echo "COLOR: $COLOR"
    done
   OUTPUT:
   COLOR: red
   COLOR: green
   COLOR: blue
    #!/bin/bash
    PICTURES=$(ls *jpg)
    DATE=$(date +%F)
    for PICTURE in $PICTURES
    do
        echo "Renaming ${PICTURE} to ${DATE}-${PICTURE}"
        mv ${PICTURE} ${DATE}-${PICTURE}
    done
    
    OUTPUT:
    $ ls
    bear.jpg
    $ ./rename-pics.sh
    Renaming bear.jpg to 2015-03-06-bear.jpg
    $ ls
    2015-03-06-bear.jpg 

parameters 0 -9

Exit statuses

Exit Status / Return Code

Checking the exit status

    ls /not/here
    echo "$?"
    
    OUTPUT:
    2
    HOST="google.com"
    ping -c l $HOST
    if [ "$?" -eq "0" ]
    then
        echo "$HOST reachable."
    else
        echo "$HOST unreachable."
    fi     
    HOST="google.com"
    ping -c l $HOST
    if [ "$?" -nq "0" ]
    then
        echo "$HOST unreachable."
    fi     
    HOST="google.com"
    ping -c l $HOST
    RETURN_CODE=$?
    
    if [ "$RETURN_CODE" -ne "0" ]
    then
        echo "$HOST unreachable."
    fi     

&& and ||

    mkdir /tmp/bak && cp test.txt /tmp/bak/
    cp test.txt /tmp/bak/ || cp test.txt /tmp
    #!/bin/bash
    HOST="google.com"
    ping -c l $HOST && echo "$HOST reachable."
    #!/bin/bash
    HOST="google.com"
    ping -c l $HOST || echo "$HOST unreachable."

The semicolon ;

    cp test.txt /tmp/bk/ ; cp test.txt /tmp
    #Same as:
    cp test.txt /tmp/bk/
    cp test.txt /tmp

Exit command

    HOST="google.com"
    ping -v 1 $HOST
    if [ "$?" -ne "0" ]
    then
        echo "$HOST unreachable."
        exit 1
    fi
    exit 0

Summary

Functions

Functions

    function function-name(){
        #code goes here
      }
    
    function-name(){
        #code goes here
    }
    
    #!/bin/bash
    function hello(){
        echo "Hello!"
    }
    hello
    #!/bin/bash
    function hello(){
        echo "hello!"
        now
    }
    function now(){
        echo "It's $(date +%r)"
    }
    hello
    #!/bin/bash
    function hello(){
        echo "hello!"
        now
    }
    hello # 会报错的
    function now(){
        echo "It's $(date +%r)"
    }

Positional Parameters

    #!/bin/bash
    function hello(){
        echo "Hello $1"
    }
    hello Jason
    
    # OUTPUT:
    # Hello Jason
    #!/bin/bash
    function hello(){
        for NAME in $@
        do  
            echo "hello $NAME"
        done
    }
    
    hello jason dan ryan
    # OUTPUT:
    # Hello jason
    # Hello dan
    # Hello v    

variable scope

    GLOBAL_VAR=1
    # GLOBAL_VAL is available
    # in the function.
    my_function
    # GLOBAL_VAL is NOT available
    # in the function.
    my_function
    GLOBAL_VAR=1
    #!/bin/bash
    my_function(){
       GLOBAL_VAR=1 
    }
    
    # GLOBAL_VAL is NOT available yet
    echo $GLOBAL_VAR
    my_function
    # GLOBAL_VAL is NOW available yet
    echo $GLOBAL_VAR

local Variables

Exit Status(Return Code)

    my_function
    echo $?
    # todo :.

Summary

Wildcards

More Wildcards-Character Classes

more wildcards - Ranges

Name character class

Mathching wildcard patterns

summary

why use wildcards?

    #!/bin/bash
    cd /var/www
    cp *.html /var/www-just-html
    #!/bin/bash
    cd /var/www
    for FILE in *.html
    do
        echo "copy $FILE"
        cp $FILE /var/www-just-html
    done
    
    OUTPUT:
    copy about.html
    copy content.html
    copy index.html
    #!/bin/bash
    
    for FILE in /var/www/*.html
    do
        echo "copy $FILE"
        cp $FILE /var/www-just-html
    done
    
    OUTPUT:
    copy /var/www/about.html
    copy /var/www/content.html
    copy /var/www/index.html

summary

Case Statements

case "$VAR" in
    pattern_1)
        # commands go here
        ;;
    patter_N)
        # commands go here    
        ;;
esac
    case "$1" in
        start)
            /usr/sbin/sshd
            ;;
        stop)
            kill $(cat /var/run/sshd.pid)
            ;;
    esac           
    case "$1" in
        start)
            /usr/sbin/sshd
            ;;
        stop)
            kill $(cat /var/run/sshd.pid)
            ;;
        *)
            echo "Usage: $0 start|stop" ; exit 1
            ;;    
    esac           
    case "$1" in
        start|START)
            /usr/sbin/sshd
            ;;
        stop|STOP)
            kill $(cat /var/run/sshd.pid)
            ;;
        *)
            echo "Usage: $0 start|stop" ; exit 1
            ;;    
    esac           
    read -p "Enter y or n: " ANSWER
    case "$ANSWER" in
        [yY]|[yY][eE][sS])
            echo "your answered yes."
            ;;
        [nN]|[nN][oO])
            echo "your answered no."
            ;;
        *)
            echo "Invalid answer."
        ;;
     esac                   
    read -p "Enter y or n: " ANSWER
    case "$ANSWER" in
        [yY]*)
            echo "your answered yes."
            ;;
        *)
            echo "Invalid answer."
        ;;
     esac                   

summary

Logging

what you will learn

Logging

todo:

summary

while loops

    while [ condition_is _true ]
    do
        command 1
        command 2
        command N
    done
    while [ condition_is_true ]
    do
        # commands change the condition
        command 1
        command 2
        command N
    done

infinite loops

    while [ condition_is_true ]
    do
        # commands do not change
        # the condition
        command N  
    done
上一篇 下一篇

猜你喜欢

热点阅读