zoukankan      html  css  js  c++  java
  • shell编程

    $ echo '#!/bin/sh' > my-script.sh
    $ echo 'echo Hello World' >> my-script.sh
    $ chmod 755 my-script.sh
    $ ./my-script.sh
    Hello World
    $

    上面这段代码中,echo是打印的意思,而>是重定向的意思,chmod是修改权限的意思。shell脚本以.sh为结尾。

    #!/bin/sh
    # This is a comment!
    echo Hello World    # This is a comment, too!

    以上为my-script.sh中的代码,可以学习一下如何写注释。

    $ chmod a+rx my-script.sh
    $ ./my-script.sh

    想要将shell脚本变成可执行的,可以使用以上语句。

    grep "mystring" /tmp/myfile

    这句的意思是将在/tmp/myfile中的"mystring"字符串搜索出来。

    #!/bin/sh
    # This is a comment!
    echo Hello World        # This is a comment, too!

    以上是first.sh中的代码。可以使用以下代码执行:

    $ chmod 755 first.sh
    $ ./first.sh
    Hello World
    $

    得到结果:

    $ echo Hello World
    Hello World
    $
    #!/bin/sh
    MY_MESSAGE="Hello World"
    echo $MY_MESSAGE

    以上是var.sh的代码。用来学习变量的使用。环境变量使用以下代码实现:

    $ export name=zuoyuan
    #!/bin/sh
    echo What is your name?
    read MY_NAME
    echo "Hello $MY_NAME - hope you're well."

    以上是var2.sh的代码。

    #!/bin/sh
    echo "What is your name?"
    read USER_NAME
    echo "Hello $USER_NAME"
    echo "I will create you a file called ${USER_NAME}_file"
    touch "${USER_NAME}_file"

    以上代码可以创建一个文件,touch关键字的作用是如果有这个文件,不做任何操作,如果没有这个文件,就创建一个这个文件。

    #!/bin/sh
    for i in 1 2 3 4 5
    do
      echo "Looping ... number $i"
    done

    以上是for循环。写入for.sh

    #!/bin/sh
    for i in hello 1 * 2 goodbye
    do
      echo "Looping ... i is set to $i"
    done

    继续for循环。写入for2.sh中。

    #!/bin/sh
    INPUT_STRING=hello
    while [ "$INPUT_STRING" != "bye" ]
    do
      echo "Please type something in (bye to quit)"
      read INPUT_STRING
      echo "You typed: $INPUT_STRING"
    done

    while循环。

    #!/bin/sh
    while :
    do
      echo "Please type something in (^C to quit)"
      read INPUT_STRING
      echo "You typed: $INPUT_STRING"
    done

    继续while循环。

    #!/bin/sh
    while read f
    do
      case $f in
        hello)        echo English    ;;
        howdy)        echo American    ;;
        gday)        echo Australian    ;;
        bonjour)    echo French    ;;
        "guten tag")    echo German    ;;
        *)        echo Unknown Language: $f
            ;;
       esac
    done < myfile

    使用while循环读取文件。

    for runlevel in 0 1 2 3 4 5 6 S
    do
      mkdir rc${runlevel}.d
    done

    批量建文件。

    if [ ... ]
    then
      # if-code
    else
      # else-code
    fi
    if [ ... ]; then
      # do something
    fi
    if  [ something ]; then
     echo "Something"
     elif [ something_else ]; then
       echo "Something else"
     else
       echo "None of the above"
    fi

    以上是几种条件结构。

    #!/bin/sh
    if [ "$X" -lt "0" ]
    then
      echo "X is less than zero"
    fi
    if [ "$X" -gt "0" ]; then
      echo "X is more than zero"
    fi
    [ "$X" -le "0" ] && 
          echo "X is less than or equal to  zero"
    [ "$X" -ge "0" ] && 
          echo "X is more than or equal to zero"
    [ "$X" = "0" ] && 
          echo "X is the string or number "0""
    [ "$X" = "hello" ] && 
          echo "X matches the string "hello""
    [ "$X" != "hello" ] && 
          echo "X is not the string "hello""
    [ -n "$X" ] && 
          echo "X is of nonzero length"
    [ -f "$X" ] && 
          echo "X is the path of a real file" || 
          echo "No such file: $X"
    [ -x "$X" ] && 
          echo "X is the path of an executable file"
    [ "$X" -nt "/etc/passwd" ] && 
          echo "X is a file which is newer than /etc/passwd"
    echo -en "Please guess the magic number: "
    read X
    echo $X | grep "[^0-9]" > /dev/null 2>&1
    if [ "$?" -eq "0" ]; then
      # If the grep found something other than 0-9
      # then it's not an integer.
      echo "Sorry, wanted a number"
    else
      # The grep found only 0-9, so it's an integer. 
      # We can safely do a test on it.
      if [ "$X" -eq "7" ]; then
        echo "You entered the magic number!"
      fi
    fi
    #!/bin/sh
    X=0
    while [ -n "$X" ]
    do
      echo "Enter some text (RETURN to quit)"
      read X
      echo "You said: $X"
    done
    #!/bin/sh
    X=0
    while [ -n "$X" ]
    do
      echo "Enter some text (RETURN to quit)"
      read X
      if [ -n "$X" ]; then
        echo "You said: $X"
      fi
    done
    if [ "$X" -lt "0" ]
    then
      echo "X is less than zero"
    fi
    
    ..........  and  ........
    
    if [ ! -n "$X" ]; then
      echo "You said: $X"
    fi

    以上是几个条件结构的例子。

    继续举几个例子。

    #!/bin/sh
    
    echo "Please talk to me ..."
    while :
    do
      read INPUT_STRING
      case $INPUT_STRING in
        hello)
            echo "Hello yourself!"
            ;;
        bye)
            echo "See you again!"
            break
            ;;
        *)
            echo "Sorry, I don't understand"
            ;;
      esac
    done
    echo 
    echo "That's all folks!"

    运行后有以下结果。

    $ ./talk.sh
    Please talk to me ...
    hello
    Hello yourself!
    What do you think of politics?
    Sorry, I don't understand
    bye
    See you again!
    
    That's all folks!
    $

    继续学习变量。以下代码写入var3.sh

    #!/bin/sh
    echo "I was called with $# parameters"
    echo "My name is $0"
    echo "My first parameter is $1"
    echo "My second parameter is $2"
    echo "All parameters are $@"

    运行结果如下:

    $ /home/steve/var3.sh
    I was called with 0 parameters
    My name is /home/steve/var3.sh
    My first parameter is
    My second parameter is    
    All parameters are 
    $
    $ ./var3.sh hello world earth
    I was called with 3 parameters
    My name is ./var3.sh
    My first parameter is hello
    My second parameter is world
    All parameters are hello world earth

    继续学习变量。以下代码写入var4.sh

    #!/bin/sh
    while [ "$#" -gt "0" ]
    do
      echo "$1 is $1"
      shift
    done

    This script keeps on using shift until $# is down to zero, at which point the list is empty. Another special variable is $?. This contains the exit value of the last run command. So the code:

    #!/bin/sh
    /usr/local/bin/my-command
    if [ "$?" -ne "0" ]; then
      echo "Sorry, we had a problem there!"
    fi

    当进程退出时的代码为0时,就没有问题。

    #!/bin/sh
    old_IFS="$IFS"
    IFS=:
    echo "Please input some data separated by colons ..."
    read x y z
    IFS=$old_IFS
    echo "x is $x y is $y z is $z"

    写入var5.sh。运行如下:

    $ ./ifs.sh
    Please input some data separated by colons ...
    hello:how are you:today
    x is hello y is how are you z is today
    $ ./ifs.sh
    Please input some data separated by colons ...
    hello:how are you:today:my:friend
    x is hello y is how are you z is today:my:friend

    使用默认变量。

    #!/bin/sh
    echo -en "What is your name [ `whoami` ] "
    read myname
    if [ -z "$myname" ]; then
      myname=`whoami`
    fi
    echo "Your name is : $myname"

    函数的用法。

    #!/bin/sh
    # A simple script with a function...
    
    add_a_user()
    {
      USER=$1
      PASSWORD=$2
      shift; shift;
      # Having shifted twice, the rest is now comments ...
      COMMENTS=$@
      echo "Adding user $USER ..."
      echo useradd -c "$COMMENTS" $USER
      echo passwd $USER $PASSWORD
      echo "Added user $USER ($COMMENTS) with pass $PASSWORD"
    }
    
    ###
    # Main body of script starts here
    ###
    echo "Start of script..."
    add_a_user bob letmein Bob Holness the presenter
    add_a_user fred badpassword Fred Durst the singer
    add_a_user bilko worsepassword Sgt. Bilko the role model
    echo "End of script..."

    阶乘的shell实现,使用递归。

    #!/bin/sh
    
    factorial()
    {
      if [ "$1" -gt "1" ]; then
        i=`expr $1 - 1`
        j=`factorial $i`
        k=`expr $1 * $j`
        echo $k
      else
        echo 1
      fi
    }
    
    
    while :
    do
      echo "Enter a number:"
      read x
      factorial $x
    done 

    模块化编程。以下代码写入common.lib

    # common.lib
    # Note no #!/bin/sh as this should not spawn 
    # an extra shell. It's not the end of the world 
    # to have one, but clearer not to.
    #
    STD_MSG="About to rename some files..."
    
    rename()
    {
      # expects to be called as: rename .txt .bak 
      FROM=$1
      TO=$2
    
      for i in *$FROM
      do
        j=`basename $i $FROM`
        mv $i ${j}$TO
      done
    }

    以下代码写入function2.sh

    #!/bin/sh
    # function2.sh
    . ./common.lib
    echo $STD_MSG
    rename .txt .bak

    返回码。

    #!/bin/sh
    
    adduser()
    {
      USER=$1
      PASSWORD=$2
      shift ; shift
      COMMENTS=$@
      useradd -c "${COMMENTS}" $USER
      if [ "$?" -ne "0" ]; then
        echo "Useradd failed"
        return 1
      fi
      passwd $USER $PASSWORD
      if [ "$?" -ne "0" ]; then
        echo "Setting password failed"
        return 2
      fi
      echo "Added user $USER ($COMMENTS) with pass $PASSWORD"
    }
    
    ## Main script starts here
    
    adduser bob letmein Bob Holness from Blockbusters
    ADDUSER_RETURN_CODE=$?
    if [ "$ADDUSER_RETURN_CODE" -eq "1" ]; then
      echo "Something went wrong with useradd"
    elif [ "$ADDUSER_RETURN_CODE" -eq "2" ]; then 
       echo "Something went wrong with passwd"
    else
      echo "Bob Holness added to the system."
    fi

    crontab的使用

    在考虑向cron进程提交一个crontab文件之前,首先要做的一件事情就是设置环境变量EDITOR。cron进程根据它来确定使用哪个编辑器编辑crontab文件。9 9 %的UNIX和LINUX用户都使用vi,如果你也是这样,那么你就编辑$ HOME目录下的. profile文件,在其中加入这样一行:

    EDITOR=vi; export EDITOR

    然后保存并退出。不妨创建一个名为<user>cron的文件,其中<user>是用户名,例如,davecron。在该文件中加入如下的内容。

    # (put your own initials here)echo the date to the console every
    # 15minutes between 6pm and 6am
    0,15,30,45 18-06 * * * /bin/echo 'date' > /dev/console

    保存并退出。确信前面5个域用空格分隔。

    在上面的例子中,系统将每隔1 5分钟向控制台输出一次当前时间。如果系统崩溃或挂起,从最后所显示的时间就可以一眼看出系统是什么时间停止工作的。在有些系统中,用tty1来表示控制台,可以根据实际情况对上面的例子进行相应的修改。为了提交你刚刚创建的crontab文件,可以把这个新创建的文件作为cron命令的参数:

    $ crontab davecron

    现在该文件已经提交给cron进程,它将每隔15分钟运行一次。

    同时,新创建文件的一个副本已经被放在/var/spool/cron目录中,文件名就是用户名(即dave)。

    为了列出crontab文件,可以用:

    $ crontab -l
  • 相关阅读:
    应用一:Vue之开发环境搭建
    基于vue项目的js工具方法汇总
    JavaScript 格式化数字、金额、千分位、保留几位小数、舍入舍去… 及其浮点数计算精度问题(推荐的类库 Numeral.js 和 accounting.js)
    Redis源码分析(二十五)--- zmalloc内存分配实现
    Redis源码分析(二十五)--- zmalloc内存分配实现
    SpringBoot系列——WebMvcConfigurer介绍
    跨域问题与SpringBoot解决方案
    源码分析SpringBoot启动
    SpringBoot+SpringSecurity+jwt整合及初体验
    【mysql学习】InnoDB数据结构
  • 原文地址:https://www.cnblogs.com/lz0504/p/9382369.html
Copyright © 2011-2022 走看看