菜鸟的《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 ...
随机推荐
- [BZOJ5219]最长路径
Description 在Byteland一共有n个城市,编号依次为1到n,它们之间计划修建n(n-1)/2条单向道路,对于任意两个不同的点i和 j,在它们之间有且仅有一条单向道路,方向要么是i到j, ...
- Points on Plane Codeforces - 576C
https://www.luogu.org/problemnew/show/CF576C 看题面,一眼按莫队的方法排一下 直接交就会和我一样发现WA掉了... 算一下会发现,上限是3e9(块内左端点1 ...
- 本地连接远程Oracle数据库
由于项目开发测试,需要在本地连接远程的Oracle数据库 之前搭过环境,但是重装了系统,现在又重新装一遍 软件安装 连接远程Oracle需要两个软件: 一个Oracle客户端,instantclien ...
- nodejs 生成验证码
此方法需要nodejs 安装canvas 扩展 准备工作 以Linux为例 1.服务器gcc版本需4.8以上 2.安装所需扩展 yum install cairo cairo-devel cairom ...
- Ionic之$scope 依赖注入报错
在开发Ionic过程中,发现会报在 LoginController 中引用locals报错,具体报错问题: ionic.bundle.js:19526 Error: [$injector:unpr] ...
- 使用aptana执行jruby
Apatana Studio只会找ruby/bin的ruby执行档....为了在Apatana Studio用JRuby,除了设定好Path之外还要在JRuby/bin下建立一的ruby.bat,里面 ...
- webpack(2)
在普通页面中使用render函数渲染组件 render:function(createElements){//createElements是一个方法,调用它,能够把指定的组件模板,渲染成为HTML结构 ...
- hihocoder1860 最大异或和
思路: 把N个前缀异或和插入一棵trie树中,然后对每个前缀异或和x计算能使x ^ y最大的前缀异或和y.利用了异或运算的a ^ b ^ a = b的性质. 参考了https://cloud.tenc ...
- 如何使用KeyChain保存和获取UDID
本文是iOS7系列文章第一篇文章,主要介绍使用KeyChain保存和获取APP数据,解决iOS7上获取不变UDID的问题.并给出一个获取UDID的工具类,使用方便,只需要替换两个地方即可. 一.iOS ...
- watchguard 软件工程师内部招聘!
作为watchguard正式员工,现发布公司最近的招聘信息,待遇优厚,请符合条件的朋友和我联系并将简历发给我,我会尽早联系公司人力部门. 我的邮件:daibao91888@163.com 博客:htt ...