SlideShare une entreprise Scribd logo
1  sur  34
Télécharger pour lire hors ligne
The Unix and GNU/Linux Command Line




      Shell Script
            2011 10.28
Shell Script            Tomcat 状态监视脚本

#!/bin/sh

while test true ;do

   ui=$(ps x | egrep tomcat | grep -v grep | 
     awk '{print $1}')

   if [ "$ui" == ""   ] ;then

       /opt/unimas/tomcatui/bin/startup.sh
       sleep 4

   else
      echo "tomcat is running"
   fi

done
Shell Script


      Shell Script 可以用来做什么?

        监控应用程序的运行
Shell Script                  Borealis 构建脚本
#!/bin/bash
export CVS_SANDBOX=$HOME/Workspace/borealis
export BOREALIS_HOME=$CVS_SANDBOX/polaris
source $BOREALIS_TOOL/rc

case "x$1" in
     "x" | "xhelp" )
         echo -e $HELP_TEXT
         ;;
     "xinit" )
         echo "try source build.sh"
         ;;
     "xall" )
         cd utility/unix/
         ./build.borealis.sh
         ./build.borealis.sh -client -tool.marshal -tool.head
         cd -
         ;;
     * )
         echo "Unknow option: "$1"
         ;;
esac
Shell Script           Autotools 辅助脚本
#!/bin/sh

if test ! -f configure.ac; then
    echo Setup must be run from the source directory >&2
    exit 1
fi

rm -rf config
mkdir    config
rm -rf autom4te.cache configure config.status 
    aclocal.m4 Makefile.in

aclocal
libtoolize
autoheader
touch NEWS README AUTHORS ChangeLog
automake --add-missing
autoconf

if test -f config.status; then
    sh config.status
fi
Shell Script


      Shell Script 可以用来做什么?

        监控应用程序的运行
        一键构建脚本,辅助开发,完成繁琐的操作步骤
Shell Script        抓取必应背景图片并设为桌面
#!/bin/bash
DOWN_DIR=$(pwd)       # 下载图片的目录
DOWN_URL="http://cn.bing.com"

cd $DOWN_DIR         # cd 到下载图片的目录去

rm -f /tmp/bing
wget -O /tmp/bing $DOWN_URL
jpg_url=$(cat /tmp/bing | grep -oi 'g_img={url:.*jpg' |
   grep -o '/.*jpg' | sed -e 's///g')

down_jpg_url=http://www.bing.com$jpg_url
wget -nc $down_jpg_url

gconftool-2 -t str –set 
   /desktop/gnome/background/picture_filename 
   $(pwd)/$(echo $jpg_url | awk -F "/" '{print $4}')

cd -    # cd 回到原来的目录
Shell Script


      Shell Script 可以用来做什么?

        监控应用程序的运行
        一键构建脚本,辅助开发,完成繁琐的操作步骤
        自己写些好玩的程序,帮助完成工作
Shell Script


      Shell Script 可以用来做什么?

        监控应用程序的运行
        一键构建脚本,辅助开发,完成繁琐的操作步骤
        自己写些好玩的程序,帮助完成工作


        很多很多其他的应用
Shell Script



               几个简单的例子 ( 前面四个例子 )

               Shell Script 基础
                   输入 / 输出流

               Shell Script 语法简介
Shell Script

   Shell Script 基础

   Linux 有很多的实用小工具,每个小工具只做一件事
   Linux 大部分的配置文件都是以文本格式保存的



    了解系统提供的实用小程序
    熟悉文本处理工具
Shell Script

     了解系统提供的实用小程序

   cat cd chmod chown chgrp cp du df fsck
   ln ls mkdir mount mv pwd rm touch

   kill ps sleep time top

   awk cut head less more sed sort tail 
   tr uniq wc xargs

   alias basename dirname echo expr false 
   printf test true unset
Shell Script

     了解系统提供的实用小程序

   find grep locate whereis which

   netstat ping netcat traceroute ssh wget


   bc cal clear date dd file help info
   size man history tee type yes uname
   whatis
Shell Script

     了解系统提供的实用小程序

   find grep locate whereis which

   netstat ping netcat traceroute ssh wget


   bc cal clear date dd file help info
   size man history tee type yes uname
   whatis

   Shell Script 更多的是一种胶水语言 (glue language)
