本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下。
本文转自:http://www.jbxue.com/article/13302.html

linux基础之Shell Script

1 Shell Scipt
使用指令和基本程序设计结构写成的程序,可以完成复杂的处理流程

1.1 程序书写

复制代码代码如下:
#!/bin/bash
# Program:
#       This program shows "Hello Wrold" in your screen.
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
echo -e "Hello World!\a\n"
exit 0

第一行 #!/bin/bash 说明使用的shell类型,不同shell语法可能不同,所以要说明使用的是哪种shell
其它#开始的表示注释,注释一般需要说明
程序功能
版本历史
作者及联系方式
设置好PATH变量,以便直接可以调用相应路径下的命令
程序主体部分
exit 0 表示程序执行成功,向环境返回0
1.2 程序执行
bash $bash sh01.sh #如果用sh sh01.sh而sh又不是指向bash,那么sh01.sh内的语法就会不一致,因为用 #sh去解释了bash语法写的shell script,针对这个程序,如果 #$type sh #得到sh is hashed (/bin/sh) #那么会输出-e Hello world!,而非Hello world!

复制代码代码如下:
$./xxx.sh $chmod +x sh01.sh $./sh01.sh
source $ source sh01.sh

注:用bash和用source的不同在于,用bash执行时,shell script其实是在在父程序bash下新建了一个 bash子程序,这个子程序中执行,当程序执行完后,shell script里定义的变量都会随子程序的结束而消失, 而用source执行时,是在父程序bash中执行,shell script里定义的变量都还在。

2 简单Shell练习

2.1 例1 接收用户输入

复制代码代码如下:
# !/bin/bash
# Program:
#       This program is used to read user's input 
# Site: www.jbxue.com
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Your first name:" firstname # tell user to input
read -p "Your last name:" lastname # tell user to input
echo -e "\nYour full name: $firstname $lastname"
exit 0 

调用:

复制代码代码如下:
$ bash sh02.sh
Your first name:Minix
Your last name:007
Your full name: Minix 007

2.2 例2 按日期建立相似名字的文件

复制代码代码如下:
# !/bin/bash
# Program:
#       This program is used to create files according to date
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
# Get filename from user
echo -e "I will use 'touch' to create three files."
read -p "Please input your filename:" tmpfilename
# Prevent the user input [Enter]
# Check whether filename exists or not
filename=${tmpfilename:-"filename"}
# Get the final filename according to date
date1=$(date --date='2 days ago' +%Y%m%d) # date of 2 days ago
date2=$(date --date='1 days ago' +%Y%m%d) # date of yesterday
date3=$(date +%Y%m%d) # date of today
filename1=${filename}${date1}
filename2=${filename}${date2}
filename3=${filename}${date3}
# Create file
touch "$filename1"
touch "$filename2"
touch "$filename3"
exit 0 

调用:

复制代码代码如下:
$ bash sh03.sh
I will use 'touch' to create three files.
Please input your filename:WhoKnows
$ ls W*
WhoKnows20130201  WhoKnows20130202  WhoKnows20130203

3 判断式
3.1 测试文件是否存在
test -e filename会根据filename是否存在返回0或1,再交由echo显示结果:

复制代码代码如下:
$ test -e sh01.sh  && echo "Exists" || echo "Not exists"
Exists
$ test -e sh0x.sh  && echo "Exists" || echo "Not exists"
Not exists

3.2 test常用选项
3.2.1 文件类型

复制代码代码如下:
-e file :file是否存在
-f file :file是否存在且为文件
-d file :file是否存在且为目录

3.2.2 权限
-r file :file是否有读的权限

3.2.3 文件新旧比较
-nt file1 file2 : file1 是否比 file2新

3.2.4 整数,字符串,多重条件判断
-z string: string是否为空
例:输出指定文件类型及属性

复制代码代码如下:
# !/bin/bash
# Program:
#       This program is used to output type and permission of the target file
# History:
# 2013/2/3 on_1y First release
#site www.jbxue.com
PATH=$PATH
export PATH
# Get filename from user
echo -e "Input name of the file that you want to check.\n"
read -p "Filename:" filename
test -z $filename && echo "You must input a filename." && exit 0
# Check whether the file exists or not
test ! -e $filename && echo "The file '$filename' DO NOT exists" && exit 0
# Check type and permission of the file
test -f $filename && filetype="regular file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable"
# Output result
echo "The filename:$filename is a $filetype"
echo "And Permissions are :$perm"
exit 0

