菜鸟的《Linux程序设计》学习—shell script
1. 认识shell script
2. shell script程序编写
[yr@localhost shellscript]$ vim helloworld.sh //在打开的文本中,编辑程序,helloworld.sh #!/bin/bash
#Program:
# This is my first shell script program. It will show "Hello World!" on
# the screen.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello World! \a \n"
exit 0
程序内容解析:
3. 程序的编译运行
[yr@localhost shellscript]$ sh helloworld.sh
Hello World!
[yr@localhost shellscript]$ chmod +x helloworld.sh
(2)使用source filename 或者. filename 执行—— 父进程执行
[yr@localhost shellscript]$ vim helloworld.sh //在打开的文本中,编辑程序,helloworld.sh #!/bin/bash
#Program:
# This is my first shell script program. It will show "Hello World!" on
# the screen.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello World! \a \n"
# 删除最后一行 exit 0
下面执行命令:
[yr@localhost shellscript]$ source helloworld.sh Hello World!
<pre code_snippet_id="666753" snippet_file_name="blog_20150514_3_8535676" name="code" class="cpp">[yr@localhost shellscript]$ . helloworld.sh Hello World!
我们看到,以上两种方式,都可以顺利成功的将脚本和helloworld.sh执行完毕。
4. 两种编译方式详解
[yr@localhost shellscript]$ vim test.sh //下面是程序内容
#!/bin/bash
#Program
# User inputs his first name and last name. Program shows his full name.
#History:
# 2015/05/14 shine_yr First release
PATH=/bin:/sbin:/usr/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" #结果在屏幕输出 # exit 0 我们不用退出命令
首先,利用直接执行的方式来执行脚本:
[yr@localhost shellscript]$ sh test.sh
Please input your first name: shine
Please input your last name: yr Your full name is: shine yr
[yr@localhost shellscript]$ echo $firstname [yr@localhost shellscript]$ echo $lastname [yr@localhost shellscript]$
从上面可以看出,程序顺利执行,然后我利用echo命令打算输出firstname以及lastname的内容,结果却输出为空。
然后,使用source filename 或者 . filename的方式执行脚本:
[yr@localhost shellscript]$ source test.sh
Please input your first name: shine
Please input your last name: yr Your full name is: shine yr
[yr@localhost shellscript]$ echo $firstname
shine
[yr@localhost shellscript]$ echo $lastname
yr
[yr@localhost shellscript]$
我们可以看到,此时变量firstname以及lastname 中是有确切值的。
5. 编写shell script的良好习惯
菜鸟的《Linux程序设计》学习—shell script的更多相关文章
- 菜鸟教程之学习Shell script笔记(上)
菜鸟教程之学习Shell script笔记 以下内容是,学习菜鸟shell教程整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-shell.ht ...
- linux基础之Shell Script入门介绍
本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下. 本文转自:http://www.jbxue.com ...
- 菜鸟教程之学习Shell script笔记(下)
菜鸟教程Shell script学习笔记(下) 以下内容是学习菜鸟教程之shell教程,所整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-sh ...
- 菜鸟教程之学习Shell script笔记(中)
菜鸟教程Shell script学习笔记(中) 以下内容是学习菜鸟教程之shell教程,所整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-sh ...
- bash shell学习-shell script基础 (笔记)
A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版) Linux ...
- Linux程序设计之shell程序设计
看了<linux程序设计(第4版)>(作者:Neil Matthew ,Richard Stones ,陈建 ,宋健建译).做个笔记,以备后面查看. 首先,清楚几个概念. shell编程属 ...
- Linux基础之-shell script(变量,运算符,流程控制,函数)
一.shell script Shell 脚本(shell script),是一种为shell编写的脚本程序.业界所说的shell通常都是指shell脚本,但读者朋友要知道,shell和shell s ...
- Linux程序设计学习笔记(独乐乐版)
在Android的开发过程中经常会遇到Linux相关的问题,为了更彻底的了解Linux准备整点没用的,找到一本 <Linux程序设计>开始系统的学习. 期间记录下自认为重要的内容,本以为是 ...
- Linux08--Shell程序设计03 shell script
第一个Shell脚本——HelloWorld [root@localhost ~]# vi sh01.sh #!/bin/bash #!表明使用哪种shell # this is my first s ...
随机推荐
- 牛客寒假6-G.区间或和
链接:https://ac.nowcoder.com/acm/contest/332/G 题意: 求a|(a+1)|(a+2)|...|(b-1)|b. 思路: 求a-b的差的每一个二进制位 自己也看 ...
- 解决thymeleaf严格html5校验的方法
用的是springboot加thyemleaf做静态模板. 然后会有个很烦的东西,就这个静态模板对html的格式非常严格,导致很多框架的格式都用不了,然后这里有个解除的方法: 1.在pom中添加依赖: ...
- Dubbo理论知识
本文是作者根据官方文档以及自己平时的使用情况,对 Dubbo 所做的一个总结.如果不懂 Dubbo 的使用的话,可以参考我的这篇文章<超详细,新手都能看懂 !使用SpringBoot+Dubbo ...
- AJPFX分享JAVA修饰符详解
1.类的修饰符分为:可访问控制符和非访问控制符两种. 可访问控制符是:公共类修饰符 public 非访问控制符有:抽象类修饰符 abstract :最终类修饰符 final 1 )公共类修饰符 pub ...
- 访问者模式和php实现
访问者模式: 表示作用于某个对象结构中的各个元素的操作.它使你可以在不改变各个元素类的前提下定义作用于这些元素的操作. 角色: 1)抽象访问者:为该对象结构中具体元素角色声明一个访问操作接口.该操作接 ...
- mybatis实现使用原生的sql
1.相应的xml文件中 <select id="selectByCategories" resultType="map" parameterType=&q ...
- LR中常见请求的使用示例
Action(){ //application/x-www-form-urlencoded //application/json //web_add_auto_header("Content ...
- js对象引用的注意
var p = {}; var arr = []; function a(param) { // var i = param.a; for (var i = 0; i < 3; i++) { p ...
- bat 符号说明
netstat -an|findstr 139 ipconfig/all findstr IP ipconfig/all |findstr 物理地址 定值选行 ipconf ...
- vba控制图表,excel图表,一键完成
来源http://club.excelhome.net/thread-1417686-1-1.html 官方教程链接 https://docs.microsoft.com/zh-cn/office/v ...