Shell Script

     熟悉文本处理工具

      cat grep sed awk tail head wc sort

      正则表达式
Shell Script



               几个简单的例子 ( 前面四个例子 )

               Shell Script 基础
                   输入 / 输出流

               Shell Script 语法简介
Shell Script

     输入 / 输出流

      STDIN    0
         $ bc <<< 1+1
      STDOUT   1
         $ echo hello
      STDERR   2
         $ echo error message 1>&2
Shell Script

     输入 / 输出流

      IO Redirection
         $ echo error message 1>&2
         $ echo hello > onefile.txt
         $ echo hello >> onefile.txt
      Pipeline
         $ echo hello | grep ll
Shell Script

   一个例子

 jpg_url=$( 
     cat /tmp/bing | 
     grep -oi 'g_img={url:.*jpg' | 
     grep -o '/.*jpg' | 
     sed -e 's///g' 
 )
Shell Script



               几个简单的例子 ( 前面四个例子 )

               Shell Script 基础
                   输入 / 输出流

               Shell Script 语法简介
Shell Script

     语法简介

      变量赋值
         VAR1=message
         echo $VAR1
      if 语句
         if [ -d /home/apple ]; do
               rm -rf /home/apple
         fi
Shell Script

     语法简介
      for 语句
         for i in 1 2 3 4; do
                echo $(($i * 2))
         done
      while 语句
         while [ 1 ]; then
               echo I am working...
         done
Shell Script

     语法简介
      case 语句
        case "$i" in
              "1" | "2")
                   echo 1
                   ;;
               * )
                   echo unknow...
                   ;;
         esac
Shell Script

     语法简介
      函数的定义与使用
       foo() {
           echo $1
           echo $1
       }

         foo message
Shell Script            Tomcat 状态监视脚本

#!/bin/sh

while test true ;do

   ui=$(ps x | egrep tomcat | grep -v grep | 
     awk '{print $1}')

   if [ "$ui" == ""   ] ;then

       /opt/unimas/tomcatui/bin/startup.sh
       sleep 4

   else
      echo "tomcat is running"
   fi

done
Shell Script            Tomcat 状态监视脚本

#!/bin/sh
                          至少 4 处不妥当的地方
while test true ;do

   ui=$(ps x | egrep tomcat | grep -v grep | 
     awk '{print $1}')

   if [ "$ui" == ""   ] ;then

       /opt/unimas/tomcatui/bin/startup.sh
       sleep 4

   else
      echo "tomcat is running"
   fi

done
Shell Script            Tomcat 状态监视脚本

#!/bin/sh

while test true ;do

   ui=$(ps x | egrep tomcat | grep -v grep | 
     awk '{print $1}')
                                 ui=pgrep tomcat
   if [ "$ui" == ""   ] ;then

       /opt/unimas/tomcatui/bin/startup.sh
       sleep 4

   else
      echo "tomcat is running"
   fi

done
Shell Script            Tomcat 状态监视脚本

#!/bin/sh

while test true ;do     死循环,耗资源
   ui=$(ps x | egrep tomcat | grep -v grep | 
     awk '{print $1}')
                                 ui=pgrep tomcat
   if [ "$ui" == ""   ] ;then

       /opt/unimas/tomcatui/bin/startup.sh
       sleep 4

   else
      echo "tomcat is running"
   fi

done
Shell Script            Tomcat 状态监视脚本

#!/bin/sh

while test true ;do     死循环,耗资源
   ui=$(ps x | egrep tomcat | grep -v grep | 
     awk '{print $1}')
                                 ui=pgrep tomcat
   if [ "$ui" == ""   ] ;then

       /opt/unimas/tomcatui/bin/startup.sh
       sleep 4
                没有判断是否启动成功
   else
      echo "tomcat is running"
   fi

done
Shell Script            Tomcat 状态监视脚本

#!/bin/sh

while test true ;do     死循环,耗资源
   ui=$(ps x | egrep tomcat | grep -v grep | 
     awk '{print $1}')
                                 ui=pgrep tomcat
   if [ "$ui" == ""   ] ;then

       /opt/unimas/tomcatui/bin/startup.sh
       sleep 4
                没有判断是否启动成功
   else
      echo "tomcat is running"
   fi
                仅通过进程号判断 tomcat 状态不合理