调用:

复制代码代码如下:
$ bash sh04.sh
Input name of the file that you want to check.

Filename:sh01.sh
The filename:sh01.sh is a regular file
And Permissions are :readable writable executable

3.3 使用[]判断

测试文件是否存在

复制代码代码如下:
$ [ -e "sh01.sh" ] ; echo $?
0
$ [ -e "sh0x.sh" ] ; echo $?

注意[]内空格必须有
这种方法和test的test -e "sho1.sh" ; echo $? 是一致的

4 Shell Script 参数

复制代码代码如下:
# !/bin/bash
# Program:
#       This program is used to ouput parameter of the shell script
# History:
# 2013/2/3 on_1y First release
# site www.jbxue.com
PATH=$PATH
export PATH
echo "The script's name is ==> $0"
echo "Total parameter number is ==> $#"
# Check whether number of the parameter is less than 2
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2.Stop here." && exit 0
echo "The whole parameter is ==> '$@'"
echo "The first parameter is ==> $1"
echo "The first parameter is ==> $2"
exit 0

调用:

复制代码代码如下:
$ bash sh05.sh 1a 2b 3c 4d
The script's name is ==> sh05.sh
Total parameter number is ==> 4
The whole parameter is ==> '1a 2b 3c 4d'
The first parameter is ==> 1a
The first parameter is ==> 2b

注:从以上程序可以看出与参数有关的预设变量如何表示

5 条件表达式

5.1 if 结构

复制代码代码如下:
# !/bin/bash
# Program:
#       This program is used to show if else expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Please input [Y/N]" choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
    echo "OK, continue"
    exit 0
fi
if [ "$choice" == "N" ] || [ "$choice" == "n" ];then
    echo "Oh, interupt"
    exit 0
fi
exit 0 

调用:

复制代码代码如下:
$ bash sh06.sh
Please input [Y/N]y
OK, continue
$ bash sh06.sh
Please input [Y/N]n
Oh, interupt

5.2 if else 结构

复制代码代码如下:
# !/bin/bash
# Program:
#       This program is used to show if else expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Please input [Y/N]" choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
    echo "OK, continue"
    exit 0
elif [ "$choice" == "N" ] || [ "$choice" == "n" ];then
    echo "Oh, interupt"
    exit 0
else
    echo "Input [Y/N]"
fi
exit 0 

5.3 case

复制代码代码如下:
# !/bin/bash
# Program:
#       This program is used to show case expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Tell me your choice:[1-3]=>" choice
case $choice in
    "1")
        echo "Your choice is ONE"

"2")
        echo "Your choice is TWO"

"3")
        echo "Your choice is THREE"

esac
exit 0

调用:

复制代码代码如下:
$ bash sh08.sh
Tell me your choice:[1-3]=>2
Your choice is TWO
$ bash sh08.sh
Tell me your choice:[1-3]=>1
Your choice is ONE
$ bash sh08.sh
Tell me your choice:[1-3]=>3
Your choice is THREE

6 函数

复制代码代码如下:
# !/bin/bash
# Program:
#       This program is used to test function
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
function myprint(){
    echo -n "Your choice is "
}
read -p "Tell me your choice:[1-3]=>" choice
case $choice in
    "1")
        myprint;echo "ONE"

"2")
        myprint;echo "TWO"

"3")
        myprint;echo "THREE"

esac
exit 0 

调用:

复制代码代码如下:
$ bash sh09.sh 
Tell me your choice:[1-3]=>1
Your choice is ONE
$ bash sh09.sh 
Tell me your choice:[1-3]=>2
Your choice is TWO
$ bash sh09.sh 
Tell me your choice:[1-3]=>3
Your choice is THREE

7 循环
7.1 while

复制代码代码如下:
# !/bin/bash
# Program:
#       This program shows while expression
# History:
# 2013/2/3 on_1y First release
# site www.jbxue.com
PATH=$PATH
export PATH
while [ "$choice" != "yes" ]
do
    read -p "Give your choice [yes/no]:" choice
done
exit 0 

调用:

复制代码代码如下:
$ bash sh10.sh 
Give your choice [yes/no]:no
Give your choice [yes/no]:no
Give your choice [yes/no]:nx
Give your choice [yes/no]:yes

7.2 for

复制代码代码如下:
# !/bin/bash
# Program:
#       This program is used to demo for expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
for choice in 1 2 3
do
    echo "your choice is $choice"
