shell script编写入门

一.什么是shell script
Shell script是利用shell的功能所写的一个“程序”,这个程序是是使用纯文本文件,将一些shell的语法与命令(含外部命令)写在里面,搭配正则表达式,管道命令与数据流重定向等功能,以达到我们所想要的处理目的。
二.shell编写注意事项:
1.命令的执行是从上而下,从左而右地分析执行;
2.命令,参数间的多个空白都会被忽略掉;
3.空白行也将被忽略掉,并且[tab]按键所得的空白同样视为空格键;
4.如果读取到一个Enter符号(CR),就尝试开始执行该行(或改串)命令;
5.至于如果一行的内容太多,则可以使用“\[Enter]”来扩展至下一行;
6.“#”可作为批注。任何加在#后面的数据将全部被视为批注文字而被忽略。
三.假设我的程序文件名是/home/haha/shell.sh,该如何执行这个文件?
1.直接命令执行:shell.sh文件必须要具备可读可执行(rx)的权限,然后:
(1)绝对路径:使用/home/haha/shell.sh来执行命令;
(2)相对路径:假设工作目录在/home/haha/,则使用./shell.sh来执行;
2.以bash进程来执行:通过“bash shell.sh”或“sh shell.sh”来执行。

四.脚本的编写

示例一:编写一个简单的script
[hongfuhao@centos6 ~]$ mkdir scripts
[hongfuhao@centos6 ~]$ cd scripts/
[hongfuhao@centos6 scripts]$ vim sh01.sh
[html] view plain copy print?
#!/bin/bash  
# program:  
#     This program shows "Hello leiyuxing!" in your screen.  
# History:  
# 2016/10/19 hongfuhao     First release  
PATH=/bin:/sbin:/use/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin  
export PATH  
echo -e "Hello hongfuhao! \a \n"  
exit 0  


程序编写说明:
1.第一行#!/bin/bash声明这个script使用的shell名称
因为我们使用的是bash,所以必须以“#!/bin/bash”来声明这个文件内的语法使用bash的语法。当这个程序被执行时,就能够加载bash的相关环境配置文件(一般来说就是non-login shell的~/.bashrc),并且执行bash来使我们下面的命令能执行。这个至关重要,如果没有设置好这一行,程序可能无法执行,因为系统可能无法判断该程序需要使用寿命shell来执行。
2.程序内容说明
整个script当中,除了第一行的“#!”是用来声明shell的之外,其他#都是“批注”的用途,
故上面的程序中第二行以下就是用来说明整个程序的基本数据。一般来说,建议你养成说明该script的内容与功能,版本信息,作者,联系方式,建立日期,历史记录等习惯,这有助于将来程序的改写与调试。
3.环境变量的声明
PATH与LANG(如果有使用到输出相关的信息时)是当中最重要的!如此一来程序可直接执行一些外部命令,二不必写绝对路径。
4.主要程序部分
echo这一行。
5.告知执行结果
一个命令的执行成功与否,可使用$?这个变量来查看,也可以利用exit 这个命令中断程序,并且传回一个数值给系统。这个程序中博主使用exit 0,这代表离开script并且回传一个0给系统,所以我执行完这个script程序后,若接着执行echo $?则可得到0的值。至此我知道了,利用exit n(n是数字)的功能,我们可以自定义错误信息,让程序变得更加聪明。
用上面的执行方法查看下结果:
[hongfuhao@centos6 scripts]$ sh sh01.sh
Hello hongfuhao!
也可使用
[hongfuhao@centos6 scripts]$ chmod a+x sh01.sh
[hongfuhao@centos6 scripts]$ ./sh01.sh
Hello hongfuhao!
 
示例二:利用read命令的用途编写一个script,让用户输入first name 和last name,最后在屏幕上显示“Your full name is:”内容
[hongfuhao@centos6 scripts]$ vim sh02.sh
[html] view plain copy print?
#!/bin/bash  
# program:  
#     User inputs his first name and last name. program shows his full name.  
# History:  
# 2016/10/20 hongfuhao     First release  
PATH=/bin:/sbin:/use/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin  
export PATH  
   
