我的编程空间,编程开发者的网络收藏夹
学习永远不晚

linux基础之Shell Script入门介绍

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

linux基础之Shell Script入门介绍

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!an"
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
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 $?
1

注意[]内空格必须有
这种方法和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
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
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的执行过程

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

linux基础之Shell Script入门介绍

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

linux基础之Shell Script入门介绍

linux基础之Shell Script 1 Shell Scipt使用指令和基本程序设计结构写成的程序,可以完成复杂的处理流程 1.1 程序书写#!/bin/bash# Program:# This program shows
2022-06-04

Python 入门基础1 --语言介绍

本节目录:一、编程语言介绍二、python解释器介绍三、安装python解释器四.运行python程序的两种方式五、变量六、后期补充内容一、编程语言介绍  1.机器语言:    直接用二进制编程,直接控制硬件,需要掌握硬件的操作细节  2.
2023-01-30

PHP循环函数使用介绍之PHP基础入门教程

PHP循环主要有四种:while,do…while,for,foreach。下面我们分开讲解每种循环的用法
2022-11-15

Linux 学习基础入门之Linux分区

安装Linux,首先要有镜像文件,以CentOS为例,可以在官网或者国内某些镜像Server来获取镜像。根据自己的需要可以选择是 Everything 或者minimal iso.这里不写关于安装的细节,对于其中的一些关键步骤做些说明。1.
2023-06-05

零基础入门篇之Linux及Arm-Linux程序开发笔记

前言:本文记录了自己从一个完全不懂Linux的人如何一步步学会Linux程序开发的过程。当然也希望本文能够达到它的目的,让那些和我一样没有任何基础的人也能快速入门Linux程序开发。 一、Arm-Linux程序开发平台简要介绍Arm-Lin
2022-06-04

Linux shell脚本基础学习详细介绍(完整版)第1/2页

Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头、注释、变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提。1. Linux 脚本编写基础◆1.1 语法基本介绍
2022-06-04

Linux 脚本编写基础知识以及在shell脚本中可以使用三类命令介绍

本篇内容主要讲解“Linux 脚本编写基础知识以及在shell脚本中可以使用三类命令介绍”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux 脚本编写基础知识以及在shell脚本中可以使用三
2023-06-09

编程热搜

目录