done
Shell Script             Tomcat 状态监视脚本

#!/bin/bash

ALIVE_URL=http://127.0.0.1:8080/alive.html
ALIVE=$(w3m -dump $ALIVE_URL)

if [ ! “$ALIVE” == “alive” ]; do
   mail -s "alert-error" hellojinjie@gmail.com 
     <<< “tomcat error, restarting tomcat”

     /opt/bin/tomcat restart

     if [ ! “$?” == “0” ]; then
        mail -s "alert-fatal" hellojinjie@gmail.com 
          <<< “error ocurred while restarting tomcat”
     fi
fi             crontab
               */5 * * * * /path/to/tomcat_monitor.sh
Shell Script

     更多的 ......

      Shell Script 有很多的语法细节
       如何编写跨平台的脚本, unix,bsd,linux
          sh,tcsh, bash, dash, ksh, csh
      sed & awk
      .bashrc & .bash_profile
      Errors and Signals (Traps)
      man & info
Shell Script




        经典教程

         Advanced Bash-Scripting Guide
Shell Script




               Q&A

Contenu connexe

Tendances

Perl在nginx里的应用
Perl在nginx里的应用Perl在nginx里的应用
Perl在nginx里的应用琛琳 饶
 
Java7 fork join framework and closures
Java7 fork join framework and closuresJava7 fork join framework and closures
Java7 fork join framework and closureswang hongjiang
 
Linux常用命令与工具简介
Linux常用命令与工具简介Linux常用命令与工具简介
Linux常用命令与工具简介weihe
 
如何学习Bash Shell
如何学习Bash Shell如何学习Bash Shell
如何学习Bash ShellLI Daobing
 
OpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceOpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceHo Kim
 
常用Mac/Linux命令分享
常用Mac/Linux命令分享常用Mac/Linux命令分享
常用Mac/Linux命令分享Yihua Huang
 
深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)wang hongjiang
 
Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)fisher zheng
 
Golangintro
GolangintroGolangintro
Golangintro理 傅
 
gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務升煌 黃
 
BASH 漏洞深入探討
BASH 漏洞深入探討BASH 漏洞深入探討
BASH 漏洞深入探討Tim Hsu
 
Python xmlrpc-odoo
Python xmlrpc-odooPython xmlrpc-odoo
Python xmlrpc-odoorobin yang
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析Justin Lin
 
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘Liu Allen
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeAngel Boy
 
Vim 由淺入淺
Vim 由淺入淺Vim 由淺入淺
Vim 由淺入淺hydai
 
Linux Binary Exploitation - Stack buffer overflow
Linux Binary Exploitation - Stack buffer overflowLinux Binary Exploitation - Stack buffer overflow
Linux Binary Exploitation - Stack buffer overflowAngel Boy
 

Tendances (20)

Perl在nginx里的应用
Perl在nginx里的应用Perl在nginx里的应用
Perl在nginx里的应用
 
Java7 fork join framework and closures
Java7 fork join framework and closuresJava7 fork join framework and closures
Java7 fork join framework and closures
 
Linux常用命令与工具简介
Linux常用命令与工具简介Linux常用命令与工具简介
Linux常用命令与工具简介
 
如何学习Bash Shell
如何学习Bash Shell如何学习Bash Shell
如何学习Bash Shell
 
OpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceOpenResty/Lua Practical Experience
OpenResty/Lua Practical Experience
 
常用Mac/Linux命令分享
常用Mac/Linux命令分享常用Mac/Linux命令分享
常用Mac/Linux命令分享
 
Execution
ExecutionExecution
Execution
 
Windbg入门
Windbg入门Windbg入门
Windbg入门
 
深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)
 
Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)Nae client(using Node.js to create shell cmd)
Nae client(using Node.js to create shell cmd)
 
Golang
GolangGolang
Golang
 
Golangintro
GolangintroGolangintro
Golangintro
 
gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務
 
BASH 漏洞深入探討
BASH 漏洞深入探討BASH 漏洞深入探討
BASH 漏洞深入探討
 
Python xmlrpc-odoo
Python xmlrpc-odooPython xmlrpc-odoo
Python xmlrpc-odoo
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析
 
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledge
 
Vim 由淺入淺
Vim 由淺入淺Vim 由淺入淺
Vim 由淺入淺
 