read -p "Please input your first name: " firstname//提示用户输入  
read -p "Please input your last name: " lastname//提示用户输入  
echo -e "\nYour full name is : $firstname $lastname"//结果由屏幕输出  
[hongfuhao@centos6 scripts]$ sh sh02.sh
Please input your first name: fuhao
Please input your last name: hong
 
Your full name is : fuhao hong

五.script的执行方式区别(source,shscript,./script)
1.利用直接执行方式来执行script
当子进程完成后,子进程内的各项变量或操作将会结束而不会传回到父进程中
例:
[五.script的执行方式区别(source,shscript,./script)
1.利用直接执行方式来执行script
当子进程完成后,子进程内的各项变量或操作将会结束而不会传回到父进程中
例:
[hongfuhao@centos6 scripts]$ echo $firstname $lastname
//确认了这两个变量并不存在
[hongfuhao@centos6 scripts]$ sh sh02.sh
Please input your first name: hongfuhao//这个名字是博主自己输入的
Please input your last name: hongfuhao
 
Your full name is : hongfuhao hongfuhao //在script运行中,这两个变量生效
[leiyuxing@centos6 scripts]$ echo $firstname $lastname
//事实上,这两个变量在父进程的bash中还是不存在的!
2.利用source来执行脚本:在父进程中执行
例:
[hongfuhao@centos6 scripts]$ source sh02.sh
Please input your first name: hongfuhao
Please input your last name: hongfuhao
 
Your full name is : hongfuhao hongfuhao
[hongfuhao@centos6 scripts]$ echo $firstname $lastname
hongfuhao hongfuhao//有数据产生,可以生效!
3.利用test命令的测试功能
比如我要检测dm是否存在时,使用:
[hongfuhao@centos6 ~]$ test -e /dm
发现结果不会显示任何信息
但我们可以通过$?或&&及||来显示整个结果:
[hongfuhao@centos6 ~]$ test -e /dm && echo "exitst" || echo "Not exist"
Not exist
最终的结果可以告知我们是“exist”还是“Not exist”
例:
首先判断一下,让用户输入一个文件名,我们判断:
(1)这个文件是否存在,若不存在给予一个“Filename does not exist”的信息,并中断程序;
(2)若这个文件存在,则判断它是文件还是目录,结果输出“Filename is regular file”或“Filename is directory”;
(3)判断一下,执行者的身份对这个文件或目录所拥有的权限,并输出权限数据。@centos6 scripts]$ echo $firstname $lastname
//确认了这两个变量并不存在
[hongfuhao@centos6 scripts]$ sh sh02.sh
Please input your first name:hongfuhao//这个名字是博主自己输入的
Please input your last name: hongfuhao
 
Your full name is : hongfuhao hongfuhao //在script运行中,这两个变量生效
[hongfuhao@centos6 scripts]$ echo $firstname $lastname
//事实上,这两个变量在父进程的bash中还是不存在的!
2.利用source来执行脚本:在父进程中执行
例:
[hongfuhao@centos6 scripts]$ source sh02.sh
Please input your first name: hongfuhao
Please input your last name: hongfuhao
 
Your full name is : hongfuhao hongfuhao
[hongfuhao@centos6 scripts]$ echo $firstname $lastname
hongfuhao hongfuhao//有数据产生,可以生效!
3.利用test命令的测试功能
比如我要检测dm是否存在时,使用:
[hongfuhao@centos6 ~]$ test -e /dm
发现结果不会显示任何信息
但我们可以通过$?或&&及||来显示整个结果:
[hongfuhao@centos6 ~]$ test -e /dm && echo "exitst" || echo "Not exist"
Not exist
最终的结果可以告知我们是“exist”还是“Not exist”
例:
首先判断一下,让用户输入一个文件名,我们判断:
(1)这个文件是否存在,若不存在给予一个“Filename does not exist”的信息,并中断程序;
(2)若这个文件存在,则判断它是文件还是目录,结果输出“Filename is regular file”或“Filename is directory”;
(3)判断一下,执行者的身份对这个文件或目录所拥有的权限,并输出权限数据。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

煮雨小哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值