done
exit 0

调用示例:

复制代码代码如下:
$ bash sh11.sh
your choice is 1
your choice is 2
your choice is 3

8 shell script的追踪与Debug
sh -n xx.sh # 语法检查
sh -x xx.sh # 列出xx.sh的执行过程

linux基础之Shell Script入门介绍的更多相关文章

  1. Linux基础之-shell script(变量,运算符,流程控制,函数)

    一.shell script Shell 脚本(shell script),是一种为shell编写的脚本程序.业界所说的shell通常都是指shell脚本,但读者朋友要知道,shell和shell s ...

  2. shell script 入门 笔记

    shell script 入门 在 shell script 注意必须使用完全相同写在下面: 1.  指令的运行是从上而下.从左而右的分析与运行: 2.  指令的运行就如同第五章内提到的: 指令.选项 ...

  3. shell script入门

    从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...

  4. Shell Script 入门教程

    和 Shell 的区别 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁. Shell 即是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提 ...

  5. 【Hadoop离线基础总结】HDFS入门介绍

    HDFS入门介绍 概述 HDFS全称为Hadoop Distribute File System,也就是Hadoop分布式文件系统,是Hadoop的核心组件之一. 分布式文件系统是横跨在多台计算机上的 ...

  6. Linux下的shell编程入门

    通常情况下,我们从命令行输入命令每输入一次就能够得到系统的一次响应.一旦需要我们一个接着一个的输入命令而最后才得到结果的时候,这样的做法显然就没有效率.要达到这样的目的,通常我们利用shell程序或者 ...

  7. Linux基础篇–shell脚本编程基础

    本章内容概要  编程基础  脚本基本格式  变量  运算  条件测试  配置用户环境 7.1 编程基础程序:指令+数据程序编程风格:   过程式:以指令为中心,数据服务于指令   对象式:以数据为中心 ...

  8. Linux基础(五) Shell函数

    Shell 函数 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action ...

  9. [linux基础学习]默认的目录介绍

    以下用一个表格来罗列linux默认的目录或文件及其用途: 目录/文件 用途 来源 / /处于Linux文件系统树形结构的最顶端,它是Linux文件系统的入口,所有的目录.文件.设备都在/之下. - / ...

随机推荐

  1. java Graphics2D 画图

    在Java中,当需要画一些特殊的形状时,比如说椭圆.矩形等,可以使用 Graphics2D 来绘图. 一些API: g.drawLine(3,3,50,50);//画一条线段 g.drawRect(8 ...

  2. 【JAVA - 基础】之Annotation注解浅析

    注解在JAVA中,尤其是一些ORM框架(如Hibernate等)中是比较常用的一种机制. 注解是JAVA 1.5之后引入的新功能,正确来说是反射的一部分,没有反射,注解也就无法正常使用.注解可以理解成 ...

  3. Xcode6中使用initWithTitle:title image:image selectedImage:自定义图片

    使用xcode6来运行项目,发现使用原生的tabbar上的图片不显示了. 这个问题是因为xcode6中的一些api方法被废弃了,同时tabbar上图片的渲染方式发生了改变. 先看xcode6中的tab ...

  4. socket编程——一个简单的样例

    从一个简单的使用TCP样例開始socket编程,其基本过程例如以下: server                                                  client ++ ...

  5. FireMonkey 使用Webbrowser

    DELPHI XE5 源码PASCAL:http://files.cnblogs.com/xe2011/FireMonkey_Webbrowser.rar 为了这个用上webbrowser真是费太大劲 ...

  6. C中sizeof()的用法——32位和64位下的sizeof()

    机器平台:X86_64 处理器 操作系统:Red Hat 4.1.2-14 编译器: gcc version 4.1.2 20070626 Size of char is:               ...

  7. radioButton的简单使用

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  8. hibernate缓存技术

    1.缓存 2.Hibernate 缓存作用:为了提高查询效率. 3.第一次操作某个对象的时候,把操作的对象数据存储到缓存中,然后下一次在对同一个对象操作的时候,就不会在连接数据库. 4.Hiberna ...

  9. tomcat中设置多项目共享jar;类包

    随着服务器上的tomcat部署的项目越来越多,最近在部署一个新的项目的时候出现内存溢出的错误 Exception in thread "main" java.lang.OutOfM ...

  10. xml中使用foreach遍历对象

    如果是一个带数据的List对象 <select id="selectProductMSTList" resultType="java.util.Map" ...