Linux Binary Exploitation - Stack buffer overflow
Linux Binary Exploitation - Stack buffer overflowLinux Binary Exploitation - Stack buffer overflow
Linux Binary Exploitation - Stack buffer overflow
 

En vedette

Javascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & AngularJavascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & AngularErik Guzman
 
AngularJS - Overcoming performance issues. Limits.
AngularJS - Overcoming performance issues. Limits.AngularJS - Overcoming performance issues. Limits.
AngularJS - Overcoming performance issues. Limits.Dragos Mihai Rusu
 
Java9 moduulit jigsaw
Java9 moduulit jigsawJava9 moduulit jigsaw
Java9 moduulit jigsawArto Santala
 
JDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerJDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerSimon Ritter
 
Why zsh is Cooler than Your Shell
Why zsh is Cooler than Your ShellWhy zsh is Cooler than Your Shell
Why zsh is Cooler than Your Shellbrendon_jag
 
Groovy Introduction for Java Programmer
Groovy Introduction for Java ProgrammerGroovy Introduction for Java Programmer
Groovy Introduction for Java ProgrammerLi Ding
 
Linux 系統管理與安全:系統防駭與資訊安全
Linux 系統管理與安全:系統防駭與資訊安全Linux 系統管理與安全:系統防駭與資訊安全
Linux 系統管理與安全:系統防駭與資訊安全維泰 蔡
 
Web_DBの監視
Web_DBの監視Web_DBの監視
Web_DBの監視ii012014
 
Nagios的安装部署和与cacti的整合(linuxtone)
Nagios的安装部署和与cacti的整合(linuxtone)Nagios的安装部署和与cacti的整合(linuxtone)
Nagios的安装部署和与cacti的整合(linuxtone)Yiwei Ma
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexKasper de Waard
 
Linux 系統管理與安全:基本 Linux 系統知識
Linux 系統管理與安全:基本 Linux 系統知識Linux 系統管理與安全:基本 Linux 系統知識
Linux 系統管理與安全:基本 Linux 系統知識維泰 蔡
 
配布用Cacti running with cherokee
配布用Cacti running with cherokee配布用Cacti running with cherokee
配布用Cacti running with cherokeeyut148atgmaildotcom
 
Tmux quick-reference
Tmux quick-referenceTmux quick-reference
Tmux quick-referenceRamesh Kumar
 
Webサーバ勉強会#5mod sedについて
Webサーバ勉強会#5mod sedについてWebサーバ勉強会#5mod sedについて
Webサーバ勉強会#5mod sedについてyut148atgmaildotcom
 

En vedette (20)

Bash Scripting
Bash ScriptingBash Scripting
Bash Scripting
 
Bash Scripting
Bash ScriptingBash Scripting
Bash Scripting
 
Javascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & AngularJavascript Memory leaks and Performance & Angular
Javascript Memory leaks and Performance & Angular
 
AngularJS - Overcoming performance issues. Limits.
AngularJS - Overcoming performance issues. Limits.AngularJS - Overcoming performance issues. Limits.
AngularJS - Overcoming performance issues. Limits.
 
Java9 moduulit jigsaw
Java9 moduulit jigsawJava9 moduulit jigsaw
Java9 moduulit jigsaw
 
JDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerJDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java Smaller
 
Why zsh is Cooler than Your Shell
Why zsh is Cooler than Your ShellWhy zsh is Cooler than Your Shell
Why zsh is Cooler than Your Shell
 
Groovy Introduction for Java Programmer
Groovy Introduction for Java ProgrammerGroovy Introduction for Java Programmer
Groovy Introduction for Java Programmer
 
Linux 系統管理與安全:系統防駭與資訊安全
Linux 系統管理與安全:系統防駭與資訊安全Linux 系統管理與安全:系統防駭與資訊安全
Linux 系統管理與安全:系統防駭與資訊安全
 
Web_DBの監視
Web_DBの監視Web_DBの監視
Web_DBの監視
 
Nagios的安装部署和与cacti的整合(linuxtone)
Nagios的安装部署和与cacti的整合(linuxtone)Nagios的安装部署和与cacti的整合(linuxtone)
Nagios的安装部署和与cacti的整合(linuxtone)
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regex
 
Linux 系統管理與安全:基本 Linux 系統知識
Linux 系統管理與安全:基本 Linux 系統知識Linux 系統管理與安全:基本 Linux 系統知識
Linux 系統管理與安全:基本 Linux 系統知識
 
配布用Cacti running with cherokee
配布用Cacti running with cherokee配布用Cacti running with cherokee
配布用Cacti running with cherokee
 
Donate Organs
Donate OrgansDonate Organs
Donate Organs
 
Tmux quick-reference
Tmux quick-referenceTmux quick-reference
Tmux quick-reference
 
RHEL roadmap
RHEL roadmapRHEL roadmap
RHEL roadmap
 
Webサーバ勉強会#5mod sedについて
Webサーバ勉強会#5mod sedについてWebサーバ勉強会#5mod sedについて
Webサーバ勉強会#5mod sedについて
 
UNIX SHELL IN DBA EVERYDAY
UNIX SHELL IN DBA EVERYDAYUNIX SHELL IN DBA EVERYDAY
UNIX SHELL IN DBA EVERYDAY
 
Symfony2 and AngularJS
Symfony2 and AngularJSSymfony2 and AngularJS
Symfony2 and AngularJS
 

Similaire à shell script introduction

Erlang Practice
Erlang PracticeErlang Practice
Erlang Practicelitaocheng
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践yiditushe
 
1, shell intro
1, shell intro1, shell intro
1, shell introted-xu
 
makefile20141121
makefile20141121makefile20141121
makefile20141121Kevin Wu
 
Erlang jiacheng
Erlang jiachengErlang jiacheng
Erlang jiachengAir-Smile
 
Javascript Training
Javascript TrainingJavascript Training
Javascript Trainingbeijing.josh
 
Talking about exploit writing
Talking about exploit writingTalking about exploit writing
Talking about exploit writingsbha0909
 
Shell脚本
Shell脚本Shell脚本
Shell脚本bj
 
利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版xingsu1021
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享Chong-Kuan Chen
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhouWill Zhou
 
2, bash synax simplified
2, bash synax simplified2, bash synax simplified
2, bash synax simplifiedted-xu
 
Vim hacks
Vim hacksVim hacks
Vim hacksXuYj
 
Bash shell script 教學
Bash shell script 教學Bash shell script 教學
Bash shell script 教學Ming-Sian Lin
 
Shell(bash) Scripting
Shell(bash) ScriptingShell(bash) Scripting
Shell(bash) ScriptingRobby Lee
 
OpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part IOpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part IHung-yu Lin
 
COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境
COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境
COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境均民 戴
 
Ruby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuRuby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuMarsZ Chen
 

Similaire à shell script introduction (20)

Erlang Practice
Erlang PracticeErlang Practice
Erlang Practice
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践
 
1, shell intro
1, shell intro1, shell intro
1, shell intro
 
makefile20141121
makefile20141121makefile20141121
makefile20141121
 
Erlang jiacheng
Erlang jiachengErlang jiacheng
Erlang jiacheng
 
Rootkit 101
Rootkit 101Rootkit 101
Rootkit 101
 
Javascript Training
Javascript TrainingJavascript Training
Javascript Training
 
Talking about exploit writing
Talking about exploit writingTalking about exploit writing
Talking about exploit writing
 
Shell脚本
Shell脚本Shell脚本
Shell脚本
 
利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版利用Cent Os快速构建自己的发行版
利用Cent Os快速构建自己的发行版
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhou
 
2, bash synax simplified
2, bash synax simplified2, bash synax simplified
2, bash synax simplified
 
Vim hacks
Vim hacksVim hacks
Vim hacks
 
Php
PhpPhp
Php
 
Bash shell script 教學
Bash shell script 教學Bash shell script 教學
Bash shell script 教學
 
Shell(bash) Scripting
Shell(bash) ScriptingShell(bash) Scripting
Shell(bash) Scripting
 
OpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part IOpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part I
 
COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境
COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境
COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境
 
Ruby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuRuby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for Ubuntu
 

shell script introduction

  • 1. The Unix and GNU/Linux Command Line Shell Script 2011 10.28
  • 2. Shell Script Tomcat 状态监视脚本 #!/bin/sh while test true ;do ui=$(ps x | egrep tomcat | grep -v grep | awk '{print $1}') if [ "$ui" == "" ] ;then /opt/unimas/tomcatui/bin/startup.sh sleep 4 else echo "tomcat is running" fi done
  • 3. Shell Script Shell Script 可以用来做什么? 监控应用程序的运行
  • 4. Shell Script Borealis 构建脚本 #!/bin/bash export CVS_SANDBOX=$HOME/Workspace/borealis export BOREALIS_HOME=$CVS_SANDBOX/polaris source $BOREALIS_TOOL/rc case "x$1" in "x" | "xhelp" ) echo -e $HELP_TEXT ;; "xinit" ) echo "try source build.sh" ;; "xall" ) cd utility/unix/ ./build.borealis.sh ./build.borealis.sh -client -tool.marshal -tool.head cd - ;; * ) echo "Unknow option: "$1" ;; esac
  • 5. Shell Script Autotools 辅助脚本 #!/bin/sh if test ! -f configure.ac; then echo Setup must be run from the source directory >&2 exit 1 fi rm -rf config mkdir config rm -rf autom4te.cache configure config.status aclocal.m4 Makefile.in aclocal libtoolize autoheader touch NEWS README AUTHORS ChangeLog automake --add-missing autoconf if test -f config.status; then sh config.status fi
  • 6. Shell Script Shell Script 可以用来做什么? 监控应用程序的运行 一键构建脚本,辅助开发,完成繁琐的操作步骤
  • 7. Shell Script 抓取必应背景图片并设为桌面 #!/bin/bash DOWN_DIR=$(pwd) # 下载图片的目录 DOWN_URL="http://cn.bing.com" cd $DOWN_DIR # cd 到下载图片的目录去 rm -f /tmp/bing wget -O /tmp/bing $DOWN_URL jpg_url=$(cat /tmp/bing | grep -oi 'g_img={url:.*jpg' | grep -o '/.*jpg' | sed -e 's///g') down_jpg_url=http://www.bing.com$jpg_url wget -nc $down_jpg_url gconftool-2 -t str –set /desktop/gnome/background/picture_filename $(pwd)/$(echo $jpg_url | awk -F "/" '{print $4}') cd - # cd 回到原来的目录
  • 8. Shell Script Shell Script 可以用来做什么? 监控应用程序的运行 一键构建脚本,辅助开发,完成繁琐的操作步骤 自己写些好玩的程序,帮助完成工作
  • 9. Shell Script Shell Script 可以用来做什么? 监控应用程序的运行 一键构建脚本,辅助开发,完成繁琐的操作步骤 自己写些好玩的程序,帮助完成工作 很多很多其他的应用
  • 10. Shell Script 几个简单的例子 ( 前面四个例子 ) Shell Script 基础 输入 / 输出流 Shell Script 语法简介
  • 11. Shell Script Shell Script 基础 Linux 有很多的实用小工具,每个小工具只做一件事 Linux 大部分的配置文件都是以文本格式保存的 了解系统提供的实用小程序 熟悉文本处理工具
  • 12. Shell Script 了解系统提供的实用小程序 cat cd chmod chown chgrp cp du df fsck ln ls mkdir mount mv pwd rm touch kill ps sleep time top awk cut head less more sed sort tail  tr uniq wc xargs alias basename dirname echo expr false  printf test true unset
  • 13. Shell Script 了解系统提供的实用小程序 find grep locate whereis which netstat ping netcat traceroute ssh wget bc cal clear date dd file help info size man history tee type yes uname whatis
  • 14. Shell Script 了解系统提供的实用小程序 find grep locate whereis which netstat ping netcat traceroute ssh wget bc cal clear date dd file help info size man history tee type yes uname whatis Shell Script 更多的是一种胶水语言 (glue language)
  • 15. Shell Script 熟悉文本处理工具 cat grep sed awk tail head wc sort 正则表达式
  • 16. Shell Script 几个简单的例子 ( 前面四个例子 ) Shell Script 基础 输入 / 输出流 Shell Script 语法简介
  • 17. Shell Script 输入 / 输出流 STDIN 0 $ bc <<< 1+1 STDOUT 1 $ echo hello STDERR 2 $ echo error message 1>&2
  • 18. Shell Script 输入 / 输出流 IO Redirection $ echo error message 1>&2 $ echo hello > onefile.txt $ echo hello >> onefile.txt Pipeline $ echo hello | grep ll
  • 19. Shell Script 一个例子 jpg_url=$( cat /tmp/bing | grep -oi 'g_img={url:.*jpg' | grep -o '/.*jpg' | sed -e 's///g' )
  • 20. Shell Script 几个简单的例子 ( 前面四个例子 ) Shell Script 基础 输入 / 输出流 Shell Script 语法简介
  • 21. Shell Script 语法简介 变量赋值 VAR1=message echo $VAR1 if 语句 if [ -d /home/apple ]; do rm -rf /home/apple fi
  • 22. Shell Script 语法简介 for 语句 for i in 1 2 3 4; do echo $(($i * 2)) done while 语句 while [ 1 ]; then echo I am working... done
  • 23. Shell Script 语法简介 case 语句 case "$i" in "1" | "2") echo 1 ;; * ) echo unknow... ;; esac
  • 24. Shell Script 语法简介 函数的定义与使用 foo() { echo $1 echo $1 } foo message
  • 25. Shell Script Tomcat 状态监视脚本 #!/bin/sh while test true ;do ui=$(ps x | egrep tomcat | grep -v grep | awk '{print $1}') if [ "$ui" == "" ] ;then /opt/unimas/tomcatui/bin/startup.sh sleep 4 else echo "tomcat is running" fi done
  • 26. Shell Script Tomcat 状态监视脚本 #!/bin/sh 至少 4 处不妥当的地方 while test true ;do ui=$(ps x | egrep tomcat | grep -v grep | awk '{print $1}') if [ "$ui" == "" ] ;then /opt/unimas/tomcatui/bin/startup.sh sleep 4 else echo "tomcat is running" fi done
  • 27. Shell Script Tomcat 状态监视脚本 #!/bin/sh while test true ;do ui=$(ps x | egrep tomcat | grep -v grep | awk '{print $1}') ui=pgrep tomcat if [ "$ui" == "" ] ;then /opt/unimas/tomcatui/bin/startup.sh sleep 4 else echo "tomcat is running" fi done
  • 28. Shell Script Tomcat 状态监视脚本 #!/bin/sh while test true ;do 死循环,耗资源 ui=$(ps x | egrep tomcat | grep -v grep | awk '{print $1}') ui=pgrep tomcat if [ "$ui" == "" ] ;then /opt/unimas/tomcatui/bin/startup.sh sleep 4 else echo "tomcat is running" fi done
  • 29. Shell Script Tomcat 状态监视脚本 #!/bin/sh while test true ;do 死循环,耗资源 ui=$(ps x | egrep tomcat | grep -v grep | awk '{print $1}') ui=pgrep tomcat if [ "$ui" == "" ] ;then /opt/unimas/tomcatui/bin/startup.sh sleep 4 没有判断是否启动成功 else echo "tomcat is running" fi done
  • 30. Shell Script Tomcat 状态监视脚本 #!/bin/sh while test true ;do 死循环,耗资源 ui=$(ps x | egrep tomcat | grep -v grep | awk '{print $1}') ui=pgrep tomcat if [ "$ui" == "" ] ;then /opt/unimas/tomcatui/bin/startup.sh sleep 4 没有判断是否启动成功 else echo "tomcat is running" fi 仅通过进程号判断 tomcat 状态不合理 done
  • 31. Shell Script Tomcat 状态监视脚本 #!/bin/bash ALIVE_URL=http://127.0.0.1:8080/alive.html ALIVE=$(w3m -dump $ALIVE_URL) if [ ! “$ALIVE” == “alive” ]; do mail -s "alert-error" hellojinjie@gmail.com <<< “tomcat error, restarting tomcat” /opt/bin/tomcat restart if [ ! “$?” == “0” ]; then mail -s "alert-fatal" hellojinjie@gmail.com <<< “error ocurred while restarting tomcat” fi fi crontab */5 * * * * /path/to/tomcat_monitor.sh
  • 32. Shell Script 更多的 ...... Shell Script 有很多的语法细节 如何编写跨平台的脚本, unix,bsd,linux sh,tcsh, bash, dash, ksh, csh sed & awk .bashrc & .bash_profile Errors and Signals (Traps) man & info
  • 33. Shell Script 经典教程 Advanced Bash-Scripting Guide